test:(改代码)

dev
zhang xu 2024-06-11 22:29:28 +08:00
parent e3da3d1b4b
commit 9db5a3bbc4
13 changed files with 369 additions and 7 deletions

View File

@ -23,5 +23,11 @@
<groupId>com.muyu</groupId>
<artifactId>muyu-common-core</artifactId>
</dependency>
<dependency>
<groupId>com.muyu</groupId>
<artifactId>muyu-networking-common</artifactId>
<version>3.6.3</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

View File

@ -25,6 +25,70 @@ public class AddService extends BaseEntity {
*
* */
private Integer addValue;
/**
*
* */
private String addName;
/**
*
* */
private Double addPrice;
/**
*
* */
private Integer addStatus;
/**
*
* */
private String addDesc;
/**
*
* */
private String addImg;
/**
*
* */
private Integer addType;
/**
*
* */
private Integer addTypeId;
/**
*
* */
private String addTypeName;
/**
*
* */
private String addTypeDesc;
/**
*
* */
private String addTypeImg;
/**
*
* */
private Integer addTypeStatus;
/**
*
* */
private Integer addTypeSort;
/**
*
* */
private Integer addTypeSort1;
/**
*
* */
private Integer addTypeSort2;
/**
*
* */
private Integer addTypeSort3;
/**
*
* */
private Integer addTypeSort4;

View File

@ -1,8 +1,11 @@
package com.muyu.networking.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.muyu.domain.Enterprise;
import com.muyu.domain.datasources.EntInfo;
import java.util.List;
/**
* @ClassDescription:
* @JdkVersion: 17
@ -10,4 +13,8 @@ import com.muyu.domain.datasources.EntInfo;
* @Created: 2024/6/10 20:55
*/
public interface EntInfoService extends IService<EntInfo> {
public List<EntInfo> selectEnterpriseList(EntInfo entInfo);
}

View File

@ -1,13 +1,14 @@
package com.muyu.networking.service.impl;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.domain.Enterprise;
import com.muyu.domain.datasources.EntInfo;
import com.muyu.networking.mapper.EntInfoMapper;
import com.muyu.networking.mapper.EnterpriseMapper;
import com.muyu.networking.service.EntInfoService;
import com.muyu.networking.service.EnterpriseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.AccessType;
import java.util.List;
/**
* @ClassDescription:
@ -18,4 +19,13 @@ import com.muyu.networking.service.EnterpriseService;
public class EntInfoServiceImpl extends ServiceImpl<EntInfoMapper, EntInfo> implements EntInfoService {
@Autowired
private EntInfoMapper entInfoMapper;
@Override
public List<EntInfo> selectEnterpriseList(EntInfo entInfo) {
LambdaQueryWrapper<EntInfo> entInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
return entInfoMapper.selectList(entInfoLambdaQueryWrapper);
}
}

View File

@ -14,6 +14,7 @@
<modules>
<module>muyu-networking-common</module>
<module>muyu-networking-service</module>
</modules>
<properties>
@ -23,6 +24,14 @@
</properties>
<dependencies>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.27</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.muyu</groupId>
<artifactId>muyu</artifactId>
<version>3.6.3</version>
<relativePath>../../../pom.xml</relativePath>
</parent>
<artifactId>muyu-vehicle-common</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

View File

@ -26,6 +26,12 @@
<version>3.6.3</version>
</dependency>
<!--rabbitMQ-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>

View File

@ -0,0 +1,60 @@
package com.muyu.vehicle.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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.beans.factory.annotation.Value;
/**
* @ClassDescription:
* @JdkVersion: 17
* @Author: zhangxu
* @Created: 2024/6/10 20:29
*/
@Configuration
public class RabbitAdminConfig {
@Value("${spring.rabbitmq.host}")
private String host;
@Value("${spring.rabbitmq.username}")
private String username;
@Value("${spring.rabbitmq.password}")
private String password;
@Value("${spring.rabbitmq.virtualhost}")
private String virtualhost;
/**
* RabbitMQ
* @return
*/
@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;
}
/**
* RabbitAdmin
* @param connectionFactory
* @return
*/
@Bean
public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
rabbitAdmin.setAutoStartup(true);
return rabbitAdmin;
}
}

View File

@ -0,0 +1,21 @@
package com.muyu.vehicle.config;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @ClassDescription:
* @JdkVersion: 17
* @Author: zhangxu
* @Created: 2024/6/10 20:27
*/
@Configuration
public class RabbitmqConfig {
// 消息转换配置
@Bean
public Jackson2JsonMessageConverter jsonMessageConverter() {
return new Jackson2JsonMessageConverter();
}
}

View File

