【SpringCloud】(九):Feign的自定義配置
上篇文章介紹了Feign以及它的基本使用,下面我們來完成Feign的定義配置,可以類比Ribbon的自定義的配置方式;
配置中也存在@Configuration和@ComponentScan衝突,不要把Configuration的類配置在@ComponentScan能掃描的路徑
根據文件內容來自定義Feign;
1.啟動類
package com.dynamic.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class ComsumerMovieFeignApplication {
public static void main(String[] args) {
SpringApplication.run(ComsumerMovieFeignApplication.class, args);
}
}
2.Controller
package com.dynamic.cloud.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.dynamic.cloud.entity.User;
import com.dynamic.cloud.feign.FeignClient2;
import com.dynamic.cloud.feign.UserFeignClient;
@RestController
public class MovieController {
@Autowired
private UserFeignClient userFeignClient;
@Autowired
private FeignClient2 feignClient2;
@GetMapping("/movie/{id}")
public User findById(@PathVariable Long id)
{
return this.userFeignClient.findById(id);
}
@GetMapping("/{serviceName}")
public String findServiceInfoFromEurekaByServiceName(@PathVariable("serviceName")String serviceName)
{
return this.feignClient2.findServiceInfoFromEurekaByServiceName(serviceName);
}
}
Controller中注入了兩個FeignClient ,分別配置不同的@Configuration。
一個是UserFeignClient:呼叫使用者微服務。
一個是FeignClient2:通過傳入使用者微服務的ServicId 從Eureka中獲取資訊。
UserFeignClient
package com.dynamic.cloud.feign;
import org.springframework.cloud.netflix.feign.FeignClient;
import com.dynamic.cloud.entity.User;
import com.dynamic.config.FooConfiguration;
import feign.Param;
import feign.RequestLine;
@FeignClient(name="microservice-provider-user",configuration = FooConfiguration.class)
public interface UserFeignClient {
@RequestLine("GET /simple/{id}")
public User findById(@Param("id") Long id);
}
配置類:FooConfiguration
package com.dynamic.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import feign.Contract;
import feign.Logger;
@Configuration
public class FooConfiguration {
@Bean
public Contract feignContract() {
return new feign.Contract.Default();
}
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}
Logger.Level.FULL設定列印日誌的級別
FeignClient2
package com.dynamic.cloud.feign;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.dynamic.config.AuthConfiguration;
@FeignClient(name="xxx",url="http://localhost:8761/",configuration=AuthConfiguration.class)
public interface FeignClient2 {
@RequestMapping(value="/eureka/apps/{serviceName}")
public String findServiceInfoFromEurekaByServiceName(@PathVariable("serviceName") String serviceName);
}
配置類:AuthConfiguration
設定 Eureka Server的訪問的使用者名稱和密碼
package com.dynamic.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import feign.auth.BasicAuthRequestInterceptor;
@Configuration
public class AuthConfiguration {
@Bean
public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
return new BasicAuthRequestInterceptor("user","pass123");
}
}
相關文章
- SpringCloud Feign的分析SpringGCCloud
- 【SpringCloud】Ribbon如何自定義客戶端配置和全域性配置SpringGCCloud客戶端
- 【SpringCloud】(七):自定義RibbonSpringGCCloud
- Springcloud(二) feignSpringGCCloud
- SpringCloud-FeignSpringGCCloud
- Feign通過自定義註解實現路徑的轉義
- java B2B2C Springcloud電子商務平臺原始碼-自定義feign配置與服務呼叫的安全驗證JavaSpringGCCloud原始碼
- 自定義九宮格載入的實現
- Spring Cloud Feign 自定義配置(重試、攔截與錯誤碼處理) 實踐SpringCloud
- 【SpringCloud】(十四):Feign對Hystrix的支援 fallbackFactorySpringGCCloud
- SpringCloud——Feign例項及原理SpringGCCloud
- php 自定義配置檔案PHP
- 打包策略 自定義打包配置
- 分享一個 SpringCloud Feign 中所埋藏的坑SpringGCCloud
- 【SpringCloud】(八):認識Feign及使用SpringGCCloud
- Laravel 自定義配置資訊的儲存方式Laravel
- 【ASP.NET Core】自定義的配置源ASP.NET
- springcloud(九):配置中心和訊息匯流排(配置中心終結版)SpringGCCloud
- (20)SpringCloud-Ribbon自定義負載均衡策略SpringGCCloud負載
- springcloud之自定義簡易消費服務元件SpringGCCloud元件
- samba 基本配置及自定義控制Samba
- docker官方mysql映象自定義配置DockerMySql
- C#建立自定義配置節C#
- 利用Apache配置本地 自定義域名Apache
- SpringCloud微服務治理二(Robbin,Hystix,Feign)SpringGCCloud微服務
- SpringCloud-使用Feign呼叫服務介面SpringGCCloud
- struts的常用配置以及自定義攔截器
- 如何自定義終端顯示配置
- Yii自定義配置檔案存放方法
- springcloud中feign檔案上傳、下載SpringGCCloud
- (22)SpringCloud-使用Feign呼叫服務介面SpringGCCloud
- SpringCloud 2020.0.4 系列之 FeignSpringGCCloud
- docker Redis 自定義配置檔案啟動DockerRedis
- _008_SpringBoot_自定義配置Spring Boot
- html-webpack-plugin之配置自定義模板HTMLWebPlugin
- 快速自定義Cordova外掛(-配置檔案)
- 使用配置檔案自定義AWS Elastic BeanstalkASTBean
- SpringCloud系列之使用Feign進行服務呼叫SpringGCCloud