作者 crossoverJie

:sparkles: Introducing new features httpclient

1 package com.crossoverjie.netty.action.client.config; 1 package com.crossoverjie.netty.action.client.config;
2 2
3 import com.crossoverjie.netty.action.common.protocol.BaseRequestProto; 3 import com.crossoverjie.netty.action.common.protocol.BaseRequestProto;
  4 +import okhttp3.OkHttpClient;
4 import org.springframework.beans.factory.annotation.Value; 5 import org.springframework.beans.factory.annotation.Value;
5 import org.springframework.context.annotation.Bean; 6 import org.springframework.context.annotation.Bean;
6 import org.springframework.context.annotation.Configuration; 7 import org.springframework.context.annotation.Configuration;
7 8
  9 +import java.util.concurrent.TimeUnit;
  10 +
8 /** 11 /**
9 - * Function:构建心跳使用的 bean 12 + * Function:bean 配置
10 * 13 *
11 * @author crossoverJie 14 * @author crossoverJie
12 * Date: 24/05/2018 15:55 15 * Date: 24/05/2018 15:55
13 * @since JDK 1.8 16 * @since JDK 1.8
14 */ 17 */
15 @Configuration 18 @Configuration
16 -public class HeartBeatConfig { 19 +public class BeanConfig {
17 20
18 @Value("${client.request.id}") 21 @Value("${client.request.id}")
19 private int requestId; 22 private int requestId;
20 23
21 24
  25 + /**
  26 + * 创建心跳单例
  27 + * @return
  28 + */
22 @Bean(value = "heartBeat") 29 @Bean(value = "heartBeat")
23 public BaseRequestProto.RequestProtocol heartBeat() { 30 public BaseRequestProto.RequestProtocol heartBeat() {
24 -  
25 BaseRequestProto.RequestProtocol heart = BaseRequestProto.RequestProtocol.newBuilder() 31 BaseRequestProto.RequestProtocol heart = BaseRequestProto.RequestProtocol.newBuilder()
26 .setRequestId(requestId) 32 .setRequestId(requestId)
27 .setReqMsg("ping") 33 .setReqMsg("ping")
28 .build(); 34 .build();
29 -  
30 return heart; 35 return heart;
31 } 36 }
  37 +
  38 +
  39 + /**
  40 + * http client
  41 + * @return okHttp
  42 + */
  43 + @Bean
  44 + public OkHttpClient okHttpClient() {
  45 + OkHttpClient.Builder builder = new OkHttpClient.Builder();
  46 + builder.connectTimeout(30, TimeUnit.SECONDS)
  47 + .readTimeout(10, TimeUnit.SECONDS)
  48 + .writeTimeout(10,TimeUnit.SECONDS)
  49 + .retryOnConnectionFailure(true);
  50 + return builder.build();
  51 + }
  52 +
32 } 53 }
1 package com.crossoverjie.netty.action.client.controller; 1 package com.crossoverjie.netty.action.client.controller;
2 2
3 import com.crossoverjie.netty.action.client.HeartbeatClient; 3 import com.crossoverjie.netty.action.client.HeartbeatClient;
  4 +import com.crossoverjie.netty.action.client.service.RouteRequest;
