server: port: 8765 spring: application: name: feign-provider eureka: instance: lease-renewal-interval-in-seconds: 5 lease-expiration-duration-in-seconds: 15 client: healthcheck: enabled: true service-url: defaultZone: http://admin:123@localhost:8761/eureka/ feign: hystrix: enabled: true hystrix: command: default: execution: timeout: enabled: true isolation: thread: timeoutInMilliseconds: 1000
<dependencies>
<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.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</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-actuator</artifactId>
</dependency>
</dependencies>
package com.transsion; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableEurekaClient @EnableFeignClients public class FeignProviderApplication { public static void main(String[] args) { SpringApplication.run(FeignProviderApplication.class, args); } }
package com.transsion; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class TestController { @Autowired TestService testService; @RequestMapping(value = "/test", method = RequestMethod.GET) public String helloConsumer() { //调用了绑定PROVIDER服务接口的客户端向服务发起/test接口的调用 return testService.test(); } }
package com.transsion; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping;
//@FeignClient("PROVIDER"):绑定注册的服务:其中value为注册服务的applicationName,fallbac为熔断器的配置 @FeignClient(name = "PROVIDER",fallback = FallbackTest.class) public interface TestService { //绑定具体服务的REST接口 @RequestMapping("/test") String test(); }
package com.transsion; import org.springframework.stereotype.Component; //从@HystrixCommand(fallbackMethod)中的回调方法转换成回调类 @Component public class FallbackTest implements TestService{ @Override public String test() { return "fallback"; } }
原文:https://www.cnblogs.com/rainsakura/p/10407889.html