2018-06-24

Feign--学习笔记(3)



目录

一、参考spring cloud的官方文档
--1、 声明性REST客户端:Feign
--2、如何加入Feign
--3、覆盖Feign默认值
二、实操
--1、在这个示例中有三个角色:注册中心、服务提供方、服务消费方
--2、消费方是如何配置的
--3、启动结果
--4、原理图



一、参考spring cloud的官方文档


1、 声明性REST客户端:Feign

reign简介.PNG

Feign是一个声明式的Web服务客户端。这使得Web服务客户端的写入更加方便 要使用Feign创建一个界面并对其进行注释。它具有可插入注释支持,包括Feign注释和JAX-RS注释。Feign还支持可插拔编码器和解码器。Spring Cloud增加了对Spring MVC注释的支持,并使用Spring Web中默认使用的HttpMessageConverters。Spring Cloud集成Ribbon和Eureka以在使用Feign时提供负载均衡的http客户端。

2、如何加入Feign

要在您的项目中包含Feign,请使用组org.springframework.cloud和工件ID spring-cloud-starter-feign的启动器。有关 使用当前的Spring Cloud发布列表设置构建系统的详细信息。

示例spring boot应用

@Configuration
@ComponentScan
@EnableAutoConfiguration
@EnableEurekaClient
@EnableFeignClients
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

StoreClient.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的名称是该接口的完全限定名称。要指定您自己的别名值,您可以使用@FeignClient注释的qualifier值。

以上的Ribbon客户端将会发现“商店”服务的物理地址。如果您的应用程序是Eureka客户端,那么它将解析Eureka服务注册表中的服务。

3、覆盖Feign默认值

Spring Cloud的Feign支持的中心概念是指定的客户端。每个假装客户端都是组合的组件的一部分,它们一起工作以根据需要联系远程服务器,并且该集合具有您将其作为应用程序开发人员使用@FeignClient注释的名称。Spring Cloud根据需要,使用FeignClientsConfiguration为每个已命名的客户端创建一个新的集合ApplicationContext。这包含(除其他外)feign.Decoderfeign.Encoderfeign.Contract

Spring Cloud可以通过使用@FeignClient声明额外的配置(FeignClientsConfiguration)来完全控制假客户端。例:

@FeignClient(name = "stores", configuration = FooConfiguration.class)
public interface StoreClient {
    //..
}

在这种情况下,客户端由FeignClientsConfiguration中的组件与FooConfiguration中的任何组件组成(后者将覆盖前者)。

application.yml

feign:
    client:
        config:
            connectTimeout: 5000
            readTimeout: 5000
            loggerLevel: full
            errorDecoder: com.example.SimpleErrorDecoder
            retryer: com.example.SimpleRetryer
            requestInterceptors:
                - com.exaple.fooRequestInterceptor
                - com.example.BarRequestInterceptor
            decode404: false

二、实操


1、在这个示例中有三个角色:注册中心、服务提供方、服务消费方

生产者-注册中心-消费者.PNG

2、消费方是如何配置的

pom.xml

        <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>

application.yml

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

server:
  port: 8083

spring:
  application:
    name: feign-demo-consumer

启动类

@SpringBootApplication
@EnableFeignClients
public class FeignDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(FeignDemoApplication.class, args);
    }
}

自定义的FeignClient接口

@FeignClient("eureka-demo-client-1")
public interface HelloClient {
    @GetMapping("/")
    String sayHello();
}

调用类:

@RestController
public class HelloController {
    @Autowired
    private HelloClient helloClient;

    @GetMapping("/hello")
    public String hello(){
        return helloClient.sayHello();
    }
}

3、启动结果

在UI界面上可以看到

监控台.PNG

在浏览器中输入http://localhost:8083/hello

result.PNG

4、原理图

发布订阅.PNG
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。