虎虎虎虎
parent
c884fc9a19
commit
c3aee00eff
|
@ -33,7 +33,7 @@ public class RabbitMQConfig implements RabbitTemplate.ConfirmCallback, RabbitTem
|
||||||
public String exchangeName;
|
public String exchangeName;
|
||||||
|
|
||||||
//路由键
|
//路由键
|
||||||
@Value(("${mq.routingKey}"))
|
@Value("${mq.routingKey}")
|
||||||
public String routingKey;
|
public String routingKey;
|
||||||
|
|
||||||
private RabbitTemplate rabbitTemplate;
|
private RabbitTemplate rabbitTemplate;
|
||||||
|
@ -69,7 +69,7 @@ public class RabbitMQConfig implements RabbitTemplate.ConfirmCallback, RabbitTem
|
||||||
* @Param: []
|
* @Param: []
|
||||||
* @Return: 返回一个配置好的DirectExchange实例。
|
* @Return: 返回一个配置好的DirectExchange实例。
|
||||||
**/
|
**/
|
||||||
@Bean("exchange")
|
@Bean
|
||||||
public DirectExchange directExchange() {
|
public DirectExchange directExchange() {
|
||||||
return new DirectExchange(exchangeName);
|
return new DirectExchange(exchangeName);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,15 +1,13 @@
|
||||||
package com.couplet.mq.controller;
|
package com.couplet.mq.controller;
|
||||||
|
|
||||||
import com.couplet.common.core.utils.uuid.IdUtils;
|
import com.couplet.common.core.utils.uuid.IdUtils;
|
||||||
import com.couplet.mq.config.RabbitMQConfig;
|
import com.couplet.mq.domain.User;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.amqp.rabbit.connection.CorrelationData;
|
import org.springframework.amqp.rabbit.connection.CorrelationData;
|
||||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ProjectName: five-groups-couplet
|
* @ProjectName: five-groups-couplet
|
||||||
|
@ -21,17 +19,37 @@ import org.springframework.web.bind.annotation.RestController;
|
||||||
@RequestMapping("/mq")
|
@RequestMapping("/mq")
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class MqController {
|
public class MqController {
|
||||||
|
// 通过注入的方式获取队列名、交换机名和路由键
|
||||||
|
//队列名
|
||||||
|
@Value("${mq.queueName}")
|
||||||
|
public String queueName;
|
||||||
|
|
||||||
|
//交换机
|
||||||
|
@Value("${mq.exchangeName}")
|
||||||
|
public String exchangeName;
|
||||||
|
|
||||||
|
//路由键
|
||||||
|
@Value("${mq.routingKey}")
|
||||||
|
public String routingKey;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private RabbitTemplate rabbitTemplate;
|
private RabbitTemplate rabbitTemplate;
|
||||||
|
|
||||||
@GetMapping("/receive/{data}")
|
/*
|
||||||
public void receive(@PathVariable("data") String data) {
|
* @Author: LiuYunHu
|
||||||
//创建配置类对象,用于获取配置值
|
* @Date: 2024/4/1 19:58
|
||||||
RabbitMQConfig config = new RabbitMQConfig();
|
* @Description: 消息发送者
|
||||||
|
* @Param: [data]
|
||||||
rabbitTemplate.convertAndSend(config.exchangeName, config.routingKey, data , message -> {
|
* @Return: void
|
||||||
|
**/
|
||||||
|
@PostMapping("/sout")
|
||||||
|
//接收json字符串
|
||||||
|
public void sout(@RequestBody User param) {
|
||||||
|
rabbitTemplate.convertAndSend(exchangeName, routingKey, param, message -> {
|
||||||
message.getMessageProperties().setMessageId(IdUtils.randomUUID());
|
message.getMessageProperties().setMessageId(IdUtils.randomUUID());
|
||||||
return message;
|
return message;
|
||||||
}, new CorrelationData(IdUtils.randomUUID()));
|
}, new CorrelationData(IdUtils.randomUUID())
|
||||||
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +0,0 @@
|
||||||
package com.couplet.mq.domain;
|
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @ProjectName: five-groups-couplet
|
|
||||||
* @Author: LiuYunHu
|
|
||||||
* @CreateTime: 2024/3/29
|
|
||||||
* @Description: 测试 参数类
|
|
||||||
*/
|
|
||||||
|
|
||||||
public class Test implements Serializable {
|
|
||||||
public String data;
|
|
||||||
}
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
package com.couplet.mq.domain;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ProjectName: five-groups-couplet
|
||||||
|
* @Author: LiuYunHu
|
||||||
|
* @CreateTime: 2024/4/1
|
||||||
|
* @Description:
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@ToString
|
||||||
|
public class User {
|
||||||
|
private String name;
|
||||||
|
private Integer age;
|
||||||
|
private String sex;
|
||||||
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
package com.couplet.mq.service;
|
package com.couplet.mq.service;
|
||||||
|
|
||||||
|
import com.couplet.mq.domain.User;
|
||||||
import com.rabbitmq.client.Channel;
|
import com.rabbitmq.client.Channel;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.amqp.core.Message;
|
import org.springframework.amqp.core.Message;
|
||||||
|
@ -18,53 +19,146 @@ import java.util.concurrent.TimeUnit;
|
||||||
* @CreateTime: 2024/3/28
|
* @CreateTime: 2024/3/28
|
||||||
* @Description: MQ消费者类
|
* @Description: MQ消费者类
|
||||||
*/
|
*/
|
||||||
@RabbitListener(queues = "${mq.queueName}")
|
|
||||||
@Component
|
@Component
|
||||||
@Slf4j
|
@Slf4j
|
||||||
|
@SuppressWarnings("all")
|
||||||
|
@RabbitListener(queues = "${mq.queueName}")
|
||||||
public class Consumer {
|
public class Consumer {
|
||||||
@Autowired
|
@Autowired
|
||||||
private StringRedisTemplate redisTemplate;
|
private StringRedisTemplate redis;
|
||||||
|
|
||||||
|
/* 线程池执行
|
||||||
|
|
||||||
|
//创建一个定长线程池
|
||||||
|
private final Executor executor = Executors.newFixedThreadPool(5);
|
||||||
|
|
||||||
|
@Async
|
||||||
@RabbitHandler
|
@RabbitHandler
|
||||||
public void receive(String data, Channel channel, Message message) throws IOException {
|
public void process(User param, Channel channel, Message message) {
|
||||||
log.info("消费者接受到数据:{}", data);
|
executor.execute(() -> {
|
||||||
|
try {
|
||||||
|
handleMessage(param, channel, message);
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("处理消息失败:{}", e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
//处理信息的方法
|
||||||
|
private void handleMessage(User param, Channel channel, Message message) throws IOException {
|
||||||
|
log.info("消费者收到消息为:{},{}" + param, message.getMessageProperties().getDeliveryTag());
|
||||||
|
|
||||||
//获取信息的标记
|
|
||||||
long deliveryTag = message.getMessageProperties().getDeliveryTag();
|
long deliveryTag = message.getMessageProperties().getDeliveryTag();
|
||||||
|
|
||||||
//获取到消息的id
|
|
||||||
String messageId = message.getMessageProperties().getMessageId();
|
String messageId = message.getMessageProperties().getMessageId();
|
||||||
|
|
||||||
Long add = redisTemplate.opsForSet().add("set:" + messageId, "set:" + messageId);
|
if (!redis.hasKey("value:" + messageId)) {
|
||||||
|
redis.opsForValue().set("value:" + messageId, "" + deliveryTag, 5, TimeUnit.MINUTES);
|
||||||
if (!redisTemplate.hasKey("value:" + messageId)) {
|
|
||||||
redisTemplate.opsForValue().set("value:" + messageId, String.valueOf(deliveryTag), 10, TimeUnit.MINUTES);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 1 添加成功新数据 0已有重复值,不允许再添加
|
||||||
|
Long add = redis.opsForSet().add("set:" + messageId, "set:" + messageId);
|
||||||
|
//过期时间
|
||||||
|
redis.expire("set:" + messageId, 5, TimeUnit.MINUTES);
|
||||||
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (add == 1) {
|
if (add == 1) {
|
||||||
log.info("---------------消费者开始消费---------------");
|
//第一次 消费
|
||||||
|
System.out.println("*****************************");
|
||||||
|
System.out.println("消费者收到消息:" + param);
|
||||||
|
System.out.println("*****************************");
|
||||||
|
log.info("消费结束");
|
||||||
|
|
||||||
System.out.println(data);
|
channel.basicAck(deliveryTag, false);
|
||||||
|
|
||||||
log.info("---------------消费者结束消费---------------");
|
} else {
|
||||||
}else {
|
//重复消费
|
||||||
log.error("重复消费!");
|
log.error("重复消费");
|
||||||
channel.basicReject(deliveryTag, false);
|
channel.basicReject(deliveryTag, false);
|
||||||
|
|
||||||
|
//删除缓存
|
||||||
|
redis.opsForSet().remove("set:" + messageId, "set:" + messageId);
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
|
||||||
// throw new RuntimeException(e);
|
|
||||||
String s = redisTemplate.opsForValue().get("value:" + messageId);
|
} catch (Exception e) {
|
||||||
|
log.error("消息没有成功消费!");
|
||||||
|
|
||||||
|
String s = redis.opsForValue().get("value:" + messageId);
|
||||||
|
|
||||||
long oldTag = Long.parseLong(s);
|
long oldTag = Long.parseLong(s);
|
||||||
|
|
||||||
if ((oldTag + 2) != deliveryTag) {
|
if (deliveryTag == (oldTag + 2)) {
|
||||||
log.info("重新入队!");
|
log.error("确实消费不了,不入队了!");
|
||||||
channel.basicNack(deliveryTag, false, true);
|
|
||||||
}else {
|
|
||||||
log.error("三次无法消费,不再入队!");
|
|
||||||
channel.basicNack(deliveryTag, false, false);
|
channel.basicNack(deliveryTag, false, false);
|
||||||
|
} else {
|
||||||
|
log.info("消息消费失败,重新入队");
|
||||||
|
channel.basicNack(deliveryTag, false, true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
**/
|
||||||
|
|
||||||
|
@RabbitHandler
|
||||||
|
public void process(User param, Channel channel, Message message) throws IOException {
|
||||||
|
log.info("消费者收到消息为:{},{}" + param, message.getMessageProperties().getDeliveryTag());
|
||||||
|
|
||||||
|
long deliveryTag = message.getMessageProperties().getDeliveryTag();
|
||||||
|
String messageId = message.getMessageProperties().getMessageId();
|
||||||
|
|
||||||
|
if (!redis.hasKey("value:" + messageId)) {
|
||||||
|
redis.opsForValue().set("value:" + messageId, "" + deliveryTag, 5, TimeUnit.MINUTES);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1 添加成功新数据 0已有重复值,不允许再添加
|
||||||
|
Long add = redis.opsForSet().add("set:" + messageId, "set:" + messageId);
|
||||||
|
//过期时间
|
||||||
|
redis.expire("set:" + messageId, 5, TimeUnit.MINUTES);
|
||||||
|
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (add == 1) {
|
||||||
|
//第一次 消费
|
||||||
|
System.out.println("*****************************");
|
||||||
|
System.out.println("消费者收到消息:" + param);
|
||||||
|
System.out.println("*****************************");
|
||||||
|
log.info("消费结束");
|
||||||
|
|
||||||
|
//确认消费
|
||||||
|
channel.basicAck(deliveryTag, false);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
//重复消费
|
||||||
|
log.error("重复消费");
|
||||||
|
//拒绝消费
|
||||||
|
channel.basicReject(deliveryTag, false);
|
||||||
|
|
||||||
|
//删除缓存
|
||||||
|
redis.opsForSet().remove("set:" + messageId, "set:" + messageId);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("消息没有成功消费!");
|
||||||
|
|
||||||
|
String s = redis.opsForValue().get("value:" + messageId);
|
||||||
|
|
||||||
|
long oldTag = Long.parseLong(s);
|
||||||
|
|
||||||
|
if (deliveryTag == (oldTag + 2)) {
|
||||||
|
log.error("确实消费不了,不入队了!");
|
||||||
|
|
||||||
|
|
||||||
|
//拒绝消费
|
||||||
|
channel.basicNack(deliveryTag, false, false);
|
||||||
|
} else {
|
||||||
|
log.info("消息消费失败,重新入队");
|
||||||
|
//重新入队
|
||||||
|
channel.basicNack(deliveryTag, false, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
com.couplet.mq.config.RabbitMQConfig
|
|
@ -31,16 +31,6 @@ logging:
|
||||||
level:
|
level:
|
||||||
com.couplet.system.mapper: DEBUG
|
com.couplet.system.mapper: DEBUG
|
||||||
|
|
||||||
# 订阅端配置
|
|
||||||
mqtt:
|
|
||||||
server:
|
|
||||||
broker: tcp://115.159.47.13:1883
|
|
||||||
username:
|
|
||||||
password:
|
|
||||||
clientid: mqttx
|
|
||||||
qos: 0
|
|
||||||
topic: test
|
|
||||||
|
|
||||||
# RabbitMQ配置
|
# RabbitMQ配置
|
||||||
mq:
|
mq:
|
||||||
queueName: queue
|
queueName: queue
|
||||||
|
|
|
@ -0,0 +1,121 @@
|
||||||
|
<?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">
|
||||||
|
<parent>
|
||||||
|
<groupId>com.couplet</groupId>
|
||||||
|
<artifactId>couplet-modules</artifactId>
|
||||||
|
<version>3.6.3</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>couplet-modules-online</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>-->
|
||||||
|
|
||||||
|
<description>
|
||||||
|
couplet-modules-online车辆上线的模块
|
||||||
|
</description>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
<!-- SpringCloud Alibaba Nacos -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- SpringCloud Alibaba Nacos Config -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- SpringCloud Alibaba Sentinel -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- SpringBoot Actuator -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Swagger UI -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.springfox</groupId>
|
||||||
|
<artifactId>springfox-swagger-ui</artifactId>
|
||||||
|
<version>${swagger.fox.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Mysql Connector -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-j</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- MuYu Common DataSource -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.couplet</groupId>
|
||||||
|
<artifactId>couplet-common-datasource</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- MuYu Common DataScope -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.couplet</groupId>
|
||||||
|
<artifactId>couplet-common-datascope</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- MuYu Common Log -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.couplet</groupId>
|
||||||
|
<artifactId>couplet-common-log</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- MuYu Common Swagger -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.couplet</groupId>
|
||||||
|
<artifactId>couplet-common-swagger</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- mqttx依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.paho</groupId>
|
||||||
|
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
|
||||||
|
<version>1.2.5</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>${project.artifactId}</finalName>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<goals>
|
||||||
|
<goal>repackage</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<!-- 加入maven deploy插件,当在deploy时,忽略些model-->
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-deploy-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<skip>true</skip>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,25 @@
|
||||||
|
package com.couplet.online;
|
||||||
|
|
||||||
|
import com.couplet.common.security.annotation.EnableCustomConfig;
|
||||||
|
import com.couplet.common.security.annotation.EnableMyFeignClients;
|
||||||
|
import com.couplet.common.swagger.annotation.EnableCustomSwagger2;
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ProjectName: Default (Template) Project
|
||||||
|
* @Author: LiuYunHu
|
||||||
|
* @CreateTime: 2024/4/1
|
||||||
|
* @Description: 车辆上线的模块启动类
|
||||||
|
*/
|
||||||
|
|
||||||
|
@EnableCustomConfig
|
||||||
|
@EnableCustomSwagger2
|
||||||
|
@EnableMyFeignClients
|
||||||
|
@SpringBootApplication
|
||||||
|
public class OnlineApplication {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(OnlineApplication.class);
|
||||||
|
System.out.println("车辆上线模块启动成功");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
package com.couplet.online.controller;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ProjectName: five-groups-couplet
|
||||||
|
* @Author: LiuYunHu
|
||||||
|
* @CreateTime: 2024/4/1
|
||||||
|
* @Description: 车辆上线控制器
|
||||||
|
*/
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/online")
|
||||||
|
public class OnlineController {
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
package com.couplet.online.mapper;
|
||||||
|
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ProjectName: five-groups-couplet
|
||||||
|
* @Author: LiuYunHu
|
||||||
|
* @CreateTime: 2024/4/1
|
||||||
|
* @Description: 车辆上线mapper
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
@Component
|
||||||
|
public interface OnlineMapper {
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
package com.couplet.online.service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ProjectName: five-groups-couplet
|
||||||
|
* @Author: LiuYunHu
|
||||||
|
* @CreateTime: 2024/4/1
|
||||||
|
* @Description:
|
||||||
|
*/
|
||||||
|
|
||||||
|
public interface OnlineService {
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
package com.couplet.online.service.impl;
|
||||||
|
|
||||||
|
import com.couplet.online.service.OnlineService;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ProjectName: five-groups-couplet
|
||||||
|
* @Author: LiuYunHu
|
||||||
|
* @CreateTime: 2024/4/1
|
||||||
|
* @Description: 车辆上线service层
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
@Slf4j
|
||||||
|
public class OnlineServiceImpl implements OnlineService {
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package com.couplet.mq.service;
|
package com.couplet.online.utils;
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.eclipse.paho.client.mqttv3.*;
|
import org.eclipse.paho.client.mqttv3.*;
|
||||||
|
@ -11,98 +11,101 @@ import javax.annotation.PostConstruct;
|
||||||
/**
|
/**
|
||||||
* @ProjectName: five-groups-couplet
|
* @ProjectName: five-groups-couplet
|
||||||
* @Author: LiuYunHu
|
* @Author: LiuYunHu
|
||||||
* @CreateTime: 2024/3/28
|
* @CreateTime: 2024/4/1
|
||||||
* @Description: mqtt客户端
|
* @Description: mqtt监听者
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class MqttListen {
|
public class MqttMonitor {
|
||||||
/*
|
/*
|
||||||
* 路径
|
* 路径
|
||||||
* */
|
* */
|
||||||
@Value("${mqtt.server.broker}")
|
@Value("${mqtt.server.broker}")
|
||||||
private String BROKER;
|
private String broker;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* 主题
|
* 主题
|
||||||
* */
|
* */
|
||||||
@Value("${mqtt.server.topic}")
|
@Value("${mqtt.server.topic}")
|
||||||
private String TOPIC;
|
private String topic;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* 客户端id
|
* 客户端id
|
||||||
* */
|
* */
|
||||||
@Value("${mqtt.server.clientid}")
|
@Value("${mqtt.server.clientId}")
|
||||||
private String CLIENTID;
|
private String clientId;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* 用户名
|
* 用户名
|
||||||
* */
|
* */
|
||||||
@Value("${mqtt.server.username}")
|
@Value("${mqtt.server.username}")
|
||||||
private String USERNAME;
|
private String userName;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* 密码
|
* 密码
|
||||||
* */
|
* */
|
||||||
@Value("${mqtt.server.password}")
|
@Value("${mqtt.server.password}")
|
||||||
private String PASSWORD;
|
private String password;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* qos
|
* 遗嘱消息qos
|
||||||
* */
|
* */
|
||||||
@Value("${mqtt.server.qos}")
|
@Value("${mqtt.server.qos}")
|
||||||
private Integer QOS;
|
private Integer qos;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//随项目启动而执行这个方法
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void connect() {
|
public void connect() {
|
||||||
System.out.println("监听者启动");
|
log.info("mqtt监听者启动");
|
||||||
try {
|
try {
|
||||||
|
MqttClient client = new MqttClient(broker, clientId, new MemoryPersistence());
|
||||||
|
|
||||||
MqttClient client = new MqttClient(BROKER, CLIENTID, new MemoryPersistence());
|
|
||||||
MqttConnectOptions options = new MqttConnectOptions();
|
MqttConnectOptions options = new MqttConnectOptions();
|
||||||
options.setUserName(USERNAME);
|
options.setUserName(userName);
|
||||||
options.setPassword(PASSWORD.toCharArray());
|
options.setPassword(password.toCharArray());
|
||||||
|
|
||||||
//连接超时
|
//连接超时
|
||||||
options.setConnectionTimeout(60);
|
options.setConnectionTimeout(10);
|
||||||
|
|
||||||
//心跳
|
//心跳时间
|
||||||
options.setKeepAliveInterval(60);
|
options.setKeepAliveInterval(60);
|
||||||
|
|
||||||
log.info("连接到:"+BROKER);
|
log.info("mqtt监听者启动成功,连接到:{}", broker);
|
||||||
|
|
||||||
client.connect(options);
|
client.connect(options);
|
||||||
log.info("连接成功");
|
log.info("连接成功!");
|
||||||
|
|
||||||
//设置回调
|
//设置回调
|
||||||
client.setCallback(new MqttCallback() {
|
client.setCallback(new MqttCallback() {
|
||||||
@Override
|
@Override
|
||||||
public void connectionLost(Throwable throwable) {
|
public void connectionLost(Throwable throwable) {
|
||||||
log.error("连接断开:" + throwable.getMessage());
|
log.error("连接丢失:{}", throwable.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void messageArrived(String topic, MqttMessage message) {
|
public void messageArrived(String topic, MqttMessage mqttMessage) {
|
||||||
log.info("消息到达");
|
log.info("消息已送达");
|
||||||
log.info("接收消息主题:" + topic);
|
log.info("接收消息主题:{}",topic);
|
||||||
log.info("接收消息Qos:" + message.getQos());
|
log.info("接收消息qos:{}", mqttMessage.getQos());
|
||||||
log.info("接收消息内容:" + new String(message.getPayload()));
|
|
||||||
|
|
||||||
|
log.info("接收消息内容:{}", new String(mqttMessage.getPayload()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void deliveryComplete(IMqttDeliveryToken token) {
|
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
|
||||||
log.info("消息发送成功----------" + token.isComplete());
|
log.info("消息发送成功!");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
client.subscribe(TOPIC, QOS);
|
|
||||||
|
client.subscribe(topic, qos);
|
||||||
|
|
||||||
|
|
||||||
} catch (MqttException e) {
|
} catch (MqttException e) {
|
||||||
|
log.error("mqtt监听者启动失败,{}", e.getMessage());
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
Spring Boot Version: ${spring-boot.version}
|
||||||
|
Spring Application Name: ${spring.application.name}
|
||||||
|
_______ _ _ _________ _ _______
|
||||||
|
( ___ )( ( /|( \ \__ __/( ( /|( ____ \
|
||||||
|
| ( ) || \ ( || ( ) ( | \ ( || ( \/
|
||||||
|
| | | || \ | || | | | | \ | || (__
|
||||||
|
| | | || (\ \) || | | | | (\ \) || __)
|
||||||
|
| | | || | \ || | | | | | \ || (
|
||||||
|
| (___) || ) \ || (____/\___) (___| ) \ || (____/\
|
||||||
|
(_______)|/ )_)(_______/\_______/|/ )_)(_______/
|
|
@ -0,0 +1,43 @@
|
||||||
|
# Tomcat
|
||||||
|
server:
|
||||||
|
port: 9616
|
||||||
|
|
||||||
|
# Spring
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
# 应用名称
|
||||||
|
name: couplet-online
|
||||||
|
profiles:
|
||||||
|
# 环境配置
|
||||||
|
active: dev
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
discovery:
|
||||||
|
# 服务注册地址
|
||||||
|
server-addr: 121.89.211.230:8848
|
||||||
|
namespace: 172469
|
||||||
|
config:
|
||||||
|
# 配置中心地址
|
||||||
|
server-addr: 121.89.211.230:8848
|
||||||
|
namespace: 172469
|
||||||
|
# 配置文件格式
|
||||||
|
file-extension: yml
|
||||||
|
# 共享配置
|
||||||
|
shared-configs:
|
||||||
|
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
||||||
|
main:
|
||||||
|
allow-bean-definition-overriding: true
|
||||||
|
logging:
|
||||||
|
level:
|
||||||
|
com.couplet.system.mapper: DEBUG
|
||||||
|
|
||||||
|
# 订阅端配置
|
||||||
|
mqtt:
|
||||||
|
server:
|
||||||
|
broker: tcp://115.159.47.13:1883
|
||||||
|
username:
|
||||||
|
password:
|
||||||
|
clientId: lyh
|
||||||
|
qos: 0
|
||||||
|
topic: test
|
||||||
|
|
|
@ -0,0 +1,74 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<configuration scan="true" scanPeriod="60 seconds" debug="false">
|
||||||
|
<!-- 日志存放路径 -->
|
||||||
|
<property name="log.path" value="logs/couplet-system"/>
|
||||||
|
<!-- 日志输出格式 -->
|
||||||
|
<property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n"/>
|
||||||
|
|
||||||
|
<!-- 控制台输出 -->
|
||||||
|
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>${log.pattern}</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<!-- 系统日志输出 -->
|
||||||
|
<appender name="file_info" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||||
|
<file>${log.path}/info.log</file>
|
||||||
|
<!-- 循环政策:基于时间创建日志文件 -->
|
||||||
|
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||||
|
<!-- 日志文件名格式 -->
|
||||||
|
<fileNamePattern>${log.path}/info.%d{yyyy-MM-dd}.log</fileNamePattern>
|
||||||
|
<!-- 日志最大的历史 60天 -->
|
||||||
|
<maxHistory>60</maxHistory>
|
||||||
|
</rollingPolicy>
|
||||||
|
<encoder>
|
||||||
|
<pattern>${log.pattern}</pattern>
|
||||||
|
</encoder>
|
||||||
|
<filter class="ch.qos.logback.classic.filter.LevelFilter">
|
||||||
|
<!-- 过滤的级别 -->
|
||||||
|
<level>INFO</level>
|
||||||
|
<!-- 匹配时的操作:接收(记录) -->
|
||||||
|
<onMatch>ACCEPT</onMatch>
|
||||||
|
<!-- 不匹配时的操作:拒绝(不记录) -->
|
||||||
|
<onMismatch>DENY</onMismatch>
|
||||||
|
</filter>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||||
|
<file>${log.path}/error.log</file>
|
||||||
|
<!-- 循环政策:基于时间创建日志文件 -->
|
||||||
|
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||||
|
<!-- 日志文件名格式 -->
|
||||||
|
<fileNamePattern>${log.path}/error.%d{yyyy-MM-dd}.log</fileNamePattern>
|
||||||
|
<!-- 日志最大的历史 60天 -->
|
||||||
|
<maxHistory>60</maxHistory>
|
||||||
|
</rollingPolicy>
|
||||||
|
<encoder>
|
||||||
|
<pattern>${log.pattern}</pattern>
|
||||||
|
</encoder>
|
||||||
|
<filter class="ch.qos.logback.classic.filter.LevelFilter">
|
||||||
|
<!-- 过滤的级别 -->
|
||||||
|
<level>ERROR</level>
|
||||||
|
<!-- 匹配时的操作:接收(记录) -->
|
||||||
|
<onMatch>ACCEPT</onMatch>
|
||||||
|
<!-- 不匹配时的操作:拒绝(不记录) -->
|
||||||
|
<onMismatch>DENY</onMismatch>
|
||||||
|
</filter>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<!-- 系统模块日志级别控制 -->
|
||||||
|
<logger name="com.couplet" level="info"/>
|
||||||
|
<!-- Spring日志级别控制 -->
|
||||||
|
<logger name="org.springframework" level="warn"/>
|
||||||
|
|
||||||
|
<root level="info">
|
||||||
|
<appender-ref ref="console"/>
|
||||||
|
</root>
|
||||||
|
|
||||||
|
<!--系统操作日志-->
|
||||||
|
<root level="info">
|
||||||
|
<appender-ref ref="file_info"/>
|
||||||
|
<appender-ref ref="file_error"/>
|
||||||
|
</root>
|
||||||
|
</configuration>
|
|
@ -0,0 +1,8 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<!DOCTYPE mapper
|
||||||
|
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
|
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
|
<mapper namespace="com.couplet.online.mapper.OnlineMapper">
|
||||||
|
|
||||||
|
|
||||||
|
</mapper>
|
|
@ -190,7 +190,11 @@ public class VehicleServiceImpl extends ServiceImpl<VehicleMapper, Vehicle> impl
|
||||||
//雪花算法生成随机数
|
//雪花算法生成随机数
|
||||||
SnowflakeIdGenerator idGenerator = new SnowflakeIdGenerator(1, 1);
|
SnowflakeIdGenerator idGenerator = new SnowflakeIdGenerator(1, 1);
|
||||||
long randomId = idGenerator.nextId();
|
long randomId = idGenerator.nextId();
|
||||||
String vin = "VIN" + randomId;
|
//转成字符串,便于切割
|
||||||
|
String vin = "" + randomId;
|
||||||
|
|
||||||
|
//切割,只留后17位
|
||||||
|
vin = vin.substring(vin.length() - 17);
|
||||||
|
|
||||||
|
|
||||||
//创建入参对象
|
//创建入参对象
|
||||||
|
|
|
@ -14,7 +14,20 @@ public class IdTest {
|
||||||
SnowflakeIdGenerator idGenerator = new SnowflakeIdGenerator(1, 1);
|
SnowflakeIdGenerator idGenerator = new SnowflakeIdGenerator(1, 1);
|
||||||
|
|
||||||
long l = idGenerator.nextId();
|
long l = idGenerator.nextId();
|
||||||
|
String s = "VIN" + l;
|
||||||
System.out.println(l);
|
System.out.println(l);
|
||||||
|
System.out.println(s);
|
||||||
|
System.out.println("剪切前长度:" + s.length());
|
||||||
|
|
||||||
|
|
||||||
|
String last17 = s.substring(s.length() - 17);
|
||||||
|
System.out.println("剪切后:"+last17+" 长度:"+last17.length());
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println("----------------------");
|
||||||
|
|
||||||
|
String s1 = "1224069209961664512";
|
||||||
|
String substring = s1.substring(s1.length() - 17);
|
||||||
|
System.out.println(substring);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue