Browse Source

20210510测试netty

logistics
zy_Java 4 years ago
parent
commit
076e1e0846
  1. 13
      logistics/src/main/java/com/ccsens/logistics/LogisticsApplication.java
  2. 4
      logistics/src/main/java/com/ccsens/logistics/service/INettyService.java
  3. 13
      logistics/src/main/java/com/ccsens/logistics/service/NerryService.java
  4. 24
      util/src/test/java/com/ccsens/util/SimpleClientHandler.java
  5. 60
      util/src/test/java/com/ccsens/util/nattyTest.java

13
logistics/src/main/java/com/ccsens/logistics/LogisticsApplication.java

@ -1,6 +1,8 @@
package com.ccsens.logistics; package com.ccsens.logistics;
import com.ccsens.logistics.service.INettyService;
import org.mybatis.spring.annotation.MapperScan; import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.boot.web.servlet.ServletComponentScan;
@ -8,6 +10,8 @@ import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableAsync;
import javax.annotation.Resource;
@MapperScan(basePackages = {"com.ccsens.logistics.persist.*"}) @MapperScan(basePackages = {"com.ccsens.logistics.persist.*"})
@ServletComponentScan @ServletComponentScan
@EnableAsync @EnableAsync
@ -15,10 +19,17 @@ import org.springframework.scheduling.annotation.EnableAsync;
@EnableCircuitBreaker @EnableCircuitBreaker
@EnableFeignClients(basePackages = "com.ccsens.cloudutil.feign") @EnableFeignClients(basePackages = "com.ccsens.cloudutil.feign")
@SpringBootApplication(scanBasePackages = "com.ccsens") @SpringBootApplication(scanBasePackages = "com.ccsens")
public class LogisticsApplication { public class LogisticsApplication implements CommandLineRunner {
@Resource
private INettyService nettyService;
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(LogisticsApplication.class, args); SpringApplication.run(LogisticsApplication.class, args);
} }
@Override
public void run(String... args) throws Exception {
}
} }

4
logistics/src/main/java/com/ccsens/logistics/service/INettyService.java

@ -0,0 +1,4 @@
package com.ccsens.logistics.service;
public interface INettyService {
}

13
logistics/src/main/java/com/ccsens/logistics/service/NerryService.java

@ -0,0 +1,13 @@
package com.ccsens.logistics.service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Slf4j
@Service
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public class NerryService implements INettyService{
}

24
util/src/test/java/com/ccsens/util/SimpleClientHandler.java

@ -0,0 +1,24 @@
package com.ccsens.util;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.AttributeKey;
import java.nio.charset.Charset;
public class SimpleClientHandler extends ChannelInboundHandlerAdapter{
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof ByteBuf) {
String value = ((ByteBuf) msg).toString(Charset.defaultCharset());
System.out.println("服务器端返回的数据:" + value);
}
AttributeKey<String> key = AttributeKey.valueOf("ServerData");
ctx.channel().attr(key).set("客户端处理完毕");
//把客户端的通道关闭
// ctx.channel().close();
}
}

60
util/src/test/java/com/ccsens/util/nattyTest.java

@ -0,0 +1,60 @@
package com.ccsens.util;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.string.StringEncoder;
public class nattyTest {
public static void main(String[] args) throws InterruptedException {
// 首先,netty通过ServerBootstrap启动服务端
Bootstrap client = new Bootstrap();
//第1步 定义线程组,处理读写和链接事件,没有了accept事件
EventLoopGroup group = new NioEventLoopGroup();
client.group(group );
//第2步 绑定客户端通道
client.channel(NioSocketChannel.class);
//第3步 给NIoSocketChannel初始化handler, 处理读写事件
client.handler(new ChannelInitializer<NioSocketChannel>() { //通道是NioSocketChannel
@Override
protected void initChannel(NioSocketChannel ch) throws Exception {
//字符串编码器,一定要加在SimpleClientHandler 的上面
ch.pipeline().addLast(new StringEncoder());
ch.pipeline().addLast(new DelimiterBasedFrameDecoder(
Integer.MAX_VALUE, Delimiters.lineDelimiter()[0]));
//找到他的管道 增加他的handler
ch.pipeline().addLast(new SimpleClientHandler());
}
});
//连接服务器
ChannelFuture future = client.connect("47.106.129.173", 15506).sync();
for(int i=0;i<5;i++){
future.channel().writeAndFlush("{\"service_id\":\"123\",\"type\":\"READSN4\",\"thing_id\":\"050005A2\",\"data\":{\"GetType\":\"Auto\",\"Highest\":70,\"Lowest\":30}}"+"\r\n");
System.out.println("请求");
Thread.sleep(5000);
}
// //当通道关闭了,就继续往下走
// future.channel().closeFuture().sync();
//接收服务端返回的数据
// AttributeKey<String> key = AttributeKey.valueOf("ServerData");
// Object result = future.channel().attr(key).get();
// System.out.println(result.toString());
}
}
Loading…
Cancel
Save