4 import com.crossoverjie.netty.action.client.vo.req.GoogleProtocolVO; 5 import com.crossoverjie.netty.action.client.vo.req.GoogleProtocolVO;
5 import com.crossoverjie.netty.action.client.vo.req.SendMsgReqVO; 6 import com.crossoverjie.netty.action.client.vo.req.SendMsgReqVO;
6 import com.crossoverjie.netty.action.client.vo.req.StringReqVO; 7 import com.crossoverjie.netty.action.client.vo.req.StringReqVO;
@@ -11,10 +12,6 @@ import com.crossoverjie.netty.action.common.pojo.CustomProtocol; @@ -11,10 +12,6 @@ import com.crossoverjie.netty.action.common.pojo.CustomProtocol;
11 import com.crossoverjie.netty.action.common.res.BaseResponse; 12 import com.crossoverjie.netty.action.common.res.BaseResponse;
12 import com.crossoverjie.netty.action.common.res.NULLBody; 13 import com.crossoverjie.netty.action.common.res.NULLBody;
13 import io.swagger.annotations.ApiOperation; 14 import io.swagger.annotations.ApiOperation;
14 -import okhttp3.MediaType;  
15 -import okhttp3.OkHttpClient;  
16 -import okhttp3.Request;  
17 -import okhttp3.Response;  
18 import org.springframework.beans.factory.annotation.Autowired; 15 import org.springframework.beans.factory.annotation.Autowired;
19 import org.springframework.boot.actuate.metrics.CounterService; 16 import org.springframework.boot.actuate.metrics.CounterService;
20 import org.springframework.stereotype.Controller; 17 import org.springframework.stereotype.Controller;
@@ -23,8 +20,6 @@ import org.springframework.web.bind.annotation.RequestMapping; @@ -23,8 +20,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
23 import org.springframework.web.bind.annotation.RequestMethod; 20 import org.springframework.web.bind.annotation.RequestMethod;
24 import org.springframework.web.bind.annotation.ResponseBody; 21 import org.springframework.web.bind.annotation.ResponseBody;
25 22
26 -import java.io.IOException;  
27 -  
28 /** 23 /**
29 * Function: 24 * Function:
30 * 25 *
@@ -45,6 +40,11 @@ public class IndexController { @@ -45,6 +40,11 @@ public class IndexController {
45 @Autowired 40 @Autowired
46 private HeartbeatClient heartbeatClient ; 41 private HeartbeatClient heartbeatClient ;
47 42
  43 +
  44 +
  45 + @Autowired
  46 + private RouteRequest routeRequest ;
  47 +
48 /** 48 /**
49 * 向服务端发消息 49 * 向服务端发消息
50 * @param sendMsgReqVO 50 * @param sendMsgReqVO
@@ -128,39 +128,15 @@ public class IndexController { @@ -128,39 +128,15 @@ public class IndexController {
128 @ApiOperation("群发消息") 128 @ApiOperation("群发消息")
129 @RequestMapping(value = "sendGroupMsg",method = RequestMethod.POST) 129 @RequestMapping(value = "sendGroupMsg",method = RequestMethod.POST)
130 @ResponseBody 130 @ResponseBody
131 - public BaseResponse<SendMsgResVO> sendGroupMsg(@RequestBody SendMsgReqVO sendMsgReqVO) throws IOException {  
132 - BaseResponse<SendMsgResVO> res = new BaseResponse();  
133 - OkHttpClient client = new OkHttpClient();  
134 - MediaType MEDIA_TYPE_MARKDOWN  
135 - = MediaType.parse("text/x-markdown; charset=utf-8");  
136 -  
137 - String postBody = ""  
138 - + "Releases\n"  
139 - + "--------\n"  
140 - + "\n"  
141 - + " * _1.0_ May 6, 2013\n"  
142 - + " * _1.1_ June 15, 2013\n"  
143 - + " * _1.2_ August 11, 2013\n";  
144 -  
145 - Request request = new Request.Builder()  
146 - .url("https://api.github.com/markdown/raw")  
147 - .post(okhttp3.RequestBody.create(MEDIA_TYPE_MARKDOWN, postBody))  
148 - .build();  
149 -  
150 - Response response = client.newCall(request).execute();  
151 - if (!response.isSuccessful()){  
152 - throw new IOException("Unexpected code " + response);  
153 - } 131 + public BaseResponse sendGroupMsg(@RequestBody SendMsgReqVO sendMsgReqVO) throws Exception {
  132 + BaseResponse<NULLBody> res = new BaseResponse();
154 133
155 - System.out.println(response.body().string()); 134 + routeRequest.sendGroupMsg(sendMsgReqVO.getMsg()) ;
156 135
157 counterService.increment(Constants.COUNTER_SERVER_PUSH_COUNT); 136 counterService.increment(Constants.COUNTER_SERVER_PUSH_COUNT);
158 137
159 - SendMsgResVO sendMsgResVO = new SendMsgResVO() ;  
160 - sendMsgResVO.setMsg("OK") ;  
161 res.setCode(StatusEnum.SUCCESS.getCode()) ; 138 res.setCode(StatusEnum.SUCCESS.getCode()) ;
162 res.setMessage(StatusEnum.SUCCESS.getMessage()) ; 139 res.setMessage(StatusEnum.SUCCESS.getMessage()) ;
163 - res.setDataBody(sendMsgResVO) ;  
164 return res ; 140 return res ;
165 } 141 }
166 } 142 }
  1 +package com.crossoverjie.netty.action.client.service;
  2 +
  3 +/**
  4 + * Function:
  5 + *
  6 + * @author crossoverJie
  7 + * Date: 2018/12/22 22:26
  8 + * @since JDK 1.8
  9 + */
  10 +public interface RouteRequest {
  11 +
  12 + /**
  13 + * 群发消息
  14 + * @param msg 消息
  15 + * @throws Exception
  16 + */
  17 + void sendGroupMsg(String msg) throws Exception;
  18 +}
  1 +package com.crossoverjie.netty.action.client.service.impl;
  2 +
  3 +import com.alibaba.fastjson.JSONObject;
  4 +import com.crossoverjie.netty.action.client.service.RouteRequest;
  5 +import okhttp3.*;
  6 +import org.springframework.beans.factory.annotation.Autowired;
  7 +import org.springframework.beans.factory.annotation.Value;
  8 +import org.springframework.stereotype.Service;
  9 +
  10 +import java.io.IOException;
  11 +
  12 +/**
  13 + * Function:
  14 + *
  15 + * @author crossoverJie
  16 + * Date: 2018/12/22 22:27
  17 + * @since JDK 1.8
  18 + */
  19 +@Service
  20 +public class RouteRequestImpl implements RouteRequest {
  21 +
  22 + @Autowired
  23 + private OkHttpClient okHttpClient ;
  24 +
  25 +
  26 + private MediaType mediaType = MediaType.parse("application/json");
  27 +
  28 + @Value("${cim.route.request.url}")
  29 + private String routeRequestUrl ;
  30 +
  31 + @Override
  32 + public void sendGroupMsg(String msg) throws Exception {
  33 +
  34 + JSONObject jsonObject = new JSONObject();
  35 + jsonObject.put("msg",msg);
  36 + RequestBody requestBody = RequestBody.create(mediaType,jsonObject.toString());
  37 +
  38 + Request request = new Request.Builder()
  39 + .url(routeRequestUrl)
  40 + .post(requestBody)
  41 + .build();
  42 +
  43 + Response response = okHttpClient.newCall(request).execute() ;
  44 + if (!response.isSuccessful()){
  45 + throw new IOException("Unexpected code " + response);
  46 + }
  47 + }
  48 +}
@@ -11,6 +11,9 @@ netty.server.port=11211 @@ -11,6 +11,9 @@ netty.server.port=11211
11 11
12 logging.level.root=info 12 logging.level.root=info
13 13
  14 +# 路由地址
  15 +cim.route.request.url=http://localhost:8083/groupRoute
  16 +
14 # 客户端唯一ID 17 # 客户端唯一ID
15 client.request.id=100 18 client.request.id=100
16 19