聚合服務,或者叫做數據聚合服務,顧名思義就是把所需要的服務給聚集在一起,這個概念在現實中非常重要,因爲聚合服務做得好,能儘可能的減少服務治理的問題。
本篇中,我們將會採用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進行操作,不作限制,本質上是一樣。
最後,謝謝觀賞,覺得好的話,點個贊,有什麼問題可以留言溝通,麼麼噠。