javascript
自定义报错返回_Spring Cloud Feign的使用和自定义配置
在上一篇文章
null:Spring Cloud 自定義Eureka Ribbon負載均衡策略?zhuanlan.zhihu.com中,我們使用Ribbon自定義的策略實現了負載均衡,接下來我們介紹下Spring Cloud的另一種服務調用方式Feign。
0. 開發環境
- IDE:IntelliJ IDEA 2017.1 x64
- jdk:1.8.0_91
- Spring Boot:2.0.9.RELEASE
- Spring Cloud:Finchley.RELEASE
1. Feign簡介
Feign是一個聲明式的偽Http Web服務客戶端,它使得寫Http客戶端變得非常簡單。使用Feign,只需創建一個接口并注解,就可實現服務間調用。它具有可插拔的注解特性,支持使用Feign注解、JAX-RS注解、Spring MVC注解。Feign默認集成了Ribbon,并結合了Eureka,默認實現了負載均衡。
2. 新建Feign服務消費者
2.1 新建Feign服務消費者
2.2 引入依賴
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><artifactId>spring-boot-consumer-feign</artifactId><packaging>jar</packaging><parent><artifactId>spring-cloud-demo</artifactId><groupId>cn.wbnull</groupId><version>1.0.0</version></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency></dependencies> </project>2.3 新建application.yml
server:port: 8084servlet:context-path: /springbootconsumerspring:application:name: spring-boot-consumer-feigneureka:client:register-with-eureka: falsefetch-registry: trueservice-url:defaultZone: http://localhost:8090/springcloudeureka/eureka/2.4 新建Spring Boot啟動類
這里要注意,啟動類需增加類注解@EnableFeignClients,表示此項目為Feign客戶端
package cn.wbnull.springbootconsumer;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication @EnableEurekaClient @EnableFeignClients public class SpringBootConsumerFeignApplication {public static void main(String[] args) {SpringApplication.run(SpringBootConsumerFeignApplication.class, args);} }2.5 定義Feign接口
cn.wbnull.springbootconsumer包下新建feign包,再新建GatewayFeignClient接口類。
- @FeignClient("服務id名稱"),表示調用哪個服務
- @GetMapping(value = "接口地址"),表示調用哪個接口。這里要注意接口地址要保留server.servlet.context-path部分,沒有則省略
2.6 新建控制器類
cn.wbnull.springbootconsumer包下新建controller包,再新建GatewayController類。
package cn.wbnull.springbootconsumer.controller;import cn.wbnull.springbootconsumer.feign.GatewayFeignClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;@RestController @Scope("prototype") public class GatewayController {@Autowiredprivate GatewayFeignClient gatewayFeignClient;@GetMapping(value = "/gateway")public String gateway() throws Exception {return gatewayFeignClient.gateway();} }這里@Autowired GatewayFeignClient可能會有報錯,是因為這個Bean是程序啟動時注入的,現在編譯器還無法裝配,可以不用管,啟動程序不會報錯。但是如果看著報錯不順眼非要去掉,可以在剛才的GatewayFeignClient接口類增加@Component注解。
2.7 測試
依次啟動spring-cloud-eureka,spring-boot-provider,spring-boot-provider-v2,spring-boot-consumer-feign。然后瀏覽器訪問http://127.0.0.1:8084/springbootconsumer/gateway,不斷刷新,可以到到返回信息正常,且 hello world,this is spring-boot-provider 和 hello world,this is spring-boot-provider-v2 交替出現。
2.8 增加傳參接口
剛才我們服務消費者調用服務提供者的接口,都是沒有參數的,下面我們新建一個帶參數的接口進行測試。
2.8.1 修改spring-boot-provider
spring-boot-provider服務GatewayController類增加如下代碼
@PostMapping(value = "/user")public String user(@RequestParam(value = "name") String name) throws Exception {return "hello world,this is spring-boot-provider. name is " + name;}2.8.2 修改spring-boot-provider-v2
spring-boot-provider-v2服務GatewayController類增加如下代碼
@PostMapping(value = "/user")public String user(@RequestParam(value = "name") String name) throws Exception {return "hello world,this is spring-boot-provider-v2. name is " + name;}2.8.3修改spring-boot-consumer-feign
1、spring-boot-consumer-feign服務GatewayFeignClient類增加如下代碼
@PostMapping(value = "/springbootprovider/user")String user(@RequestParam(value = "name") String name) throws Exception;2、spring-boot-consumer-feign服務GatewayController類增加如下代碼
@PostMapping(value = "/user")public String user(@RequestParam(value = "name") String name) throws Exception {return gatewayFeignClient.user(name);}2.8.4 測試
依次啟動spring-cloud-eureka,spring-boot-provider,spring-boot-provider-v2,spring-boot-consumer-feign。然后打開Postman,配置如下,不斷點擊Send按鈕,可以看到返回信息正常,且 hello world,this is spring-boot-provider. name is 測試name 和 hello world,this is spring-boot-provider-v2. name is 測試name 交替出現。
3. 自定義Feign配置
上篇我們自定義Ribbon負載均衡策略時,會出現包掃描的問題,Feign自定義配置也同樣存在這個問題,這里我們不再贅述,直接使用注解 @ComponentScan 自定義掃描類。但是與上篇不同,我們這里自定義一個注解,當Java類使用了該注解時,@ComponentScan就會忽略掃描。
cn.wbnull.springbootconsumer包下新建config包,再新建ComponentScanExclude注解類
package cn.wbnull.springbootconsumer.config;public @interface ComponentScanExclude { }3.1 自定義配置類
config包下新建GlobalConfiguration類
package cn.wbnull.springbootconsumer.config;import feign.Contract; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;@Configuration @ComponentScanExclude public class GlobalConfiguration {@Beanpublic Contract GlobalContract() {return new Contract.Default();} }3.1.1 Contract
我們可以看到 GlobalConfiguration 類的 GlobalContract() 方法返回了一個 Contract 對象,Contract 表示契約,說白了,就是一個注解解釋器。前面我們說過,Feign支持使用Feign注解、JAX-RS注解、Spring MVC注解。Contract 可以幫助我們將這些注解解釋成Feign能夠理解的Http請求。我們也可以通過自定義一個Contract 解釋器來實現我們自己的注解,這個放到后面介紹,本篇不做贅述。
Feign一開始默認使用的契約是Spring MVC,所以我們前面測試使用的都是Spring MVC注解。但現在我們return new Contract.Default(),Contract.Default()默認使用的契約是Feign,所以我們也對應的要修改成Feign注解。
3.2 加載自定義配置
修改GatewayFeignClient類,@FeignClient 注解引入自定義配置類,并且將接口注解修改成Feign注解
package cn.wbnull.springbootconsumer.feign;import cn.wbnull.springbootconsumer.config.GlobalConfiguration; import feign.Param; import feign.RequestLine; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Component;@Component @FeignClient(value = "spring-boot-provider", configuration = GlobalConfiguration.class) public interface GatewayFeignClient {@RequestLine("GET /springbootprovider/gateway")String gateway() throws Exception;@RequestLine("POST /springbootprovider/user?name={name}")String user(@Param("name") String name) throws Exception; }3.3 測試
依次啟動spring-cloud-eureka,spring-boot-provider,spring-boot-provider-v2,spring-boot-consumer-feign。然后瀏覽器訪問http://127.0.0.1:8084/springbootconsumer/gateway,不斷刷新,跟剛才測試結果相同;Postman配置跟剛才一樣,不斷點擊Send,跟剛才測試結果相同。
3.4 增加application/json方式傳參接口
下面我們再新建一個application/json方式傳參的接口進行測試。
3.4.1 修改spring-boot-provider
spring-boot-provider服務GatewayController類增加如下代碼
@PostMapping(value = "/users")public Map<String, String> users(@RequestBody Map<String, String> request) throws Exception {request.put("hello world", "spring-boot-provider");return request;}3.4.2 修改spring-boot-provider-v2
spring-boot-provider-v2服務GatewayController類增加如下代碼
@PostMapping(value = "/users")public Map<String, String> users(@RequestBody Map<String, String> request) throws Exception {request.put("hello world", "spring-boot-provider-v2");return request;}3.4.3修改spring-boot-consumer-feign
1、spring-boot-consumer-feign服務GatewayFeignClient類增加如下代碼
@RequestLine("POST /springbootprovider/users")@Headers("Content-Type: application/json")Map<String, String> users(Map<String, String> request) throws Exception;2、spring-boot-consumer-feign服務GatewayController類增加如下代碼
@PostMapping(value = "/users")public Map<String, String> users(@RequestBody Map<String, String> request) throws Exception {return gatewayFeignClient.users(request);}2.8.4 測試
依次啟動spring-cloud-eureka,spring-boot-provider,spring-boot-provider-v2,spring-boot-consumer-feign。然后打開Postman,配置如下,不斷點擊Send按鈕,可以看到返回信息正常,且兩組返回信息交替出現。
GitHub:
dkbnull/SpringCloudDemo?github.comCSDN:
https://blog.csdn.net/dkbnull/article/details/89525792?blog.csdn.net微信:
Spring Cloud Feign的使用和自定義配置?mp.weixin.qq.com微博:
Spring Cloud Feign的使用和自定義配置?weibo.com總結
以上是生活随笔為你收集整理的自定义报错返回_Spring Cloud Feign的使用和自定义配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ajax接口一直在重复调用请求是什么原因
- 下一篇: aaynctask控制多个下载进度_史上