分享一下我老师大神的人工智能教程吧。零基础,通俗易懂!风趣幽默!http://www.captainbed.net/
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
说明:本文是个人在学习Feign模块在拜读官方资料时,突发奇想有种翻译的冲动,然后便成此文,仅作参考。
原文地址:http://cloud.spring.io/spring-cloud-static/Camden.SR1/#spring-cloud-feign
声明式REST客户端:Feign
Feign是一个声明式的Web服务客户端。它使得Web服务客户端的写入更加方便。
使用Feign创建一个界面并对其进行注释;
具有可插拔注释支持,包括Feign注释和JAX-RS注释;
支持可插拔编码器和解码器;
Spring Cloud增加了对Spring MVC注释的支持,并且使用了在Spring Web中默认使用的相同的HttpMessageConverter。Spring Cloud集成了Ribbon和Eureka,在使用Feign时提供了负载均衡的http客户端。
如何添加Feign
要在项目中添加Feign,请使用组org.springframework.cloud和artifact id spring-cloud-starter-feign的启动器。有关使用当前的Spring Cloud发布列设置构建系统的详细信息,请参阅Spring Cloud Project page
示例spring boot app
@Configuration@ComponentScan@EnableAutoConfiguration@EnableEurekaClient@EnableFeignClientspublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
StoreClinet.java
@FeignClient("stores")public interface StoreClient { @RequestMapping(method = RequestMethod.GET, value = "/stores") List<Store> getStores(); @RequestMapping(method = RequestMethod.POST, value = "/stores/{storeId}", consumes = "application/json") Store update(@PathVariable("storeId") Long storeId, Store store);}
在@FeignClient注释中,String值(以上“存储”)是一个任意客户端名称,用于创建功能区负载平衡器(有关Ribbon支持的详细信息,请参考下文)。您还可以使用url属性(绝对值或只是主机名)指定URL。应用程序上下文中的bean的名称是该接口的完全限定名称。还创建了一个别名,它是'name'属性加'FeignClient'。对于上面的实例,可以使用@Qualifier("storesFeignClient")来引用该bean.如果要更改默认的@Qualifier值,可以使用@FeignClient中的限定符值来完成此操作。
上面的Ribbon客户端将要发现“store”服务的物理地址。如果您的应用程序是Eureka客户端,那么它将解决Eureka服务注册表中的服务。如果您不想使用Eureka,您可以在外部配置中简单地配置服务列表。(例如,参见上文)
覆盖Feign默认值
@FeignClient(name = "stores", configuration = FooConfiguration.class)public interface StoreClient { //..}
在这种情况下,客户端由已在FeignClientsConfiguration中的组件以及FooConfiguration中的任何组件组成(后者将覆盖前者)。警告:
@FeignClient(name = "${feign.name}", url = "${feign.url}")public interface StoreClient { //..}
Spring Cloud Netflix默认为feign(BeanType beanName: ClassName):
@Configurationpublic class FooConfiguration { @Bean public Contract feignContract() { return new feign.Contract.Default(); } @Bean public BasicAuthRequestInterceptor basicAuthRequestInterceptor() { return new BasicAuthRequestInterceptor("user", "password"); }}
这将使用feign.Contract.Default替换SpringMvcContract,并将RequestInterceptor添加到RequestInterceptor的集合中。可以在@EnableFeignClients属性defaultConfiguration中以与上述相似的方式指定默认配置。不同之处在于,此配置将适用于所有feign客户端。
Feign Hystrix支持
@Configurationpublic class FooConfiguration { @Bean @Scope("prototype") public Feign.Builder feignBuilder() { return Feign.builder(); }}
Feign Hystrix回退
@FeignClient(name = "hello", fallback = HystrixClientFallback.class)protected interface HystrixClient { @RequestMapping(method = RequestMethod.GET, value = "/hello") Hello iFailSometimes();}static class HystrixClientFallback implements HystrixClient { @Override public Hello iFailSometimes() { return new Hello("fallback"); }}
如果需要访问导致回退触发器的原因,可以使用@FeignClient内的fallbackFactory属性。
@FeignClient(name = "hello", fallbackFactory = HystrixClientFallbackFactory.class)protected interface HystrixClient { @RequestMapping(method = RequestMethod.GET, value = "/hello") Hello iFailSometimes();}@Componentstatic class HystrixClientFallbackFactory implements FallbackFactory<HystrixClient> { @Override public HystrixClient create(Throwable cause) { return new HystrixClientWithFallBackFactory() { @Override public Hello iFailSometimes() { return new Hello("fallback; reason was: " + cause.getMessage()); } }; }}
警告:
public interface UserService { @RequestMapping(method = RequestMethod.GET, value ="/users/{id}") User getUser(@PathVariable("id") long id);}
UserResource.java
@RestControllerpublic class UserResource implements UserService {}
UserClient.java
package project.user;@FeignClient("users")public interface UserClient extends UserService {}
注意:
feign.compression.request.enabled=truefeign.compression.response.enabled=true
Feign请求压缩为您提供与您为Web服务器设置的类似的设置:
feign.compression.request.enabled=truefeign.compression.request.mime-types=text/xml,application/xml,application/jsonfeign.compression.request.min-request-size=2048
这些属性可以让您对压缩介质类型和最小请求阈值长度有选择性。
Feign日志记录
application.yml
logging.level.project.user.UserClient: DEBUG
您可以为每个客户端配置的Logger.Level对象告诉Feign记录多少。选择是:
@Configurationpublic class FooConfiguration { @Bean Logger.Level feignLoggerLevel() { return Logger.Level.FULL; }}