关于springcloud:Spring-Cloud基础之Hystrix熔断器

首先须要在Discovery Service章节中的创立Discovery Server服务
创立weather-service服务

//导入包,理论是通过Spring Initializr导入的,Eureka Discovery,Spring Web
spring-boot-starter-web
spring-cloud-starter-netflix-eureka-client
//配置启动类注解
@SpringBootApplication
@RestController
@EnableDiscoveryClient
public class WeatherServiceApplication {
   private String[] weather = new String[] {"sunny", "cloudy","rainy","windy"};
   public static void main(String[] args) {
       SpringApplication.run(WeatherServiceApplication.class, args);
   }
   @GetMapping("/weather")
   public String getWeather(){
       int rand = ThreadLocalRandom.current().nextInt(0,4);
       return weather[rand];
   }
}
//配置文件
server.port=9000
spring.application.name=weather-service
eureka.client.service-url.defaultZone=http://localhost:8761/eureka

创立weather-app服务

//导入包,理论是通过Spring Initializr导入的,Eureka Discovery,Spring Web,Hystrix,Actuator
spring-boot-starter-web
spring-cloud-starter-netflix-eureka-client
//配置启动类注解
@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
@RestController
public class WeatherAppApplication {
   @Autowired
   public WeatherService weatherService;
   public static void main(String[] args) {
       SpringApplication.run(WeatherAppApplication.class, args);
   }
   @Bean
   @LoadBalanced
   public RestTemplate restTemplate(){
       return new RestTemplate();
   }
   @GetMapping("/current/weather")
   public String getWeather(){
       return "The current weather is " +weatherService.getWeather();
   }
}
//配置业务类,HystrixCommand配置了fallbackMethod办法为unknown
@Service
public class WeatherService {
   @Autowired
   private RestTemplate restTemplate;
   @HystrixCommand(fallbackMethod = "unknown")
   public String getWeather() {
       return restTemplate.getForEntity("http://weather-service/weather",String.class).getBody();
   }
   public String unknown(){
       return "unknown";
   }
}
//配置文件
server.port=8000
spring.application.name=weather-app
eureka.client.service-url.defaultZone=http://localhost:8761/eureka

//通过拜访http://localhost:8000/current/weather
//当weather服务down掉时,输入
The current weather is unknown
//当weather服务ok时,输入
The current weather is windy

配置hystirx-dashboard服务

//导入包,理论是通过Spring Initializr导入的,Hystrix Dashboard
spring-cloud-starter-netflix-hystrix-dashboard
//配置启动类注解
@SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashboardApplication {
   public static void main(String[] args) {
       SpringApplication.run(HystrixDashboardApplication.class, args);
   }
}
//须要在weather-app服务的配置文件减少如下配置才能够监控到weather-app的服务
management.endpoints.web.exposure.include=hystrix.stream
//关上http://localhost:8080/hystrix/
//配置monitor地址http://localhost:8000/actuator/hystrix.stream
//新增后即可monitor服务的状况,如下图

配置turbine服务

//导入包,理论是通过Spring Initializr导入的,Turbine
spring-cloud-starter-netflix-turbine
//配置启动类注解
@SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashboardApplication {
   public static void main(String[] args) {
       SpringApplication.run(HystrixDashboardApplication.class, args);
   }
}
//配置文件
server.port=3000
spring.application.name=turbine-aggregator
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
turbine.app-config=weather-app,datetime-app
turbine.cluster-name-expression='default'
//启动服务后,须要在dashboard我的项目中新建monitor的时候减少http://localhost:3000/turbine.stream
//即可监控多个服务

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理