[java]微服务架构连载No5 服务通讯桥梁Feign代理

Feign:是一套基于Netfix Feign实现的声明式服务调用客户端,是的编写web服务客服端更加简单,只需通过接口并用注解配置即可完成Web服务接口的绑定,具备可插拔的注解支持

使用

工程 spring-cloud-04-feign-consumer (服务调用方)

spring-cloud-04-feign-provider (服务发布方)

spring-cloud-04-feign-eureka (服务注册中心)


spring-cloud-04-feign-eureka (服务注册中心)

代码略,同上篇文章


spring-cloud-04-feign-provider (服务发布方)

@RestController
public class FeignController {

    @GetMapping("/hello")
    public String hello() throws InterruptedException {
        System.err.println("---hello feigns --");
        return "--- hello feign --";
    }

    @GetMapping("/hi")
    public String hi() throws InterruptedException {
        System.err.println("---hi feign --");

        Thread.sleep(3000);
        return "--- hi feign --";
    }
}

备注: 发布两个服务 /hello / hi


spring:
  application:
    name: feign-provider

server:
  port: 7001
  context-path: /

eureka:
  client:
    service-url:
     defaultZone: http://localhost:8001/eureka/

备注:端口 7001


spring-cloud-04-feign-consumer (服务调用方)


<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
备注: 导入feign jar包




spring:
  application:
    name: feign-consumer

server:
  port: 7002
  context-path: /

eureka:
  client:
    service-url:
     defaultZone: http://localhost:8001/eureka/

#启动断路器超时时间
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 15000

#使用 feign 不需要自定义超时时间
#custom:
#   rest:
#    connect-timeout: 1000
#    connection-request-timeout: 1000
#    read-timeout: 30000

#使用feign自定义超时时间
feign-provider:
  ribbon:
    ConnectTimeout: 250  #连接超时时间
    ReadTimeout: 1000   #处理超时时间
    OkToRetryOnAllOperations: true # 是否对所有请求都进行重试
    MaxAutoRetriesNextServer: 1   #重试切换实例次数
    MaxAutoRetries: 3     #重试次数

## feign的配置项
feign:
  #开启feign的断路器功能
  hystrix:
    enabled: true
  compression:
    request:
      min-request-size: 2048
      mime-types:
        - text/xml,application/xml, application/json
    response:
      enabled: true

备注: 1: 端口7002

          2:feign.hystrix.enabled 开启feign的断路器功能

         3:使用feign自定义超时时间, feign-provider.ribbon 服务发布者


@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
@EnableFeignClients  //开启代理功能
public class ConsumerApplication {

    //使用 feign 不需要自定义超时时间
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

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

备注: 通过@EnableFeignClients 开启服务代理


@FeignClient(name="feign-provider",fallback = HelloFeignClientHystrixFallback.class )
public interface HelloFeignClient {

    @GetMapping("/hello")
    String hello() ;

    @GetMapping("/hi")
    String hi() ;

}
备注:  @FeignClient 设置代理服务地址feign-provider

   fallback 设置服务失败(异常,超时...)降级策略实现类HelloFeignClientHystrixFallback

           @GetMapping value值要和服务发布地址一样,hello , hi 两个服务名


// 注意,这里要写component
@Component
public class HelloFeignClientHystrixFallback implements HelloFeignClient{
    @Override
    public String hello() {
        return "-----hello hystrix -------";
    }

    @Override
    public String hi() {
        return "-----hi  hystrix -------";
    }
}

备注:  @Component声明该类为spirng容器管理

           implements HelloFeignClient 实现接口,自动以降级方法


@RestController
public class HelloController {

    @Autowired
    private HelloFeignClient helloFeignClient;

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

    @GetMapping("/hi")
    public String hi(){
        return helloFeignClient.hi();
    }
}
备注: rest服务


使用说明:

       1:发布了两个服务,/hello和 /hi ,其中 hi睡眠了3秒

        2:设置服务超时时间1秒,超时重试次数3次,并且切换实例一次

        3: 定义服务代理接口HelloFeignClient ,和失败策略实现类,返回,

截图一: 服务注册中心

截图二: 调用hello, 服务正产返回

截图三:调用服务 hi, 返回调用超时失败,进行连接一次+重试3次+ 切换实例一次+重试3次,断路器超时,返回


[java]微服务架构连载No5 服务通讯桥梁Feign代理


截图二:

[java]微服务架构连载No5 服务通讯桥梁Feign代理


截图三

[java]微服务架构连载No5 服务通讯桥梁Feign代理