作者 crossoverJie

:sparkles: Introducing new features.客户端 push 消息 OK

1 # netty-action 1 # netty-action
2 -Netty in action 2 +
  3 +Netty 个人实战
  1 +package com.crossoverjie.netty.action.common.util;
  2 +
  3 +import java.util.Random;
  4 +import java.util.UUID;
  5 +
  6 +/**
  7 + * Function:
  8 + *
  9 + * @author crossoverJie
  10 + * Date: 22/05/2018 17:10
  11 + * @since JDK 1.8
  12 + */
  13 +public class RandomUtil {
  14 +
  15 + public static int getRandom() {
  16 +
  17 + double random = Math.random();
  18 + return (int) (random * 100);
  19 + }
  20 +}
@@ -38,7 +38,7 @@ public class HeartbeatClient { @@ -38,7 +38,7 @@ public class HeartbeatClient {
38 @Value("${netty.server.host}") 38 @Value("${netty.server.host}")
39 private String host; 39 private String host;
40 40
41 - private SocketChannel channel ; 41 + private SocketChannel channel;
42 42
43 @PostConstruct 43 @PostConstruct
44 public void start() throws InterruptedException { 44 public void start() throws InterruptedException {
@@ -57,12 +57,13 @@ public class HeartbeatClient { @@ -57,12 +57,13 @@ public class HeartbeatClient {
57 57
58 /** 58 /**
59 * 发送消息 59 * 发送消息
  60 + *
60 * @param customProtocol 61 * @param customProtocol
61 */ 62 */
62 - public void sendMsg(CustomProtocol customProtocol){ 63 + public void sendMsg(CustomProtocol customProtocol) {
63 ChannelFuture future = channel.writeAndFlush(customProtocol); 64 ChannelFuture future = channel.writeAndFlush(customProtocol);
64 future.addListener((ChannelFutureListener) channelFuture -> 65 future.addListener((ChannelFutureListener) channelFuture ->
65 - LOGGER.info("手动发消息成功={}", JSON.toJSONString(customProtocol))); 66 + LOGGER.info("客户端手动发消息成功={}", JSON.toJSONString(customProtocol)));
66 67
67 } 68 }
68 } 69 }
@@ -6,6 +6,7 @@ import com.crossoverjie.netty.action.client.vo.res.SendMsgResVO; @@ -6,6 +6,7 @@ import com.crossoverjie.netty.action.client.vo.res.SendMsgResVO;
6 import com.crossoverjie.netty.action.common.enums.StatusEnum; 6 import com.crossoverjie.netty.action.common.enums.StatusEnum;
7 import com.crossoverjie.netty.action.common.pojo.CustomProtocol; 7 import com.crossoverjie.netty.action.common.pojo.CustomProtocol;
8 import com.crossoverjie.netty.action.common.res.BaseResponse; 8 import com.crossoverjie.netty.action.common.res.BaseResponse;
  9 +import com.crossoverjie.netty.action.common.util.RandomUtil;
9 import io.swagger.annotations.ApiOperation; 10 import io.swagger.annotations.ApiOperation;
10 import org.springframework.beans.factory.annotation.Autowired; 11 import org.springframework.beans.factory.annotation.Autowired;
11 import org.springframework.stereotype.Controller; 12 import org.springframework.stereotype.Controller;
@@ -37,7 +38,7 @@ public class IndexController { @@ -37,7 +38,7 @@ public class IndexController {
37 @ResponseBody 38 @ResponseBody
38 public BaseResponse<SendMsgResVO> sendMsg(@RequestBody SendMsgReqVO sendMsgReqVO){ 39 public BaseResponse<SendMsgResVO> sendMsg(@RequestBody SendMsgReqVO sendMsgReqVO){
39 BaseResponse<SendMsgResVO> res = new BaseResponse(); 40 BaseResponse<SendMsgResVO> res = new BaseResponse();
40 - heartbeatClient.sendMsg(new CustomProtocol(122,sendMsgReqVO.getMsg())) ; 41 + heartbeatClient.sendMsg(new CustomProtocol(RandomUtil.getRandom(),sendMsgReqVO.getMsg())) ;
41 42
42 SendMsgResVO sendMsgResVO = new SendMsgResVO() ; 43 SendMsgResVO sendMsgResVO = new SendMsgResVO() ;
43 sendMsgResVO.setMsg("OK") ; 44 sendMsgResVO.setMsg("OK") ;
  1 +package com.crossoverjie.netty.action.config;
  2 +
  3 +import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
  4 +import org.springframework.context.annotation.Bean;
  5 +import org.springframework.context.annotation.Configuration;
  6 +import springfox.documentation.builders.ApiInfoBuilder;
  7 +import springfox.documentation.builders.PathSelectors;
  8 +import springfox.documentation.builders.RequestHandlerSelectors;
  9 +import springfox.documentation.service.ApiInfo;
  10 +import springfox.documentation.spi.DocumentationType;
  11 +import springfox.documentation.spring.web.plugins.Docket;
  12 +import springfox.documentation.swagger2.annotations.EnableSwagger2;
  13 +
  14 +
  15 +@Configuration
  16 +@EnableSwagger2
  17 +/** 是否打开swagger **/
  18 +@ConditionalOnExpression("'${swagger.enable}' == 'true'")
  19 +public class SwaggerConfig {
  20 +
  21 +
  22 + @Bean
  23 + public Docket createRestApi() {
  24 + return new Docket(DocumentationType.SWAGGER_2)
  25 + .apiInfo(apiInfo())
  26 + .select()
  27 + .apis(RequestHandlerSelectors.basePackage("com.crossoverjie.netty.action.controller"))
  28 + .paths(PathSelectors.any())
  29 + .build();
  30 + }
  31 +
  32 + private ApiInfo apiInfo() {
  33 + return new ApiInfoBuilder()
  34 + .title("sbc order api")
  35 + .description("sbc order api")
  36 + .termsOfServiceUrl("http://crossoverJie.top")
  37 + .contact("crossoverJie")
  38 + .version("1.0.0")
  39 + .build();
  40 + }
  41 +
  42 +}
  1 +package com.crossoverjie.netty.action.controller;
  2 +
  3 +import com.crossoverjie.netty.action.common.enums.StatusEnum;
  4 +import com.crossoverjie.netty.action.common.pojo.CustomProtocol;
  5 +import com.crossoverjie.netty.action.common.res.BaseResponse;
  6 +import com.crossoverjie.netty.action.common.util.RandomUtil;
  7 +import com.crossoverjie.netty.action.server.HeartBeatServer;
  8 +import com.crossoverjie.netty.action.vo.req.SendMsgReqVO;
  9 +import com.crossoverjie.netty.action.vo.res.SendMsgResVO;
  10 +import io.swagger.annotations.ApiOperation;
  11 +import org.springframework.beans.factory.annotation.Autowired;
  12 +import org.springframework.stereotype.Controller;
  13 +import org.springframework.web.bind.annotation.RequestBody;
  14 +import org.springframework.web.bind.annotation.RequestMapping;
  15 +import org.springframework.web.bind.annotation.ResponseBody;
  16 +
  17 +import java.util.UUID;
  18 +
  19 +/**
  20 + * Function:
  21 + *
  22 + * @author crossoverJie
  23 + * Date: 22/05/2018 14:46
  24 + * @since JDK 1.8
  25 + */
  26 +@Controller
  27 +@RequestMapping("/")
  28 +public class IndexController {
  29 +
  30 + @Autowired
  31 + private HeartBeatServer heartbeatClient ;
  32 +
  33 + /**
  34 + * 向服务端发消息
  35 + * @param sendMsgReqVO
  36 + * @return
  37 + */
  38 + @ApiOperation("发送消息")
  39 + @RequestMapping("sendMsg")
  40 + @ResponseBody
  41 + public BaseResponse<SendMsgResVO> sendMsg(@RequestBody SendMsgReqVO sendMsgReqVO){
  42 + BaseResponse<SendMsgResVO> res = new BaseResponse();
  43 + heartbeatClient.sendMsg(new CustomProtocol(RandomUtil.getRandom(),sendMsgReqVO.getMsg())) ;
  44 +
  45 + SendMsgResVO sendMsgResVO = new SendMsgResVO() ;
  46 + sendMsgResVO.setMsg("OK") ;
  47 + res.setCode(StatusEnum.SUCCESS.getCode()) ;
  48 + res.setMessage(StatusEnum.SUCCESS.getMessage()) ;
  49 + res.setDataBody(sendMsgResVO) ;
  50 + return res ;
  51 + }
  52 +}
@@ -49,7 +49,7 @@ public class HeartBeatSimpleHandle extends SimpleChannelInboundHandler<CustomPro @@ -49,7 +49,7 @@ public class HeartBeatSimpleHandle extends SimpleChannelInboundHandler<CustomPro
49 49
50 //手动处理数据并返回 50 //手动处理数据并返回
51 customProtocol.setHeader(customProtocol.getHeader() + 1000); 51 customProtocol.setHeader(customProtocol.getHeader() + 1000);
52 - customProtocol.setContent(customProtocol.getContent() + 1000); 52 + customProtocol.setContent(customProtocol.getContent() + "asdfg");
53 ctx.writeAndFlush(Unpooled.copiedBuffer(customProtocol.toString(), CharsetUtil.UTF_8)); 53 ctx.writeAndFlush(Unpooled.copiedBuffer(customProtocol.toString(), CharsetUtil.UTF_8));
54 } 54 }
55 } 55 }
1 package com.crossoverjie.netty.action.server; 1 package com.crossoverjie.netty.action.server;
2 2
  3 +import com.alibaba.fastjson.JSON;
3 import com.crossoverjie.netty.action.channel.init.HeartbeatInitializer; 4 import com.crossoverjie.netty.action.channel.init.HeartbeatInitializer;
4 import com.crossoverjie.netty.action.common.pojo.CustomProtocol; 5 import com.crossoverjie.netty.action.common.pojo.CustomProtocol;
5 import io.netty.bootstrap.ServerBootstrap; 6 import io.netty.bootstrap.ServerBootstrap;
@@ -77,6 +78,9 @@ public class HeartBeatServer { @@ -77,6 +78,9 @@ public class HeartBeatServer {
77 * @param customProtocol 78 * @param customProtocol
78 */ 79 */
79 public void sendMsg(CustomProtocol customProtocol){ 80 public void sendMsg(CustomProtocol customProtocol){
80 - channel.writeAndFlush(customProtocol) ; 81 + ChannelFuture future = channel.writeAndFlush(customProtocol);
  82 +
  83 + future.addListener((ChannelFutureListener) channelFuture ->
  84 + LOGGER.info("服务端手动发消息成功={}", JSON.toJSONString(customProtocol)));
81 } 85 }
82 } 86 }
1 # web port 1 # web port
2 server.port=8081 2 server.port=8081
3 3
  4 +# 是否打开swagger
  5 +swagger.enable = true
  6 +
4 netty.server.port=11211 7 netty.server.port=11211
5 8
6 logging.level.root=info 9 logging.level.root=info