Fegin Hystrix无效

时间:2019-03-28 03:20:51

标签: java spring hystrix feign

我尝试用Feign配置Hystrix。在浏览器地址栏中输入127.0.0.1:8100/test。无论是使用fallback还是fallbackFactory进行配置,提示的结果都是:“ com.netflix.client.ClientException:负载均衡器没有适用于客户端的服务器:microservice-provider-user”,此说明没有fallback工作。

控制器


@Import(FeignClientsConfiguration.class)
@RestController
public class TestController {

    private UserFeignClient userFeignClient;

    private UserFeignClient adminFeignClient;

    @Autowired
    public TestController(Decoder decoder, Encoder encoder, Client client, Contract contract) {
        this.userFeignClient = Feign.builder().client(client).encoder(encoder)
                .decoder(decoder).contract(contract)
                .requestInterceptor(new BasicAuthRequestInterceptor("user", "user"))
                .target(UserFeignClient.class, "http://microservice-provider-user/");

        this.adminFeignClient = Feign.builder().client(client).encoder(encoder)
                .decoder(decoder).contract(contract)
                .requestInterceptor(new BasicAuthRequestInterceptor("admin", "admin"))
                .target(UserFeignClient.class, "http://microservice-provider-user/");
    }

    @GetMapping("/test")
    @ResponseBody
    public String test() {
        return userFeignClient.findById(123L);
    }

    @GetMapping("/testAdmin")
    @ResponseBody
    public String test2() {
        return adminFeignClient.findById(123L);
    }
}

主要方法


@SpringBootApplication
@EnableFeignClients
@EnableHystrixDashboard
@EnableHystrix
@EnableCircuitBreaker
public class Demo2Application {

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

}

后备

@Component
public class FeignClientFallback implements UserFeignClient{
    @Override
    public String findById(Long id) {
        return "hello FeignClientFallback";
    }
}

渐变

plugins {
    id 'org.springframework.boot' version '2.1.3.RELEASE'
    id 'java'
}

apply plugin: 'io.spring.dependency-management'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

ext {
    set('springCloudServicesVersion', '2.1.2.RELEASE')
    set('springCloudVersion', 'Greenwich.SR1')
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-hystrix'
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-hystrix-dashboard'
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-turbine'
    implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

dependencyManagement {
    imports {
        mavenBom "io.pivotal.spring.cloud:spring-cloud-services-dependencies:${springCloudServicesVersion}"
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

假客户

@FeignClient(name = "microservice-provider-user", fallback = FeignClientFallback.class)
public interface UserFeignClient {

    @GetMapping("/test2/{id}")
    String findById(@PathVariable("id") Long id);
}

application.yml

spring:
  application:
    name: microservice-custom
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
    healthcheck:
      enabled: true
  instance:
    prefer-ip-address: true

server:
  port: 8100

feign:
  hystrix:
    enabled: true
  client:
    config:
      default:
        connectTimeout: 1000
        readTimeout: 1000
        loggerLevel: basic

hystrix:
  command:
    default:
      execution:
        isolation:
          strategy: SEMAPHORE

logging:
  level:
    com.lv.springCloudClient1.UserFeignClient: DEBUG

management:
  endpoints:
    web:
      exposure:
        include: '*'

从理论上讲,如果我关闭“ microservice-provider-user”服务器,我将获得通过后备方法返回的内容。

1 个答案:

答案 0 :(得分:0)


@Import(FeignClientsConfiguration.class)
@RestController
public class TestController {

    @Autowired
    private UserFeignClient userFeignClient;

    @Autowired
    private UserFeignClient adminFeignClient;

    @GetMapping("/test")
    @ResponseBody
    public String test() {
        return userFeignClient.findById(123L);
    }

    @GetMapping("/testAdmin")
    @ResponseBody
    public String test2() {
        return adminFeignClient.findById(123L);
    }
}

我将以前的控制器代码更改为当前代码。 Hystrix后备方法有效。