您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)tio-core-spring-boot-starter如何整合tio到springboot項(xiàng)目,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
首先,你需要在 pom.xml 中引入 tio-core-spring-boot-starter
構(gòu)件
<dependency> <groupId>org.t-io</groupId> <artifactId>tio-core-spring-boot-starter</artifactId> <!--此版本號(hào)跟著tio主版本號(hào)一致即可--> <version>3.3.5.v20190712-RELEASE</version> </dependency>
一、給SpringBoot Application 主類添加 @EnableTioServerServer
注解
@SpringBootApplication @EnableTioServerServer public class TioServerApplication { public static void main(String[] args) { SpringApplication.run(TioServerApplication.class, args); } }
二、接下來(lái),修改配置文件
tio: core: server: # websocket port default 9876 port: 6789 # 心跳時(shí)間 heartbeat-timeout: 60000 # 集群配置 默認(rèn)關(guān)閉 cluster: enabled: false # 集群是通過(guò)redis的Pub/Sub實(shí)現(xiàn),所以需要配置Redis redis: ip: 127.0.0.1 port: 6379 all: true group: true ip: true user: true # SSL 配置 ssl: enabled: false key-store: password: trust-store:
三、編寫消息處理類
/** * 消息處理 handler, 通過(guò)加 {@link TioServerMsgHandler} 注解啟用,否則不會(huì)啟用 * 注意: handler 是必須要啟用的,否則啟動(dòng)會(huì)拋出 {@link TioMsgHandlerNotFoundException} 異常 * * @author yangjian */ @TioServerMsgHandler public class HelloServerMsgHandler implements ServerAioHandler { /** * 解碼:把接收到的ByteBuffer,解碼成應(yīng)用可以識(shí)別的業(yè)務(wù)消息包 * 總的消息結(jié)構(gòu):消息頭 + 消息體 * 消息頭結(jié)構(gòu): 4個(gè)字節(jié),存儲(chǔ)消息體的長(zhǎng)度 * 消息體結(jié)構(gòu): 對(duì)象的json串的byte[] */ @Override public HelloPacket decode(ByteBuffer buffer, int limit, int position, int readableLength, ChannelContext channelContext) throws AioDecodeException { return PacketUtil.decode(buffer, limit, position, readableLength, channelContext); } /** * 編碼:把業(yè)務(wù)消息包編碼為可以發(fā)送的ByteBuffer * 總的消息結(jié)構(gòu):消息頭 + 消息體 * 消息頭結(jié)構(gòu): 4個(gè)字節(jié),存儲(chǔ)消息體的長(zhǎng)度 * 消息體結(jié)構(gòu): 對(duì)象的json串的byte[] */ @Override public ByteBuffer encode(Packet packet, GroupContext groupContext, ChannelContext channelContext) { return PacketUtil.encode(packet, groupContext, channelContext); } /** * 處理消息 */ @Override public void handler(Packet packet, ChannelContext channelContext) throws Exception { HelloPacket helloPacket = (HelloPacket) packet; byte[] body = helloPacket.getBody(); if (body != null) { String str = new String(body, HelloPacket.CHARSET); System.out.println("收到消息:" + str); HelloPacket resppacket = new HelloPacket(); resppacket.setBody(("收到了你的消息,你的消息是:" + str).getBytes(HelloPacket.CHARSET)); Tio.send(channelContext, resppacket); } return; } }
四、實(shí)現(xiàn)消息實(shí)體包
/** * 消息包實(shí)體 * * @author yangjian */ public class HelloPacket extends Packet { private static final long serialVersionUID = -172060606924066412L; public static final int HEADER_LENGTH = 4;//消息頭的長(zhǎng)度 public static final String CHARSET = "utf-8"; private byte[] body; /** * @return the body */ public byte[] getBody() { return body; } /** * @param body the body to set */ public void setBody(byte[] body) { this.body = body; } }
接下來(lái)啟動(dòng)服務(wù)端
客戶端采用 Tio 的常規(guī)程序啟動(dòng),只有三個(gè)文件,啟動(dòng)非常簡(jiǎn)單。
一、編寫常量類
public interface Const { /** * 服務(wù)器地址 */ public static final String SERVER = "127.0.0.1"; /** * 監(jiān)聽(tīng)端口 */ public static final int PORT = 6789; /** * 心跳超時(shí)時(shí)間 */ public static final int TIMEOUT = 5000; }
二、消息處理類
public class HelloClientAioHandler implements ClientAioHandler { private static HelloPacket heartbeatPacket = new HelloPacket(); /** * 解碼:把接收到的ByteBuffer,解碼成應(yīng)用可以識(shí)別的業(yè)務(wù)消息包 * 總的消息結(jié)構(gòu):消息頭 + 消息體 * 消息頭結(jié)構(gòu): 4個(gè)字節(jié),存儲(chǔ)消息體的長(zhǎng)度 * 消息體結(jié)構(gòu): 對(duì)象的json串的byte[] */ @Override public HelloPacket decode(ByteBuffer buffer, int limit, int position, int readableLength, ChannelContext channelContext) throws AioDecodeException { return PacketUtil.decode(buffer, limit, position, readableLength, channelContext); } /** * 編碼:把業(yè)務(wù)消息包編碼為可以發(fā)送的ByteBuffer * 總的消息結(jié)構(gòu):消息頭 + 消息體 * 消息頭結(jié)構(gòu): 4個(gè)字節(jié),存儲(chǔ)消息體的長(zhǎng)度 * 消息體結(jié)構(gòu): 對(duì)象的json串的byte[] */ @Override public ByteBuffer encode(Packet packet, GroupContext groupContext, ChannelContext channelContext) { return PacketUtil.encode(packet, groupContext, channelContext); } /** * 處理消息 */ @Override public void handler(Packet packet, ChannelContext channelContext) throws Exception { HelloPacket helloPacket = (HelloPacket) packet; byte[] body = helloPacket.getBody(); if (body != null) { String str = new String(body, HelloPacket.CHARSET); System.out.println("收到消息:" + str); } return; } /** * 此方法如果返回null,框架層面則不會(huì)發(fā)心跳;如果返回非null,框架層面會(huì)定時(shí)發(fā)本方法返回的消息包 */ @Override public HelloPacket heartbeatPacket(ChannelContext channelContext) { return heartbeatPacket; }
三、客戶端啟動(dòng)類
public class HelloClientStarter { //服務(wù)器節(jié)點(diǎn) public static Node serverNode = new Node(Const.SERVER, Const.PORT); //handler, 包括編碼、解碼、消息處理 public static ClientAioHandler tioClientHandler = new HelloClientAioHandler(); //事件監(jiān)聽(tīng)器,可以為null,但建議自己實(shí)現(xiàn)該接口,可以參考showcase了解些接口 public static ClientAioListener aioListener = null; //斷鏈后自動(dòng)連接的,不想自動(dòng)連接請(qǐng)?jiān)O(shè)為null private static ReconnConf reconnConf = new ReconnConf(5000L); //一組連接共用的上下文對(duì)象 public static ClientGroupContext clientGroupContext = new ClientGroupContext(tioClientHandler, aioListener, reconnConf); public static TioClient tioClient = null; public static ClientChannelContext clientChannelContext = null; /** * 啟動(dòng)程序入口 */ public static void main(String[] args) throws Exception { clientGroupContext.setHeartbeatTimeout(Const.TIMEOUT); tioClient = new TioClient(clientGroupContext); clientChannelContext = tioClient.connect(serverNode); //連上后,發(fā)條消息玩玩 send(); } private static void send() throws Exception { HelloPacket packet = new HelloPacket(); packet.setBody("hello world".getBytes(HelloPacket.CHARSET)); Tio.send(clientChannelContext, packet); } }
啟動(dòng)客戶端端,查看終端輸出。
服務(wù)端輸出
跟 handler 一樣,其他原生回調(diào)接口的使用方法保持不變,只需要在對(duì)應(yīng)的實(shí)現(xiàn)類上加上對(duì)應(yīng)的注解就 OK 了。
//最主要的邏輯處理類,必須要寫,否則拋異常 public class HelloServerMsgHandler implements ServerAioHandler {} //可不寫,通過(guò)加 @TioServerAioListener 注解啟用,否則不會(huì)啟用 public class HelloServerAioListener implements ServerAioListener {} //可不寫, 通過(guò)加 @TioServerGroupListener 注解啟用,否則不會(huì)啟用 public class HelloServerGroupListener implements GroupListener{} //可不寫,通過(guò)加 @link TioServerIpStatListener 注解啟用,否則不會(huì)啟用 public class HelloServerIpStatListener implements IpStatListener {}
這里注意:每個(gè)對(duì)應(yīng)的回調(diào)接口都需要通過(guò)添加注解手動(dòng)啟用,否則默認(rèn)不啟用,不會(huì)自動(dòng)掃描
這個(gè)也非常簡(jiǎn)單,只需獲取到 TioServerBootstrap
,其他都變得非常簡(jiǎn)單。
@RestController public class HelloController { static Logger logger = LoggerFactory.getLogger(HelloController.class); @Autowired private TioServerBootstrap bootstrap; @GetMapping("/") public String index() { return "Hello, tio-spring-boot-starter !!!"; } /** * 推送消息到客戶端 * @throws Exception */ @GetMapping("/push") public String pushMessage() throws Exception { HelloPacket packet = new HelloPacket(); packet.setBody("This message is pushed by Tio Server.".getBytes(HelloPacket.CHARSET)); Tio.sendToAll(bootstrap.getServerGroupContext(), packet); logger.info("Push a message to client successfully"); return "Push a message to client successfully"; } }
客戶端輸出截圖
# SSL 配置 ssl: enabled: true key-store: key-store path password: password trust-store: trust-store path
# 集群配置 默認(rèn)關(guān)閉 cluster: enabled: false # 集群是通過(guò)redis的Pub/Sub實(shí)現(xiàn),所以需要配置Redis redis: ip: 127.0.0.1 port: 6379 all: true group: true ip: true user: true
https://github.com/tywo45/t-io/tree/master/src/zoo/spring-boot/tio-core/src/main/java/org/tio/core/starter
看完上述內(nèi)容,你們對(duì)tio-core-spring-boot-starter如何整合tio到springboot項(xiàng)目有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。