Eureka客户端-服务消费者(Feign+Hystrix)

1、pom.xml

<properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR1</spring-cloud.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</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-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <!--客户端只有当我们有了这个依赖之后,才能有那些状态页面的查看,否则会报ErrorPage-->
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>

2、application.xml

spring:
  application:
    name: eureka-client-consumer-feign-hystrix
server:
  port: 8603
eureka:
  instance:
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
feign:
  hystrix:
    enabled: true

feign.hystrix.enabled默认是false只有设置成true时,断路器才会起作用,也就是说只要在这块设置为false,那么所有的断路器都将禁用。

3、简单的fallback代码实现

注:服务提供者代码和之前博客中一致

(1)启动类

@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
@EnableHystrix
public class EurekaConsumerFeignHystrixApplication {

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

}
@EnableHystrix这个注解要加上,负责没法访问hystrix.stream节点(自己测试了一下,要访问hystrix.stream节点必须要有这个)

(2)FeignClient

@FeignClient(name="eureka-client-provider",fallback = ConsumerFeignClientFallBack.class)
public interface ConsumerFeignClient {
    @RequestMapping("/getUUid/{i}")
    public String getUUid(@PathVariable("i") Integer i);

}

(3)Fallback类

@Component
public class ConsumerFeignClientFallBack implements ConsumerFeignClient{
    @Override
    public String getUUid(Integer i) {
        return "进入断路器";
    }
}

(4)调用类

@RestController
public class ConsumerController {
    @Autowired
    ConsumerFeignClient consumerFeignClient;
    @RequestMapping("/getTest/{i}")
    public String getTest(@PathVariable Integer i){
        return consumerFeignClient.getUUid(i);
    }
}

3、简单的fallbackFactory代码实现

(1)启动类一致

(2)FeignClient

@FeignClient(name="eureka-client-provider",fallbackFactory = ConsumerFeignClientFallBackFactory.class)
public interface ConsumerFeignClientFactory {
    @RequestMapping("/getUUidFactory/{i}")
    public String getUUid(@PathVariable("i") Integer i);
}

(3)FallbackFactory类

@Component
public class ConsumerFeignClientFallBackFactory implements FallbackFactory<ConsumerFeignClientFactory> {
    @Override
    public ConsumerFeignClientFactory create(Throwable throwable) {
        return new ConsumerFeignClientFactory() {
            @Override
            public String getUUid(Integer i) {
                return "ConsumerFeignClientFactory熔断器";
            }
        };
    }
}

(4)调用类

    @Autowired
    ConsumerFeignClientFactory consumerFeignClientFactory;

    @RequestMapping("/getTestFactory/{i}")
    public String getTestFactory(@PathVariable Integer i){
        return consumerFeignClientFactory.getUUid(i);
    }

猜你喜欢

转载自blog.csdn.net/weixin_42152604/article/details/85127902