日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Feign-2覆写Feign的默认配置

發布時間:2024/4/13 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Feign-2覆写Feign的默认配置 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
現在我們來復寫Feign的默認配置7.2 Overriding Feign Defaultshttps://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-feign.htmlSpring Cloud的Feign支持的一個中心是命名客戶端,也就是我們可以為Client進行命名,每個FeignClient是整體的一部分,他們一起工作,按需聯系遠程服務器,并且該整體有一個名稱,開發人員可以使用FeignClient進行命名A central concept in Spring Cloud’s Feign support is that of the named client. Each feign client is part of an ensemble of components that work together to contact a remote server on demand,and the ensemble has a name that you give it as an application developer using the @FeignClient annotation. @FeignClient("microservice-simple-provider-user") public interface UserFeignClient {我們可以用這個進行命名org.springframework.cloud.netflix.feign.FeignClient/*** The service id with optional protocol prefix. Synonym for {@link #value() value}.*/ @AliasFor("value") String name() default "";name其實他是value的別名,Spring Cloud按照需要,使用FeignClientsConfiguration為每個命名的客戶端創建一個新的整體,FeignClientsConfiguration這個是FeignClient的默認配置類,/*** A custom <code>@Configuration</code> for the feign client. Can contain override* <code>@Bean</code> definition for the pieces that make up the client, for instance* {@link feign.codec.Decoder}, {@link feign.codec.Encoder}, {@link feign.Contract}.** @see FeignClientsConfiguration for the defaults*/ Class<?>[] configuration() default {};他默認就是FeignClientsConfiguration,為每一個命名的客戶端建立一個新的整體作為ApplicationContext,我們知道Spring他是一個父子容器,我們通過FeignClient其實創建了一個子容器,他包含了feign.Decoder,feign.Encoder,feign.Contract,包含了解碼器,編碼器,和契約,Spring Cloud creates a new ensemble as an ApplicationContext on demand for each named client using FeignClientsConfiguration. This contains (amongst other things) an feign.Decoder, a feign.Encoder,and a feign.Contract.你可以用額外的聲明,額外的配置,去完全控制Feign的配置Spring Cloud lets you take full control of the feign client by declaring additional configuration(on top of the FeignClientsConfiguration) using @FeignClient. Example:@FeignClient(name = "stores", configuration = FooConfiguration.class) public interface StoreClient {//.. }FooConfiguration does not need to be annotated with @Configuration. However, if it is, then take care to exclude it from any @ComponentScan that would otherwise include this configuration as it will become the default source for feign.Decoder, feign.Encoder, feign.Contract, etc., when specified. This can be avoided by putting it in a separate, non-overlapping package from any @ComponentScan or @SpringBootApplication, or it can be explicitly excluded in @ComponentScan.The serviceId attribute is now deprecated in favor of the name attribute.@FeignClient("microservice-simple-provider-user")你這里用了name就不要用id了,/*** The service id with optional protocol prefix. Synonym for {@link #value() value}.** @deprecated use {@link #name() name} instead*/ @Deprecated String serviceId() default "";以前你使用URL屬性,name不是必須的,但是現在他是必須的了,Previously, using the url attribute, did not require the name attribute. Using name is now required./*** An absolute URL or resolvable hostname (the protocol is optional).*/ String url() default "";Placeholders are supported in the name and url attributes.@FeignClient(name = "${feign.name}", url = "${feign.url}") public interface StoreClient {//.. }我這邊只寫url,沒有寫name,他還是不報錯的,啟動的時候就啟動不來Spring Cloud Netflix provides the following beans by default for feign (BeanType beanName: ClassName):Decoder feignDecoder: ResponseEntityDecoder (which wraps a SpringDecoder)Encoder feignEncoder: SpringEncoderLogger feignLogger: Slf4jLoggerContract feignContract: SpringMvcContractFeign.Builder feignBuilder: HystrixFeign.BuilderClient feignClient: if Ribbon is enabled it is a LoadBalancerFeignClient, otherwise the default feign client is used.Spring Cloud Netflix項目呢,使用這些作為默認配置,解碼器用的是ResponseEntityDecoder這個解碼器,編碼器用的是SpringEncoder這個編碼器,Logger用的是Slf4jLogger這個Log,Contract用的是SpringMvcContract契約,所以他才可以使用SpringMVC的注解,如果你使用Ribbon的話,然后你還可以使用OkHttpClient,這也是一個非常火的一個項目,The OkHttpClient and ApacheHttpClient feign clients can be used by setting feign.okhttp.enabled or feign.httpclient.enabled to true, 下面是Spring Cloud沒有進行默認配置的Spring Cloud Netflix does not provide the following beans by default for feign, but still looks up beans of these types from the application context to create the feign client:Logger.LevelRetryerErrorDecoderRequest.OptionsCollection<RequestInterceptor>SetterFactoryhttps://github.com/OpenFeign/feigninterface GitHub {@RequestLine("GET /repos/{owner}/{repo}/contributors")List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo);@RequestLine("POST /repos/{owner}/{repo}/issues")void createIssue(Issue issue, @Param("owner") String owner, @Param("repo") String repo);}localhost:8010/movie/1 <?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>microservice-consumer-movie-feign</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>microservice-simple-consumer-movie</name><description>Demo project for Spring Boot</description><parent><groupId>cn.learn</groupId><artifactId>microcloud02</artifactId><version>0.0.1</version></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-feign</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project> #debug=true server.port=8010eureka.client.serviceUrl.defaultZone=http://admin:1234@10.40.8.152:8761/eurekaspring.application.name=microservice-consumer-movie-ribbon eureka.instance.prefer-ip-address=true eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}} eureka.client.healthcheck.enabled=true spring.redis.host=10.40.8.152 spring.redis.password=1234 spring.redis.port=6379#stores.ribbon.listOfServers=10.40.8.144:7900 package com.learn.config;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;import feign.Contract; import feign.Logger;@Configuration public class Configuration1 {@Beanpublic Contract feignContract() {return new feign.Contract.Default();}@BeanLogger.Level feignLoggerLevel() {return Logger.Level.FULL;} } package com.learn.cloud.feign;import org.springframework.cloud.netflix.feign.FeignClient;import com.learn.cloud.entity.User; import com.learn.config.Configuration1;import feign.Param; import feign.RequestLine;@FeignClient(name = "microservice-simple-provider-user", configuration = Configuration1.class) public interface UserFeignClient {@RequestLine("GET /simple/{id}")public User findById(@Param("id") Long id);} package com.learn.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.learn.cloud.entity.User; import com.learn.cloud.feign.UserFeignClient;@RestController public class MovieController {@Autowiredprivate UserFeignClient userFeignClient;@GetMapping("/movie/{id}")public User findById(@PathVariable Long id) {return this.userFeignClient.findById(id);}} package com.learn.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 ConsumerMovieFeignApplication {public static void main(String[] args) {SpringApplication.run(ConsumerMovieFeignApplication.class, args);} }

?

總結

以上是生活随笔為你收集整理的Feign-2覆写Feign的默认配置的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。