聚合服务,或者叫做数据聚合服务,顾名思义就是把所需要的服务给聚集在一起,这个概念在现实中非常重要,因为聚合服务做得好,能尽可能的减少服务治理的问题。
本篇中,我们将会采用resttemplate+ribbon来进行构建聚合服务。
首先是在pom文件中引入我们需要的文件,以下两个文件默认zuul已经存在,但是我们重新引入,容易看一些。
pom文件新增
<!-- 负载均衡器 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<!-- springboot核心web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
接下来,我们新建一个resttemplate的配置类
RestTemplateConfig配置类
/**
* All rights Reserved, Designed By OprCalf
* Copyright: Copyright(C) 2016-2020
* Company LengYin Ltd.
*/
package com.platform.gateway.common.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
/**
* @projectName: widget-web-request
* @package: com.widget.request.config
* @className: RestTemplateConfig.java
* @description: resttemplate配置
* @author: OprCalf
* @date: 2019年1月29日
*/
@Configuration
public class RestTemplateConfig {
@Bean(name = "balanceTemplate")
@LoadBalanced
public RestTemplate balanceTemplate() {
final RestTemplate restTemplate = new RestTemplate(new OkHttp3ClientHttpRequestFactory());
return restTemplate;
}
@Bean(name = "singleTemplate")
public RestTemplate singleTemplate() {
final RestTemplate restTemplate = new RestTemplate(new OkHttp3ClientHttpRequestFactory());
return restTemplate;
}
}
在这个配置类中,singleTemplate代表这请求外部接口,不走eureka,balanceTemplate代表着走eureka,进行负载均衡访问后端原子服务。
接下来,新增请求类Request,用于向后端请求数据
Request类:
Request
新增Get方式的请求类GetRequest,继承Request
GetRequest类:
/**
* All rights Reserved, Designed By OprCalf
* Copyright: Copyright(C) 2016-2020
* Company LengYin Ltd.
*/
package com.platform.gateway.common.request;
import javax.annotation.Resource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.alibaba.fastjson.JSONObject;
/**
* @projectName: platform-gateway
* @package: com.platform.gateway.common.request
* @className: GetRequest.java
* @description: Get请求
* @author: OprCalf
* @date: 2020年3月27日
*/
@Service
@SuppressWarnings("deprecation")
public class GetRequest extends Request {
@Resource(name = "balanceTemplate")
private RestTemplate balanceTemplate;
@Resource(name = "singleTemplate")
private RestTemplate singleTemplate;
/**
* 发送请求到服务端,初始化head里面的user为空
* @author OprCalf
* @param serviceAdd
* @param servicePath
* @param params
* @return JSONObject
*/
public JSONObject getNullUserForJson(String serviceAdd, String servicePath) {
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
final HttpEntity<Object> requestEntity = new HttpEntity<>("", headers);
final ResponseEntity<JSONObject> response = balanceTemplate.exchange(serviceAdd + servicePath, HttpMethod.GET,
requestEntity,
JSONObject.class);
return JSONObject.parseObject(JSONObject.toJSONString(response.getBody(), Request.filter));
}
}
到此配置完成,我们在网关上新建一个接口,来模拟请求后端服务
AggController聚合接口:
/**
* All rights Reserved, Designed By OprCalf
* Copyright: Copyright(C) 2016-2020
* Company LengYin Ltd.
*/
package com.platform.gateway.agg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.platform.gateway.common.message.ResponseMsg;
import com.platform.gateway.common.request.GetRequest;
import com.platform.gateway.common.utils.MsgUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
/**
* @projectName: platform-gateway
* @package: com.platform.gateway.agg
* @className: AggController.java
* @description: 聚合接口
* @author: OprCalf
* @date: 2020年3月27日
*/
@Api(tags = { "聚合服务:聚合接口" })
@RestController
@RequestMapping("agg/test")
@SuppressWarnings("deprecation")
public class AggController {
@Autowired
private GetRequest getRequest;
@ApiOperation(value = "聚合服务测试接口")
@GetMapping(value = "getTest", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseMsg<JSONObject> getTest() {
return MsgUtils.buildSuccessMsg(getRequest.getNullUserForJson("http://ATOMIC-DATAS/", "/web/demo/user/test"));
}
}
最后,我们直接访问接口:
http://localhost:8012/agg/test/getTest
聚合接口访问后端服务
直接访问后端接口拿到数据,此时我们就可以重新组装,或者直接访问,自定根据需要来执行即可。
总结:本篇中我们采用resttemplate+ribbon进行后端接口的访问,也可以feign进行操作,不作限制,本质上是一样。
最后,谢谢观赏,觉得好的话,点个赞,有什么问题可以留言沟通,么么哒。