Merge branch 'dev.gateway.aliyuninitecs' into dev
commit
75f8da414d
|
@ -0,0 +1,44 @@
|
||||||
|
package com.muyu.common.rabbit.callback;
|
||||||
|
|
||||||
|
import jakarta.annotation.PostConstruct;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.amqp.rabbit.connection.CorrelationData;
|
||||||
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Lenovo
|
||||||
|
* @Description 消息发送到broker确认回调
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class ConfirmCallback implements RabbitTemplate.ConfirmCallback {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private RabbitTemplate rabbitTemplate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化
|
||||||
|
*/
|
||||||
|
@PostConstruct
|
||||||
|
public void init() {
|
||||||
|
this.rabbitTemplate.setConfirmCallback(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 回调确认方法(消息发送之后 消息无论发送成功还是失败都会执行这个回调方法)
|
||||||
|
* @param correlationData 回调的相关数据
|
||||||
|
* @param ack 确认结果(true表示消息已经被broker接收,false表示消息未被broker接收)
|
||||||
|
* @param cause 失败原因(当ack为false时,表示拒绝接收消息的原因;当ack为true时,该值为空)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
|
||||||
|
if (ack) {
|
||||||
|
log.info("消息发送到broker成功!");
|
||||||
|
} else {
|
||||||
|
log.info("消息发送到broker失败,失败原因:{}", cause);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,39 @@
|
||||||
|
package com.muyu.common.rabbit.callback;
|
||||||
|
|
||||||
|
import jakarta.annotation.PostConstruct;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.amqp.core.ReturnedMessage;
|
||||||
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Lenovo
|
||||||
|
* @Description 消息发送失败时回调
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class ReturnsCallback implements RabbitTemplate.ReturnsCallback {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private RabbitTemplate rabbitTemplate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化
|
||||||
|
*/
|
||||||
|
@PostConstruct
|
||||||
|
public void init() {
|
||||||
|
rabbitTemplate.setReturnsCallback(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 消息发送到队列失败时执行
|
||||||
|
* @param returnedMessage 返回的消息和元数据
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void returnedMessage(ReturnedMessage returnedMessage) {
|
||||||
|
log.info("消息:{}被交换机:{}回退!回退原因:{}", returnedMessage.getMessage().toString(),
|
||||||
|
returnedMessage.getExchange(), returnedMessage.getReplyText());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,72 @@
|
||||||
|
package com.muyu.common.rabbit.config;
|
||||||
|
|
||||||
|
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
|
||||||
|
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
|
||||||
|
import org.springframework.amqp.rabbit.core.RabbitAdmin;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Lenovo
|
||||||
|
* @Description RabbitMQ连接和管理功能配置
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class RabbitAdminConfig {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RabbitMQ服务器的主机地址
|
||||||
|
*/
|
||||||
|
@Value("${spring.rabbitmq.host}")
|
||||||
|
private String host;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RabbitMQ的用户名
|
||||||
|
*/
|
||||||
|
@Value("${spring.rabbitmq.username}")
|
||||||
|
private String username;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RabbitMQ的密码
|
||||||
|
*/
|
||||||
|
@Value("${spring.rabbitmq.password}")
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RabbitMQ的虚拟主机
|
||||||
|
*/
|
||||||
|
@Value("${spring.rabbitmq.virtualhost}")
|
||||||
|
private String virtualhost;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建并初始化RabbitAdmin实例
|
||||||
|
*
|
||||||
|
* @return RabbitAdmin 实例
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public RabbitAdmin rabbitAdmin() {
|
||||||
|
RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory());
|
||||||
|
rabbitAdmin.setAutoStartup(true);
|
||||||
|
return rabbitAdmin;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建RabbitMQ连接工厂
|
||||||
|
*
|
||||||
|
* @return ConnectionFactory 实例
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public ConnectionFactory connectionFactory() {
|
||||||
|
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
|
||||||
|
connectionFactory.setAddresses(host);
|
||||||
|
connectionFactory.setUsername(username);
|
||||||
|
connectionFactory.setPassword(password);
|
||||||
|
connectionFactory.setVirtualHost(virtualhost);
|
||||||
|
|
||||||
|
// 配置发送确认回调时,次配置必须配置,否则即使在RabbitTemplate配置了ConfirmCallback也不会生效
|
||||||
|
connectionFactory.setPublisherConfirmType(CachingConnectionFactory.ConfirmType.CORRELATED);
|
||||||
|
connectionFactory.setPublisherReturns(true);
|
||||||
|
return connectionFactory;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,57 @@
|
||||||
|
package com.muyu.common.rabbit.config;
|
||||||
|
|
||||||
|
import org.springframework.amqp.rabbit.annotation.RabbitListenerConfigurer;
|
||||||
|
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
|
||||||
|
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistrar;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
|
||||||
|
import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Lenovo
|
||||||
|
* @Description rabbitMQ的监听器配置
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class RabbitListenerConfig implements RabbitListenerConfigurer {
|
||||||
|
|
||||||
|
static {
|
||||||
|
// 设置为信任所有类型的反序列化,确保消息能够正确反序列化
|
||||||
|
System.setProperty("spring.amqp.deserialization.trust.all", "true");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RabbitMQ连接工厂,用于创建连接
|
||||||
|
*/
|
||||||
|
@Autowired
|
||||||
|
public ConnectionFactory connectionFactory;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建处理器方法工厂的bean
|
||||||
|
*
|
||||||
|
* @return DefaultMessageHandlerMethodFactory 实例,用于处理消息的转换和方法调用
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public DefaultMessageHandlerMethodFactory handlerMethodFactory() {
|
||||||
|
DefaultMessageHandlerMethodFactory factory = new DefaultMessageHandlerMethodFactory();
|
||||||
|
// 这里的转换器设置实现了 通过 @Payload 注解 自动反序列化message body
|
||||||
|
factory.setMessageConverter(new MappingJackson2MessageConverter());
|
||||||
|
return factory;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 配置RabbitMQ监听器的消息处理方法工厂。
|
||||||
|
*
|
||||||
|
* @param rabbitListenerEndpointRegistrar 实例,用于注册监听器的配置
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void configureRabbitListeners(RabbitListenerEndpointRegistrar rabbitListenerEndpointRegistrar) {
|
||||||
|
// 注册自定义的消息处理方法工厂
|
||||||
|
rabbitListenerEndpointRegistrar.setMessageHandlerMethodFactory(handlerMethodFactory());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
|
||||||
|
package com.muyu.common.rabbit.config;
|
||||||
|
|
||||||
|
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
|
||||||
|
import org.springframework.amqp.support.converter.MessageConverter;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Lenovo
|
||||||
|
* @Description rabbitMQ消息转换器配置
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class RabbitMQMessageConverterConfig {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 消息转换配置
|
||||||
|
*
|
||||||
|
* @return 消息转换器
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public MessageConverter jsonMessageConverter() {
|
||||||
|
return new Jackson2JsonMessageConverter();
|
||||||
|
}
|
||||||
|
}
|
|
@ -22,9 +22,13 @@ public class VehicleConnection {
|
||||||
/**
|
/**
|
||||||
* 用户名
|
* 用户名
|
||||||
*/
|
*/
|
||||||
private String userName;
|
private String username;
|
||||||
/**
|
/**
|
||||||
* 随机数
|
* 随机数
|
||||||
*/
|
*/
|
||||||
private String nonce;
|
private String nonce;
|
||||||
|
/**
|
||||||
|
* 密码
|
||||||
|
*/
|
||||||
|
private String password;
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,7 +28,7 @@ public class VehicleConnectionReq {
|
||||||
/**
|
/**
|
||||||
* 用户名
|
* 用户名
|
||||||
*/
|
*/
|
||||||
private String userName;
|
private String username;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 随机数
|
* 随机数
|
||||||
|
|
|
@ -126,8 +126,8 @@ public class DelInstance implements ApplicationListener<ContextClosedEvent> {
|
||||||
try{
|
try{
|
||||||
log.info("=======>删除实例");
|
log.info("=======>删除实例");
|
||||||
delInstance();
|
delInstance();
|
||||||
redisTemplate.delete("instanceIds");
|
// redisTemplate.delete("instanceIds");
|
||||||
redisTemplate.delete("instanceList");
|
// redisTemplate.delete("instanceList");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,7 +61,7 @@ public class Sample implements ApplicationRunner {
|
||||||
// 创建创建实例请求对象并设置参数
|
// 创建创建实例请求对象并设置参数
|
||||||
RunInstancesRequest runInstancesRequest = new RunInstancesRequest()
|
RunInstancesRequest runInstancesRequest = new RunInstancesRequest()
|
||||||
.setRegionId("cn-shanghai") // 设置地域ID
|
.setRegionId("cn-shanghai") // 设置地域ID
|
||||||
.setImageId("m-uf63dnbv4od71jlezdne")// 设置镜像ID
|
.setImageId("m-uf66taa8r57ky0pg3e7s")// 设置镜像ID
|
||||||
.setInstanceType("ecs.e-c1m1.large")// 设置实例类型
|
.setInstanceType("ecs.e-c1m1.large")// 设置实例类型
|
||||||
.setSecurityGroupId("sg-uf6hyictocodexptlgiv")// 设置安全组ID
|
.setSecurityGroupId("sg-uf6hyictocodexptlgiv")// 设置安全组ID
|
||||||
.setVSwitchId("vsw-uf6ags5luz17qd6ckn2tb")// 设置虚拟交换机ID
|
.setVSwitchId("vsw-uf6ags5luz17qd6ckn2tb")// 设置虚拟交换机ID
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
package com.muyu.cloud.vehicle.gateway.aliyun.mapper;
|
package com.muyu.cloud.vehicle.gateway.aliyun.mapper;
|
||||||
|
|
||||||
|
import com.muyu.cloud.vehicle.gateway.aliyun.domain.VehicleConnection;
|
||||||
import com.muyu.cloud.vehicle.gateway.aliyun.domain.req.VehicleConnectionReq;
|
import com.muyu.cloud.vehicle.gateway.aliyun.domain.req.VehicleConnectionReq;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
@Mapper
|
@Mapper
|
||||||
public interface VehicleConnectionMapper {
|
public interface VehicleConnectionMapper {
|
||||||
void addConnect(VehicleConnectionReq vehicleConnectionReq);
|
void addConnect(VehicleConnection vehicleConnection);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
package com.muyu.cloud.vehicle.gateway.aliyun.service.impl;
|
package com.muyu.cloud.vehicle.gateway.aliyun.service.impl;
|
||||||
|
|
||||||
|
import com.muyu.cloud.vehicle.gateway.aliyun.domain.VehicleConnection;
|
||||||
import com.muyu.cloud.vehicle.gateway.aliyun.domain.req.VehicleConnectionReq;
|
import com.muyu.cloud.vehicle.gateway.aliyun.domain.req.VehicleConnectionReq;
|
||||||
import com.muyu.cloud.vehicle.gateway.aliyun.mapper.VehicleConnectionMapper;
|
import com.muyu.cloud.vehicle.gateway.aliyun.mapper.VehicleConnectionMapper;
|
||||||
import com.muyu.cloud.vehicle.gateway.aliyun.service.VehicleConnectionService;
|
import com.muyu.cloud.vehicle.gateway.aliyun.service.VehicleConnectionService;
|
||||||
import lombok.extern.log4j.Log4j2;
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@ -11,11 +13,23 @@ import org.springframework.stereotype.Service;
|
||||||
@Service
|
@Service
|
||||||
public class VehicleConnectionServiceImpl implements VehicleConnectionService {
|
public class VehicleConnectionServiceImpl implements VehicleConnectionService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RabbitTemplate rabbitTemplate;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private VehicleConnectionMapper vehicleConnectionMapper;
|
private VehicleConnectionMapper vehicleConnectionMapper;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void getConnect(VehicleConnectionReq vehicleConnectionReq) {
|
public void getConnect(VehicleConnectionReq vehicleConnectionReq) {
|
||||||
log.info("车辆连接请求:{}",vehicleConnectionReq.toString());
|
log.info("车辆连接请求:{}",vehicleConnectionReq.toString());
|
||||||
vehicleConnectionMapper.addConnect(vehicleConnectionReq);
|
|
||||||
|
//发送消息
|
||||||
|
rabbitTemplate.convertAndSend("exchange_topics_inform","",vehicleConnectionReq.getVehicleVin());
|
||||||
|
|
||||||
|
VehicleConnection vehicleConnection = new VehicleConnection();
|
||||||
|
vehicleConnection.setVehicleVin(vehicleConnectionReq.getVehicleVin());
|
||||||
|
vehicleConnection.setUsername(vehicleConnectionReq.getUsername());
|
||||||
|
vehicleConnection.setPassword(vehicleConnectionReq.getVehicleVin()+vehicleConnectionReq.getTimestamp()+vehicleConnectionReq.getNonce());
|
||||||
|
vehicleConnectionMapper.addConnect(vehicleConnection);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,8 +7,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
|
|
||||||
<insert id="addConnect">
|
<insert id="addConnect">
|
||||||
insert into car_one_click_operation
|
insert into car_one_click_operation
|
||||||
(vehicle_vin,user_name,nonce,timestamp)
|
(vehicle_vin,user_name,password)
|
||||||
values
|
values
|
||||||
(#{vehicleVin},#{userName},#{nonce},#{timestamp})
|
(#{vehicleVin},#{username},#{password})
|
||||||
</insert>
|
</insert>
|
||||||
</mapper>
|
</mapper>
|
||||||
|
|
Loading…
Reference in New Issue