1.1 在Produce服务层导入须要的依赖java
<!-- hystrix --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency>
1.2 编写须要访问的Controllerweb
package com.shi.prodect.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.ResponseBody; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import com.shi.core.model.Dept; import com.shi.prodect.service.DeptService; @Controller public class DeptContoller { @Autowired private DeptService deptService; @Autowired private DiscoveryClient client; /** * 测试使用熔断器Hystrix的使用 * @author SHF * @version 建立时间:2018年11月30日 下午4:51:37 * @param id * @return */ @GetMapping("/dept/get/{id}") @ResponseBody //一旦服务调用失败并抛出了错误信息后,会自动调用@HystrixCommand标注好的fallbackMethod中指定的方法 @HystrixCommand(fallbackMethod="Get_DeptError") public Dept get(@PathVariable(name="id") Long id) { Dept dept = deptService.get(id); if(dept == null) { throw new RuntimeException("该"+id+"暂时没有对应的信息"); } return dept; } public Dept Get_DeptError(@PathVariable(name="id") Long id) { Dept dept = new Dept(); dept.setDeptno(id); dept.setDname("该数据库中"+id+"尚未改id的存在 @HystrixCommand "); dept.setDb_source("no data_source exit..."); return dept; } }
1.3 在主启动类上面添加对熔断器的支持spring
@EnableCircuitBreaker //对Hystrix熔断器的支持
2. 服务降级(服务降级是在客户端完成的)数据库
2.1 如今core包中配置接口的降级处理方案json
package com.shi.core.service; import java.util.List; import org.springframework.stereotype.Component; import com.shi.core.model.Dept; import feign.hystrix.FallbackFactory; /** * @Component 该注解必定要在customer 容器初始化中被初始化到 否则加载不到该注解,没法初始化该对象, * 会报错:No fallbackFactory instance of type * @author SHF * @version 建立时间:2018年12月3日 下午2:14:06 */ @Component //不要忘记添加,不要忘记添加,不要忘记添加 public class DeptClientServiceFallbackFactory implements FallbackFactory<DeptClientService>{ @Override public DeptClientService create(Throwable arg0) { return new DeptClientService() { @Override public List<Dept> list() { // TODO Auto-generated method stub return null; } @Override public Dept get(long id) { // TODO Auto-generated method stub Dept dept = new Dept(); dept.setDeptno(id); dept.setDname("该数据库中"+id+"尚未改id的存在,Consumer客户端提供的降级信息,此刻服务Provider已经关闭"); dept.setDb_source("no data_source exit..."); return dept; } @Override public boolean add(Dept dept) { // TODO Auto-generated method stub return false; } }; } }
package com.shi.core.service; import java.util.List; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.shi.core.model.Dept; /** * 让该接口实现熔断机制,让该接口在使用的中远程调用出错就调用 DeptClientServiceFallbackFactory 中指定的方法 * @author SHF * @version 建立时间:2018年12月3日 上午10:36:25 */ //@FeignClient(value = "SPRINGCLOUD04-PRODECT-8001") @FeignClient(name = "SPRINGCLOUD04-PRODECT-8001",fallbackFactory = DeptClientServiceFallbackFactory.class) //指定为哪一个微服务提供接口 public interface DeptClientService { @RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET) public Dept get(@PathVariable("id") long id); @RequestMapping(value = "/dept/list", method = RequestMethod.GET) public List<Dept> list(); @RequestMapping(value = "/dept/add", method = RequestMethod.POST) public boolean add(Dept dept); }
2.3 编写客户端类增长配置app
feign: hystrix: enabled: true
package com.shi.customer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.feign.EnableFeignClients; import org.springframework.cloud.netflix.ribbon.RibbonClient; import org.springframework.context.annotation.ComponentScan; import org.springframework.stereotype.Component; @SpringBootApplication @EnableEurekaClient @EnableFeignClients(basePackages= {"com.shi.core.service"})//feign服务类的包名 @ComponentScan(basePackages = "com.shi") //必定要保证扫描到core包中@Component 注解 public class Customer7001Feign { public static void main(String[] args) { SpringApplication.run(Customer7001Feign.class, args); } }
2.4 测试ide
先走通整个流程,而后把服务提供者(product)关闭掉,看调用是否正常微服务