使用假装客户端

时间:2017-08-05 17:21:50

标签: microservices netflix-eureka netflix-feign spring-cloud-feign feign

我正在尝试使用Feign客户端实现回退,但没有获得成功。它是一个最简单的代码请在下面找到。

主类

@SpringBootApplication
@EnableDiscoveryClient
@RestController
@EnableFeignClients
public class EurekaClient1Application {

    @Autowired
    public DiscoveryClient discoveryClient;

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

    @Autowired 
    FeingInterface feingInterface;




    @GetMapping("/hi/{name}")
    public String test(@PathVariable String name)
    {
        String h = feingInterface.test(name);

        return h;
    }
}

Feign界面

@FeignClient(name="client22",fallback=FallBack.class)
public interface FeingInterface {

    @GetMapping("/hiname/{name}")
    public String test(@PathVariable("name") String name);

}

后备班

@Component
class FallBack implements FeingInterface{

    @Override
    public String test(String name) {
        // TODO Auto-generated method stub
        return "fall back methord being called";
    }

}

在休息客户端中获取错误,但不从后备方法

获取错误
  

" timestamp":1501950134118,       "状态":500,       "错误":"内部服务器错误",       " exception":" java.lang.RuntimeException",       " message":" com.netflix.client.ClientException:负载均衡器没有客户端的可用服务器:client22",

要获取回退方法消息,我通过了eureka服务器中没有的client22 eureka id。我在pom中有假装。有人可以调查这个。

1 个答案:

答案 0 :(得分:4)

回退实际上并非由Feign本身处理,而是由断路器处理。所以,你需要在你的类路径上放置Hystrix(这是Netflix断路器)并在你的application.yml文件中启用它,如下所示:

feign:
  hystrix:
    enabled: true

如果你正在使用云:spring-cloud-starter-openfeign'在您的build.gradle或pom.xml文件中,Hystrix应该自动在您的类路径上。