注:在Demo中的pom一样,这里推荐Feign,因为Feign集合了Hystrix熔断器
主要是在客户端(接收端)配置
主要是在application配置里开启,然后在接口和实现类配置。
1.主方法
@SpringBootApplication @EnableEurekaClient //可加可不加,加了默认是euraka @EnableFeignClients //开启Fiegn //@EnableHystrix//不要打这个注解,因为Feign集成了注解,不然会报错 public class UserProviderApplication_4444 { public static void main(String[] args) { SpringApplication.run(UserProviderApplication_4444.class, args); } }
2.配置
server: port: 4444 eureka: client: # registerWithEureka: false #作为一个注册中心不需要注册自己是否要注册到eureka #fetchRegistry: false #表示是否从Eureka Server获取注册信息 serviceUrl: defaultZone: http://peer1000:1000/eureka/,http://peer1001:1001/eureka/ #defaultZone: http://peer1000:1000/eureka/ spring: application: name: getting1 #开启hystirx,豪猪保护系统 feign: hystrix: enabled: true ribbon: ConnectTimeout: 3000 ReadTimeout: 60000 hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 3000 #hystrix超时时间
3.Feign的接口
/** * @FeignClient("SENDING") 代表这个接口是一个feign的客户端 * SENDING 是这个接口要调用的远程服务的名字 * fallback = FeignClientEmployeeImpl.class就是出错后会调用一个回调方法 */ @FeignClient(value = "SENDING",fallback = FeignClientEmployeeImpl.class ) public interface FeignClientEmployee { @RequestMapping("/employee/{id}") //将{id}的值转换成传入的参数 public Employee employee(@PathVariable("id") Long id); }
3.Feign的接口的实现类(出错的回调类)
@Service//交给spring管理 public class FeignClientEmployeeImpl implements FeignClientEmployee { @Override public Employee employee(Long id) { return new Employee(-1L,"失败"); } }
4.controller层同demo中Feign的一样。
5.测试:开启注册中心,开启发送端,开启客户端(Feign),然后关闭发送端,看是否有失败。信息