标签:main serve dep 准备 分享图片 base cli back private
// 参考 microservicecloud-provider-dept-8001
// pom.xml
<!-- hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
// application.yml
eureka:
client: # 客户端注册进eureka服务列表内
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
instance:
instance-id: microservicecloud-dept8001-hystrix # 自定义hystrix相关的服务名称信息
prefer-ip-address: true # 访问路径可以显示IP地址
// 修改DeptController
@RestController
public class DeptController {
@Autowired
private DeptService deptService;
@Autowired
private DiscoveryClient client;
@RequestMapping(value="/dept/get/{id}",method=RequestMethod.GET)
// 一旦调用服务方法失败并抛出错误信息后,会自动调用@HystrixCommand标注好的fallbackMethod调用类中的指定方法
@HystrixCommand(fallbackMethod="processHystrix_Get")
public Dept get(@PathVariable("id") Long id) {
Dept dept = this.deptService.get(id);
if(null == dept) {
throw new RuntimeException("该ID:"+id+"没有对应的信息");
}
return dept;
}
public Dept processHystrix_Get(@PathVariable("id") Long id) {
return new Dept().setDeptno(id).setDname("该ID:"+id+"没有对应的信息,null -- @HystrixCommand")
.setDb_source("no this database in MySQL");
}
}
// 修改主启动类DeptProvider8001_Hystrix_App并添加新注解@EnableCircuitBreaker
@SpringBootApplication
@EnableEurekaClient // 本服务启动后,自动注册进eureka服务中
@EnableDiscoveryClient // 服务发现
@EnableCircuitBreaker // 对Hystrix熔断机制的支持
public class DeptProvider8001_Hystrix_App {
public static void main(String[] args) {
SpringApplication.run(DeptProvider8001_Hystrix_App.class, args);
}
}
// 测试访问:
// http://localhost:8082/consumer/dept/get/112
// 修改microservicecloud-api工程
// 根据已有的DeptClientService接口,新建一个实现了FallbackFactory接口的类DeptClientServiceFallbackFactory
@Component // 不要忘记添加
public class DeptClientServiceFallbackFactory implements FallbackFactory<DeptClientService> {
@Override
public DeptClientService create(Throwable arg0) {
return new DeptClientService() {
@Override
public Dept get(long id) {
return new Dept().setDeptno(id).setDname("该ID:"+id+"没有对应的信息,Consumer客户端提供的降级信息,此刻服务Provider已经关闭")
.setDb_source("no this database in MySQL");
}
@Override
public List<Dept> list() {
return null;
}
@Override
public boolean add(Dept dept) {
return false;
}
};
}
}
// 修改microservicecloud-api工程, DeptClientService接口
// 在注解@FeignClient中添加 fallbackFactory 属性值
@FeignClient(value="MICROSERVICECLOUD-DEPT",
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);
}
// mvn clean
// mvn install
// 修改microservicecloud-consumer-dept-feign 工程
// application.yml
feign:
hystrix:
enabled: true
// 测试访问:
// http://localhost:8082/consumer/dept/get/1
// 然后手动关闭工程:microservicecloud-provider-dept-8001
// pom.xml
<!-- hystrix和hystrix-dashboard 相关 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
// application.yml
server:
port: 9001
// 主启动类:
@SpringBootApplication
@EnableHystrixDashboard
public class DeptConsumer_DashBoard_App {
public static void main(String[] args) {
SpringApplication.run(DeptConsumer_DashBoard_App.class, args);
}
}
// 所有Provider微服务提供类(8001/8002/8003)都需要提供监控依赖配置
// (8001/8002/8003)pom.xml
<!-- actuator 监控信息完善 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
// 启动相关工程
// microservicecloud-consumer-hystrix-dashboard // 访问: http://localhost:9001/hystrix
// 3个Eureka集群
// microservicecloud-provider-dept-hystrix-8001
// 访问: http://localhost:8001/dept/get/1
// 访问: http://localhost:8001/hystrix.stream
参考资料:
标签:main serve dep 准备 分享图片 base cli back private
原文地址:https://www.cnblogs.com/linkworld/p/9191547.html