SpringCloud各服务之间通过balancer、feign、ribbon通信,相互调用小记(二)
1、添加服务service-ribbon-hystrix并引入相关包。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
2、启动类中添加相关注解以及初始化RestTemplate。
package com.example.serviceribbonhystrix;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@EnableHystrix
@EnableEurekaClient
@SpringBootApplication
public class ServiceRibbonHystrixApplication {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ServiceRibbonHystrixApplication.class, args);
}
}
3、添加RibbonHystrixService类以及相关方法,“@HystrixCommand(fallbackMethod = “getUserNameByError”)”注解代表启用熔断,在出现错误时调用“getUserNameByError”方法。
package com.example.serviceribbonhystrix.service;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class RibbonHystrixService {
@Autowired
RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "getUserNameByError")
public String getUsesrName() {
return restTemplate.getForObject("http://service-user/getUserName", String.class);
}
public String getUserNameByError() {
return "请求超时";
}
}
4、添加ServiceRibbonHystrixController类以及相关方法。
package com.example.serviceribbonhystrix.web;
import com.example.serviceribbonhystrix.service.RibbonHystrixService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ServiceRibbonHystrixController {
@Autowired
RibbonHystrixService ribbonHystrixService;
@GetMapping(value = "/getUserName")
public String getUserName() {
String userName = ribbonHystrixService.getUsesrName();
return "RibbonHystrix: " + userName;
}
}
5、启动注册中心eureka-server以及server-user、service-ribbon-hystrix服务。
6、启动后第一次访问service-ribbon-hystrix服务中的“getUserName”方法得到返回结果“张三琪”,然后卸载server-user,再次访问“getUserName”方法,这是会触发熔断机制返回“请求超时”。
7、添加服务hystrix-dashborard,引入相关包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<!--想要用spring-cloud-netflix-hystrix-dashboard
但是@enablehystrixdashboard引入不进去
属于springBot版本问题,使用以下包,begin-->
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-javanica</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-hystrix-dashboard</artifactId>
<!-- 默认使用的版本是 2.2.2.RELEASE-->
<version>2.2.2.RELEASE</version>
</dependency>
<!--@enablehystrixdashboard包引入End-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
8、在service-ribbon-hystrix服务的pom中添加“spring-boot-starter-actuator”包,在配置文件中添加“management.endpoints.web.exposure.include=*”,在启动类中添加“@EnableCircuitBreaker”注解
9、启动服务访问hystrix-dashborard服务的访问地址“http://127.0.0.1:8085/hystrix”即可看到一个小熊监控主页。
10、输入service-ribbon-hystrix服务的访问地址“http://127.0.0.1:8083/actuator/hystrix.stream”,然后再次访问service-ribbon-hystrix服务的“getUserName”方法,即可看到一个统计图。