@ -0,0 +1,36 @@
package com.muyu.vehicle.config;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.PostConstruct;
/**
* @ClassDescription:
* @JdkVersion: 17
* @Author: zhangxu
* @Created: 2024/6/10 20:27
*/
public class ReturnCallbackConfig implements RabbitTemplate.ReturnsCallback{
@Autowired
private RabbitTemplate rabbitTemplate;
@PostConstruct // @PostContruct是spring框架的注解在⽅法上加该注解会在项⽬启动的时候执⾏该⽅法也可以理解为在spring容器初始化的时候执
public void init() {
rabbitTemplate.setReturnsCallback(this);
}
/**
*
* @param returnedMessage the returned message and metadata.
*/
@Override
public void returnedMessage(ReturnedMessage returnedMessage) {
System.out.println("消息" + returnedMessage.getMessage().toString() + "被交换机" + returnedMessage.getExchange() + "回退!"
+ "退回原因为:" + returnedMessage.getReplyText());
// 回退了所有的信息,可做补偿机制 记录到 数据库
}
}

View File

@ -0,0 +1,121 @@
package com.muyu.vehicle.myDatasource;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.fastjson.JSON;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.SpringUtils;
import com.muyu.common.system.remote.RemoteUserService;
import com.muyu.domain.datasources.EntInfo;
import com.muyu.vehicle.myDatasource.domain.DataSourceInfo;
import com.muyu.vehicle.myDatasource.domain.EnterpriseInfo;
import com.muyu.vehicle.myDatasource.factory.DruidDataSourceFactory;
import com.muyu.vehicle.myDatasource.rule.DynamicDataSource;
import com.rabbitmq.client.Channel;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @ClassDescription:
* @JdkVersion: 17
* @Author: zhangxu
* @Created: 2024/6/10 20:04
*/
@Log4j2
@Component
public class ManyDataSource {
@Autowired
private RemoteUserService remoteEntInfoService;
@Autowired
private RedisTemplate<String, String> redisTemplate;
@RabbitListener(queuesToDeclare = {@Queue(name = "RoutingKey")})
public void smsConfig(String msg, Message message, Channel channel) {
//获取消息的ID
String messageId = message.getMessageProperties().getMessageId();
try {
//添加消息id到redis set集合中 添加成功返回1 表示未消费 添加失败返回0 表示已消费
Long count = redisTemplate.opsForSet().add("messageId", messageId);
//添加成功 正常消费信息
if (count == 1) {
log.info("开始消费");
DruidDataSourceFactory druidDataSourceFactory = SpringUtils.getBean(DruidDataSourceFactory.class);
DynamicDataSource dynamicDataSource = SpringUtils.getBean(DynamicDataSource.class);
EntInfo entinfo = JSON.parseObject(msg, EntInfo.class);
DataSourceInfo dataSourceInfo = DataSourceInfo.hostAndPortBuild(entinfo.getEntCode(), entinfo.getIp(), Math.toIntExact(entinfo.getPort()));
DruidDataSource druidDataSource = druidDataSourceFactory.create(dataSourceInfo);
dynamicDataSource.put(dataSourceInfo.getKey(), druidDataSource);
//确认消费
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
log.info("消费成功");
}
} catch (Exception e) {
//删除队列ID
log.info("消费重复");
try {
//回退消息
channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
log.info("消费失败");
} catch (IOException ex) {
//回退失败
log.info("消费异常");
}
}
}
private List<EnterpriseInfo> dataSourceInfoList() {
Result<List<EntInfo>> entInfoResult = remoteEntInfoService.list();
List<EntInfo> entInfoList = entInfoResult.getData();
List<EnterpriseInfo> list = new ArrayList<>();
entInfoList.forEach(entInfo -> {
list.add(
EnterpriseInfo.builder()
.entCode(entInfo.getEntCode())
.ip(entInfo.getIp())
.port(String.valueOf(entInfo.getPort()))
.build()
);
});
return list;
}
@Bean
@Primary
public DynamicDataSource dynamicDataSource(DruidDataSourceFactory druidDataSourceFactory) {
Map<Object, Object> dataSourceMap = new HashMap<>();
dataSourceInfoList()
.stream()
.map(enterpriseInfo -> DataSourceInfo.hostAndPortBuild(enterpriseInfo.getEntCode(), enterpriseInfo.getIp(), Integer.valueOf(enterpriseInfo.getPort())))
.forEach(dataSourceInfo -> {
dataSourceMap.put(dataSourceInfo.getKey(), druidDataSourceFactory.create(dataSourceInfo));
});
//设置动态数据源
DynamicDataSource dynamicDataSource = new DynamicDataSource();
// dynamicDataSource.setDefaultTargetDataSource(masterDataSource());
dynamicDataSource.setTargetDataSources(dataSourceMap);
//将数据源信息备份在defineTargetDataSources中
dynamicDataSource.setDefaultTargetDataSource(dataSourceMap);
return dynamicDataSource;
}
}

View File

@ -8,11 +8,11 @@ package com.muyu.vehicle.myDatasource.constants;
*/
public class DatasourceConstant {
public final static String DATASOURCE_URL = "jdbc:mysql://{}:{}/car_networking?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
public final static String DATASOURCE_URL = "jdbc:mysql://{}:{}/networking?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
public final static String USER_NAME = "root";
public final static String PASSWORD = "wxl@123";
public final static String PASSWORD = "sal75-z";
}

View File

@ -1,5 +1,6 @@
package com.muyu.vehicle.myDatasource.holder;
import cn.hutool.core.lang.Assert;
import lombok.extern.log4j.Log4j2;
/**