美文网首页小卜java
Spring Cloud基础之Hystrix熔断器

Spring Cloud基础之Hystrix熔断器

作者: 汤太咸啊 | 来源:发表于2021-12-15 20:48 被阅读0次

首先需要在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服务的情况,如下图
image.png

配置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
//即可监控多个服务

感谢各位的阅读,帮忙点赞,感谢各位。

相关文章

网友评论

    本文标题:Spring Cloud基础之Hystrix熔断器

    本文链接:https://www.haomeiwen.com/subject/qawpfrtx.html