5.SpringCloud-Hystrix

Hystrix(断路器)作用是在某个服务挂掉之后,对其进行访问时做回退处理。

1.在Ribbon工程基础上加入相关配置及代码
a.加入hystrix

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>

b.启动类加入注解

@EnableHystrix

c.在调用服务的方法加上注解

@HystrixCommand(fallbackMethod = "hiError")

以及相关处理方法

public String hiError(String name) {
    return "hi,"+name+",sorry,error!";
}

2.在Feign工程基础上加入相关配置及代码
a.配置文件加入配置

feign:
  hystrix:
    enabled: true

b.在调用服接口注解中加入 fallback = SchedualServiceHiHystric.class

@FeignClient(value = "service-yuan",fallback = SchedualServiceHiHystric.class)
public interface SchedualServiceHi {
    @RequestMapping(value = "/hi", method = RequestMethod.GET)
    String sayHiFromClientOne(@RequestParam(value = "name") String name);
}

c.加入调用服务接口的实现类,并重写方法

@Component
public class SchedualServiceHiHystric implements SchedualServiceHi {
    @Override
    public String sayHiFromClientOne(String name) {
        return "sorry "+name;
    }
}

3.Hystrix Dashboard(断路器仪表盘)
a.加入相关jar包

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>

b.启动类加入注解

@EnableHystrix
@EnableHystrixDashboard

c.访问
http://localhost:8008/hystrix

根据提示输入url
http://localhost:8008/hystrix.stream
输入title并点击Monitor Stream按钮

4.在工程中项目服务数量会很多,通过Hystrix Turbine可以将多个Dashboard显示在同一窗口中,方便监控
4.1 创建一个service-turbine工程,并加入依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-turbine</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-netflix-turbine</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

4.2 启动类加入注解

@EnableTurbine

4.3 修改配置文件

spring:
  application.name: service-turbine
server:
  port: 8005
security:
  basic:
    enabled: false
turbine:
  aggregator:
    clusterConfig: default   # 指定聚合哪些集群,多个使用","分割,默认为default。可使用http://.../turbine.stream?cluster={clusterConfig之一}访问
  appConfig: service-yuan,service-lili  # 配置Eureka中的serviceId列表,表明监控哪些服务
  clusterNameExpression: new String("default")
  # 1. clusterNameExpression指定集群名称,默认表达式appName;此时:turbine.aggregator.clusterConfig需要配置想要监控的应用名称
  # 2. 当clusterNameExpression: default时,turbine.aggregator.clusterConfig可以不写,因为默认就是default
  # 3. 当clusterNameExpression: metadata['cluster']时,假设想要监控的应用配置了eureka.instance.metadata-map.cluster: ABC,则需要配置,同时turbine.aggregator.clusterConfig: ABC
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8888/eureka/

5 对Eureka Client进行改造
5.1 加入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>

5.2 启动类加入注解

@EnableHystrix
@EnableHystrixDashboard

5.3 给请求加入断路器

@RequestMapping("/hi")
@HystrixCommand(fallbackMethod = "hiError")
public String home(@RequestParam String name) {
    return "hi "+name+",i am from port:" +port;
}

public String hiError(String name) {
    return "hi,"+name+",sorry,error!";
}

5.4 启动Eureka Server,2个Eureka Client,再启动service-turbine
访问http://localhost:8001/hystrix
输入http://localhost:8005/turbine.stream
可以看到已经将2个Dashboard集成进来了

猜你喜欢

转载自blog.csdn.net/gaojingyuan/article/details/79007062