首先须要在 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
// 即可监控多个服务