import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; public interface MemberService { @RequestMapping(value = "testMember",method = RequestMethod.GET) public String testMember(); @RequestMapping(value = "test",method = RequestMethod.GET) public String test();
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; public interface OrderService { @RequestMapping(value = "testOrder",method = RequestMethod.GET) public String testOrder(); }
注意:該類實現的接口是feign中的接口java
@Component public class MemberFallBack implements MemberFeign { @Override public String testMember() { return "服務降級"; } @Override public String test(){ return "服務降級"; }
import com.bdqn.fallBack.MemberFallBack; import com.bdqn.service.MemberService; import org.springframework.cloud.openfeign.FeignClient; @FeignClient(name = "member",fallback = MemberFallBack.class) public interface MemberFeign extends MemberService { }
@RestController public class OrderService { @Autowired private MemberFeign memberFeign; @RequestMapping("test") public String test(){ return memberFeign.testMember(); }
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableEurekaClient @EnableFeignClients public class OrderStar { public static void main(String[] args) { SpringApplication.run(OrderStar.class,args); } @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); }
application.yml配置文件web
###服務啓動端口號 server: port: 8000 ###服務名稱(服務註冊到eureka名稱) spring: application: name: order ###服務註冊到eureka地址 eureka: client: service-url: defaultZone: http://localhost:8100/eureka ###由於該應用爲註冊中心,不會註冊本身 register-with-eureka: true ###是否須要從eureka上獲取註冊信息 fetch-registry: true ###開啓Hystrix斷路器 feign: hystrix: enabled: true ##### hystrix禁止服務超時時間 #hystrix: # command: # default: # execution: # timeout: # enabled: false #
import com.bdqn.service.MemberService; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class MemberServiceIpml implements MemberService { @RequestMapping(value = "testMember",method = RequestMethod.GET) public String testMember() { try { Thread.sleep(1500); }catch (Exception e){} return "會員接口"; } @Override public String test() { try { Thread.sleep(1500); }catch (Exception e){} return "會員接口"; } }
@SpringBootApplication @EnableEurekaClient @EnableFeignClients public class MemberStar { public static void main(String[] args) { SpringApplication.run(MemberStar.class,args); } @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } }
###服務啓動端口號 server: port: 8001 ###服務名稱(服務註冊到eureka名稱) spring: application: name: member ###服務註冊到eureka地址 eureka: client: service-url: defaultZone: http://localhost:8100/eureka ###由於該應用爲註冊中心,不會註冊本身 register-with-eureka: true ###是否須要從eureka上獲取註冊信息 fetch-registry: true ###開啓Hystrix斷路器 feign: hystrix: enabled: true