作者 crossoverJie

:sparkles: Introducing new features httpclient

package com.crossoverjie.netty.action.client.config;
import com.crossoverjie.netty.action.common.protocol.BaseRequestProto;
import okhttp3.OkHttpClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.TimeUnit;
/**
* Function:构建心跳使用的 bean
* Function:bean 配置
*
* @author crossoverJie
* Date: 24/05/2018 15:55
* @since JDK 1.8
*/
@Configuration
public class HeartBeatConfig {
public class BeanConfig {
@Value("${client.request.id}")
private int requestId;
/**
* 创建心跳单例
* @return
*/
@Bean(value = "heartBeat")
public BaseRequestProto.RequestProtocol heartBeat() {
BaseRequestProto.RequestProtocol heart = BaseRequestProto.RequestProtocol.newBuilder()
.setRequestId(requestId)
.setReqMsg("ping")
.build();
return heart;
}
/**
* http client
* @return okHttp
*/
@Bean
public OkHttpClient okHttpClient() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10,TimeUnit.SECONDS)
.retryOnConnectionFailure(true);
return builder.build();
}
}
... ...
package com.crossoverjie.netty.action.client.controller;
import com.crossoverjie.netty.action.client.HeartbeatClient;
import com.crossoverjie.netty.action.client.service.RouteRequest;
import com.crossoverjie.netty.action.client.vo.req.GoogleProtocolVO;
import com.crossoverjie.netty.action.client.vo.req.SendMsgReqVO;
import com.crossoverjie.netty.action.client.vo.req.StringReqVO;
... ... @@ -11,10 +12,6 @@ import com.crossoverjie.netty.action.common.pojo.CustomProtocol;
import com.crossoverjie.netty.action.common.res.BaseResponse;
import com.crossoverjie.netty.action.common.res.NULLBody;
import io.swagger.annotations.ApiOperation;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.metrics.CounterService;
import org.springframework.stereotype.Controller;
... ... @@ -23,8 +20,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.IOException;
/**
* Function:
*
... ... @@ -45,6 +40,11 @@ public class IndexController {
@Autowired
private HeartbeatClient heartbeatClient ;
@Autowired
private RouteRequest routeRequest ;
/**
* 向服务端发消息
* @param sendMsgReqVO
... ... @@ -128,39 +128,15 @@ public class IndexController {
@ApiOperation("群发消息")
@RequestMapping(value = "sendGroupMsg",method = RequestMethod.POST)
@ResponseBody
public BaseResponse<SendMsgResVO> sendGroupMsg(@RequestBody SendMsgReqVO sendMsgReqVO) throws IOException {
BaseResponse<SendMsgResVO> res = new BaseResponse();
OkHttpClient client = new OkHttpClient();
MediaType MEDIA_TYPE_MARKDOWN
= MediaType.parse("text/x-markdown; charset=utf-8");
String postBody = ""
+ "Releases\n"
+ "--------\n"
+ "\n"
+ " * _1.0_ May 6, 2013\n"
+ " * _1.1_ June 15, 2013\n"
+ " * _1.2_ August 11, 2013\n";
Request request = new Request.Builder()
.url("https://api.github.com/markdown/raw")
.post(okhttp3.RequestBody.create(MEDIA_TYPE_MARKDOWN, postBody))
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()){
throw new IOException("Unexpected code " + response);
}
public BaseResponse sendGroupMsg(@RequestBody SendMsgReqVO sendMsgReqVO) throws Exception {
BaseResponse<NULLBody> res = new BaseResponse();
System.out.println(response.body().string());
routeRequest.sendGroupMsg(sendMsgReqVO.getMsg()) ;
counterService.increment(Constants.COUNTER_SERVER_PUSH_COUNT);
SendMsgResVO sendMsgResVO = new SendMsgResVO() ;
sendMsgResVO.setMsg("OK") ;
res.setCode(StatusEnum.SUCCESS.getCode()) ;
res.setMessage(StatusEnum.SUCCESS.getMessage()) ;
res.setDataBody(sendMsgResVO) ;
return res ;
}
}
... ...
package com.crossoverjie.netty.action.client.service;
/**
* Function:
*
* @author crossoverJie
* Date: 2018/12/22 22:26
* @since JDK 1.8
*/
public interface RouteRequest {
/**
* 群发消息
* @param msg 消息
* @throws Exception
*/
void sendGroupMsg(String msg) throws Exception;
}
... ...
package com.crossoverjie.netty.action.client.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.crossoverjie.netty.action.client.service.RouteRequest;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.IOException;
/**
* Function:
*
* @author crossoverJie
* Date: 2018/12/22 22:27
* @since JDK 1.8
*/
@Service
public class RouteRequestImpl implements RouteRequest {
@Autowired
private OkHttpClient okHttpClient ;
private MediaType mediaType = MediaType.parse("application/json");
@Value("${cim.route.request.url}")
private String routeRequestUrl ;
@Override
public void sendGroupMsg(String msg) throws Exception {
JSONObject jsonObject = new JSONObject();
jsonObject.put("msg",msg);
RequestBody requestBody = RequestBody.create(mediaType,jsonObject.toString());
Request request = new Request.Builder()
.url(routeRequestUrl)
.post(requestBody)
.build();
Response response = okHttpClient.newCall(request).execute() ;
if (!response.isSuccessful()){
throw new IOException("Unexpected code " + response);
}
}
}
... ...
... ... @@ -11,6 +11,9 @@ netty.server.port=11211
logging.level.root=info
# 路由地址
cim.route.request.url=http://localhost:8083/groupRoute
# 客户端唯一ID
client.request.id=100
... ...