1、安装RocketMQ,忽略;
2、创建springboot项目,pom添加
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.53.Final</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.53.Final</version>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
<version>4.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
<version>4.7.1</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>29.0-jre</version>
</dependency>
</dependencies>
3、MyChannelHandlerPool.java类,channelHandler设置。
/**
* @Author: Liu Yue
* @Descripition:
* @Date; Create in 2020/1/14 9:24
**/
public class MyChannelHandlerPool {
public MyChannelHandlerPool(){}
public static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
}
4、MyWebSocketHandler,socketHandler
/**
* @Author: Liu Yue
* @Descripition:
* @Date; Create in 2020/1/14 9:25
**/
@Configuration
@Component
@Slf4j
public class MyWebSocketHandler extends ChannelInboundHandlerAdapter {
@Autowired
private ProducerConfigure defaultMQProducer;
@Autowired
private ProducerConfig producerConfigure;
public MyWebSocketHandler(){}
public MyWebSocketHandler(ProducerConfigure defaultMQProducer,ProducerConfig producerConfigure){
this.defaultMQProducer = defaultMQProducer;
this.producerConfigure = producerConfigure;
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("与客户端建立连接,通道开启!");
//添加到channelGroup通道组
//添加到channelGroup通道组
MyChannelHandlerPool.channelGroup.add(ctx.channel());
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.out.println("与客户端断开连接,通道关闭!");
//添加到channelGroup 通道组
MyChannelHandlerPool.channelGroup.remove(ctx.channel());
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
log.info("server channelRead......" + " thread :" + ctx.getClass());
log.info("获取值:" + msg);
Message message = new Message(producerConfigure.getTopic(), producerConfigure.getTags(), System.currentTimeMillis()+"", msg.toString().getBytes());
// 这里用到了这个mq的异步处理,类似ajax,可以得到发送到mq的情况,并做相应的处理
//不过要注意的是这个是异步的
DefaultMQProducer producer = this.defaultMQProducer.defaultProducer();
producer.send(message, new SendCallback() {
@Override
public void onSuccess(SendResult sendResult) {
log.info("传输成功");
//log.info(GsonUtil.GSON.toJson(sendResult));
}
@Override
public void onException(Throwable e) {
log.error("传输失败", e);
}
});
//将客户端的信息直接返回写入ctx
ctx.write(msg + "\n");
//刷新缓存区
ctx.flush();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
public void sendAllMessage(String message,final String mark){
//收到信息后,群发给所有channel
log.info("向服务器发送信息:{}",message);
//MyChannelHandlerPool.channelGroup.writeAndFlush( new TextWebSocketFrame(message));
byte[] bytes = MyStringUtil.hexString2Bytes(message);
ByteBuf bufff = Unpooled.buffer();//netty需要用ByteBuf传输
bufff.writeBytes(bytes);//对接需要16进制
//MyChannelHandlerPool.channelGroup.writeAndFlush(bufff);
MyChannelHandlerPool.channelGroup.writeAndFlush(bufff).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
StringBuilder sb = new StringBuilder("");
if(!StringUtils.isEmpty(mark)){
sb.append("【").append(mark).append("】");
}
if (future.isSuccess()) {
System.out.println(sb.toString()+"回写成功"+message);
log.info(sb.toString()+"回写成功"+message);
} else {
System.out.println(sb.toString()+"回写失败"+message);
log.error(sb.toString()+"回写失败"+message);
}
}
});
}
5、NettyServer
/**
* @Author: Liu Yue
* @Descripition:
* @Date; Create in 2020/1/14 9:22
**/
@Configuration
@Slf4j
public class NettyServer {
@Autowired
private ProducerConfigure defaultMQProducer;
@Autowired
private ProducerConfig producerConfigure;
public void start(InetSocketAddress address) throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup group = new NioEventLoopGroup();
try {
ServerBootstrap sb = new ServerBootstrap();
sb.option(ChannelOption.SO_BACKLOG, 1024);
sb.group(group, bossGroup) // 绑定线程池
.channel(NioServerSocketChannel.class) // 指定使用的channel
.localAddress(address)// 绑定监听端口
.childHandler(new ChannelInitializer<SocketChannel>() { // 绑定客户端连接时候触发操作
@Override
protected void initChannel(SocketChannel ch) throws Exception {
log.info("收到新连接");
//ch.pipeline().addLast("decoder",new StringDecoder(CharsetUtil.UTF_8));
//ch.pipeline().addLast("encoder",new StringEncoder(CharsetUtil.UTF_8));
ChannelPipeline pipeline = ch.pipeline();
//IdleStateHandler心跳机制,如果超时触发Handle中userEventTrigger()方法
pipeline.addLast("idleStateHandler",
new IdleStateHandler(5, 0, 0, TimeUnit.MINUTES));
//修改了这里
pipeline.addLast("decoder", new ZMFADecoder());
pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 4, 4, -8, 0));
//pipeline.addLast("byteArrayEncoder", new ByteArrayEncoder());
//自定义Handler
pipeline.addLast(new MyWebSocketHandler(defaultMQProducer,producerConfigure));
}
})
/*.childHandler(new ServerChannelInitializer())*/
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture cf = sb.bind().sync(); // 服务器异步创建绑定
log.info(NettyServer.class + " 启动正在监听: " + cf.channel().localAddress());
cf.channel().closeFuture().sync(); // 关闭服务器通道
} finally {
group.shutdownGracefully().sync(); // 释放线程池资源
bossGroup.shutdownGracefully().sync();
}
}
}
6、ZMFADecoder
/**
* @Author: Liu Yue
* @Descripition: 16进制解码器
* @Date; Create in 2020/10/30 9:05
**/
@Component
@Slf4j
public class ZMFADecoder extends ByteToMessageDecoder {
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
//创建字节数组,buffer.readableBytes可读字节长度
byte[] b = new byte[buffer.readableBytes()];
//复制内容到字节数组b
buffer.readBytes(b);
//字节数组转字符串
out.add(bytesToHexString(b));
}
public String bytesToHexString(byte[] bArray) {
StringBuffer sb = new StringBuffer(bArray.length);
String sTemp;
for (int i = 0; i < bArray.length; i++) {
sTemp = Integer.toHexString(0xFF & bArray[i]);
if (sTemp.length() < 2)
sb.append(0);
sb.append(sTemp.toUpperCase());
}
return sb.toString();
}
public static String toHexString1(byte[] b) {
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < b.length; ++i) {
buffer.append(toHexString1(b[i]));
}
return buffer.toString();
}
public static String toHexString1(byte b) {
String s = Integer.toHexString(b & 0xFF);
if (s.length() == 1) {
return "0" + s;
} else {
return s;
}
}
}
7、ProducerConfigure
/**
* @Author: Liu Yue
* @Descripition:
* @Date; Create in 2020/11/2 10:47
**/
@Configuration
@Slf4j
public class ProducerConfigure {
@Autowired
private ProducerConfig producerConfigure;
/**
* 创建普通消息发送者实例
*
* @return
* @throws MQClientException
*/
@Bean
public DefaultMQProducer defaultProducer() throws MQClientException {
log.info(producerConfigure.toString());
log.info("defaultProducer 正在创建---------------------------------------");
DefaultMQProducer producer = new DefaultMQProducer(producerConfigure.getGroupname());
producer.setNamesrvAddr(producerConfigure.getNamesrvaddr());
producer.setVipChannelEnabled(false);
producer.setRetryTimesWhenSendAsyncFailed(10);
producer.start();
log.info("rocketmq producer server开启成功---------------------------------.");
return producer;
}
}
8、工具类MyStringUtil
/**
* @Author: Liu Yue
* @Descripition:
* @Date; Create in 2020/11/4 10:17
**/
@Component
public class MyStringUtil {
/**
* @Title:bytes2HexString
* @Description:字节数组转16进制字符串
* @param b
* 字节数组
* @return 16进制字符串
* @throws
*/
public static String bytes2HexString(byte[] b) {
StringBuffer result = new StringBuffer();
String hex;
for (int i = 0; i < b.length; i++) {
hex = Integer.toHexString(b[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
result.append(hex.toUpperCase());
}
return result.toString();
}
/**
* @Title:hexString2Bytes
* @Description:16进制字符串转字节数组
* @param src 16进制字符串
* @return 字节数组
*/
public static byte[] hexString2Bytes(String src) {
src = src.replaceAll(" ", "");
byte[] bytes = new byte[src.length() / 2];
for (int i = 0; i < src.length() / 2; i++) {
String subStr = src.substring(i * 2, i * 2 + 2);
bytes[i] = (byte) Integer.parseInt(subStr, 16);
}
return bytes;
}
/**
* @Title:string2HexString
* @Description:字符串转16进制字符串
* @param strPart 字符串
* @return 16进制字符串
*/
public static String string2HexString(String strPart) {
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < strPart.length(); i++) {
int ch = (int) strPart.charAt(i);
String strHex = Integer.toHexString(ch);
hexString.append(strHex);
}
return hexString.toString();
}
/**
* @Title:hexString2String
* @Description:16进制字符串转字符串
* @param src
* 16进制字符串
* @return 字节数组
* @throws
*/
public static String hexString2String(String src) {
String temp = "";
for (int i = 0; i < src.length() / 2; i++) {
//System.out.println(Integer.valueOf(src.substring(i * 2, i * 2 + 2),16).byteValue());
temp = temp+ (char)Integer.valueOf(src.substring(i * 2, i * 2 + 2),16).byteValue();
}
return temp;
}
/**
* @Title:char2Byte
* @Description:字符转成字节数据char-->integer-->byte
* @param src
* @return
* @throws
*/
public static Byte char2Byte(Character src) {
return Integer.valueOf((int)src).byteValue();
}
/**
* @Title:intToHexString
* @Description:10进制数字转成16进制
* @param a 转化数据
* @param len 占用字节数
* @return
* @throws
*/
public static String intToHexString(int a,int len){
len<<=1;
String hexString = Integer.toHexString(a);
int b = len -hexString.length();
if(b>0){
for(int i=0;i<b;i++) {
hexString = "0" + hexString;
}
}
return hexString;
}
/**
* 将16进制的2个字符串进行异或运算
* http://blog.csdn.net/acrambler/article/details/45743157
* @param strHex_X
* @param strHex_Y
* 注意:此方法是针对一个十六进制字符串一字节之间的异或运算,如对十五字节的十六进制字符串异或运算:1312f70f900168d900007df57b4884
先进行拆分:13 12 f7 0f 90 01 68 d9 00 00 7d f5 7b 48 84
13 xor 12-->1
1 xor f7-->f6
f6 xor 0f-->f9
....
62 xor 84-->e6
即,得到的一字节校验码为:e6
* @return
*/
public static String xor(String strHex_X,String strHex_Y){
//将x、y转成二进制形式
String anotherBinary=Integer.toBinaryString(Integer.valueOf(strHex_X,16));
String thisBinary=Integer.toBinaryString(Integer.valueOf(strHex_Y,16));
String result = "";
//判断是否为8位二进制,否则左补零
if(anotherBinary.length() != 8){
for (int i = anotherBinary.length(); i <8; i++) {
anotherBinary = "0"+anotherBinary;
}
}
if(thisBinary.length() != 8){
for (int i = thisBinary.length(); i <8; i++) {
thisBinary = "0"+thisBinary;
}
}
//异或运算
for(int i=0;i<anotherBinary.length();i++){
//如果相同位置数相同,则补0,否则补1
if(thisBinary.charAt(i)==anotherBinary.charAt(i))
result+="0";
else{
result+="1";
}
}
return Integer.toHexString(Integer.parseInt(result, 2));
}
/**
* Convert byte[] to hex string.这里我们可以将byte转换成int
* @param src byte[] data
* @return hex string
*/
public static String bytes2Str(byte[] src){
StringBuilder stringBuilder = new StringBuilder("");
if (src == null || src.length <= 0) {
return null;
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
}
/**
* @param by
* @return 接收字节数据并转为16进制字符串
*/
public static String receiveHexToString(byte[] by) {
try {
/*io.netty.buffer.WrappedByteBuf buf = (WrappedByteBuf)msg;
ByteBufInputStream is = new ByteBufInputStream(buf);
byte[] by = input2byte(is);*/
String str = bytes2Str(by);
str = str.toLowerCase();
return str;
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("接收字节数据并转为16进制字符串异常");
}
return null;
}
/**
* "7dd",4,'0'==>"07dd"
* @param input 需要补位的字符串
* @param size 补位后的最终长度
* @param symbol 按symol补充 如'0'
* @return
* N_TimeCheck中用到了
*/
public static String fill(String input, int size, char symbol) {
while (input.length() < size) {
input = symbol + input;
}
return input;
}
}
9、配置文件ProducerConfig
/**
* @Author: Liu Yue
* @Descripition:
* @Date; Create in 2020/11/2 10:27
**/
@Data
@Slf4j
@Configuration
@Component
@ConfigurationProperties(prefix = "rocketmq.producer")
@ToString
public class ProducerConfig {
private String namesrvaddr;
private String groupname;
private String topic;
private String tags;
}
yml配置
rocketmq:
producer:
namesrvaddr: 192.168.2.0:9876
groupname: base_group_syncMsg
topic: base_topic
tags: base_tags
10、MessageController
/**
* @Author: Liu Yue
* @Descripition:修改服务器的阈值
* @Date; Create in 2020/11/4 10:18
**/
@RestController
@RequestMapping("/fegin/server")
public class MessageController {
@Resource
MyWebSocketHandler myWebSocketHandler;
@GetMapping("/send/{id}/{thresholdval}")
public String send(@PathVariable("id") String id, @PathVariable("thresholdval")String thresholdval){
String msg = "FD FE "+id+" 00 00 "+ thresholdval;
myWebSocketHandler.sendAllMessage(msg,"SUCCESS");
return "SUCCESS";
}
}
11、设置
http://localhost:9005/rocketmq-producer/fegin/server/send/01/FB
三个功能,1、netty监听服务;2、rocketMQ 生产者;3、设置阈值MessageController。下一题,使用fegin客户端。
来源:oschina
链接:https://my.oschina.net/maojindaoGG/blog/4702879