首先须要在Discovery Service章节中的创立Discovery Server服务
创立weather-service服务
//导入包,理论是通过Spring Initializr导入的,Eureka Discovery,Spring Webspring-boot-starter-webspring-cloud-starter-netflix-eureka-client//配置启动类注解@SpringBootApplication@RestController@EnableDiscoveryClientpublic 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=9000spring.application.name=weather-serviceeureka.client.service-url.defaultZone=http://localhost:8761/eureka
创立weather-app服务
//导入包,理论是通过Spring Initializr导入的,Eureka Discovery,Spring Web,Hystrix,Actuatorspring-boot-starter-webspring-cloud-starter-netflix-eureka-client//配置启动类注解@SpringBootApplication@EnableDiscoveryClient@EnableCircuitBreaker@RestControllerpublic 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@Servicepublic 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=8000spring.application.name=weather-appeureka.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 Dashboardspring-cloud-starter-netflix-hystrix-dashboard//配置启动类注解@SpringBootApplication@EnableHystrixDashboardpublic 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导入的,Turbinespring-cloud-starter-netflix-turbine//配置启动类注解@SpringBootApplication@EnableHystrixDashboardpublic class HystrixDashboardApplication { public static void main(String[] args) { SpringApplication.run(HystrixDashboardApplication.class, args); }}//配置文件server.port=3000spring.application.name=turbine-aggregatoreureka.client.service-url.defaultZone=http://localhost:8761/eurekaturbine.app-config=weather-app,datetime-appturbine.cluster-name-expression='default'//启动服务后,须要在dashboard我的项目中新建monitor的时候减少http://localhost:3000/turbine.stream//即可监控多个服务