1.服务熔断
1.参考servicecloud-provider-dept-8001 新建servicecloud的子项目servicecloud-provider-dept-hystrix-8001
pom文件添加一下依赖
<dependency>
<groupId> org.springframework.cloud </groupId>
<artifactId> spring-cloud-starter-netflix-hystrix </artifactId>
</dependency>
在yml文件中添加 eureka的instance节点添加节点nstance-id: servicecloud-dept-hystrix-8001 #自定义服务名称信息
2.修改DeptController:@HystrixCommand报异常后如何处理
package com.goolecloud.springcloud.controller;
import com.goolecloud.springcloud.entities.Dept;
import com.goolecloud.springcloud.service.DeptService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@CrossOrigin
public class DeptController {
@Autowired
DeptService service;
@RequestMapping(value = "/dept/add",method = RequestMethod.POST)
public boolean add(Dept dept){
return service.add(dept);
}
@RequestMapping(value = "/dept/get/{id}",method = RequestMethod.GET)
@HystrixCommand(fallbackMethod ="defaultFallbackDet" )
public Dept get(@PathVariable("id") Long id){
return service.get(id);
}
@RequestMapping(value = "/dept/list",method = RequestMethod.GET)
public List<Dept> list(){
return service.list();
}
public Dept defaultFallbackDet(@PathVariable("id") Long id){
System.out.println("dfjhsjfhjksdh");
Dept dept=new Dept();
dept.setDeptno(id);
dept.setDname("该id没有对应的信息");
dept.setDb_source("No id database in mysql");
return dept;
}
}
3.修改主启动类ServerspringcloudProviderDeptHystrix8001Application并添加新注解@EnableCircuitBreaker
package com.goolecloud.springcloud;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableCircuitBreaker
public class ServerspringcloudProviderDeptHystrix8001Application {
public static void main(String[] args) {
SpringApplication.run(ServerspringcloudProviderDeptHystrix8001Application.class, args);
}
}
4.测试
3个eureka先启动
主启动类ServerspringcloudProviderDeptHystrix8001Application
Consumer启动microservicecloud-consumer-dept-80
http://localhost/consumer/dept/get/112
2.服务降级
1.修改servicecloud-api工程,
根据已经有的DeptClientService接口新建一个实现了
FallbackFactory接口的类DeptClientServiceFallbackFactory
package com.goolecloud.springcloud.service;
import com.goolecloud.springcloud.entities.Dept;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class DeptClientServiceFallbackFactory implements FallbackFactory<DeptClientService> {
@Override
public DeptClientService create(Throwable throwable) {
return new DeptClientService() {
@Override
public Dept get(Long id) {
Dept dept=new Dept();
dept.setDeptno(id);
dept.setDname("该id没有对应的信息");
dept.setDb_source("No id database in mysql");
return dept;
}
@Override
public List<Dept> list() {
return null;
}
@Override
public boolean add(Dept dept) {
return false;
}
};
}
}
2.croservicecloud-api工程,DeptClientService接口在注解@FeignClient中添加fallbackFactory属性值
package com.goolecloud.springcloud.service;
import com.goolecloud.springcloud.entities.Dept;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;
@FeignClient(value="SERVERSPRINGCLOUD-DEPT",fallbackFactory = DeptClientServiceFallbackFactory.class)
public interface DeptClientService {
@RequestMapping(value = "/dept/add",method = RequestMethod.POST)
boolean add(Dept dept);
@RequestMapping(value = "/dept/get/{id}",method = RequestMethod.GET)
Dept get(@PathVariable("id") Long id);
@RequestMapping(value = "/dept/list",method = RequestMethod.GET)
List<Dept> list();
}
3.servicecloud-consumer-dept-feign工程修改YML
增加
feign:
hystrix :
enabled: true
测试
3个eureka先启动
微服务microservicecloud-provider-dept-8001启动
microservicecloud-consumer-dept-feign启动
正常访问测试
http://localhost/consumer/dept/get/1
故意关闭微服务microservicecloud-provider-dept-8001
客户端自己调用提示
http://localhost/consumer/dept/get/1
此时服务端provider已经down了,但是我们做了服务降级处理,让客户端在服务端不可用时也会获得提示信息而不会挂起耗死服务器
3.服务监控hystrixDashboard
1.新建工程servicecloud-consumer-hystrix-dashboard
pom springboot2.2 大坑是如果添加了eureka的客户端依赖则会显示没有服务注入,监控不需要eureka注入所以不需要他的依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.goolecloud.springcloud</groupId>
<artifactId>serverspringcloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>serverspringcloud-consumer-hystrix-dashboard</artifactId>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.goolecloud.springcloud</groupId>
<artifactId>serverspringcloud-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<!-- Ribbon 相关 -->
<!--<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-openfeign </artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
yml
server:
port: 9001
management:
endpoints:
web:
exposure:
include: '*'
2.主启动类改名+新注解@EnableHystrixDashboard
package com.goolecloud.springcloud;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
@EnableHystrixDashboard
@SpringBootApplication
public class ServerspringcloudConsumerHystrixDashboardApplication {
public static void main(String[] args) {
SpringApplication.run(ServerspringcloudConsumerHystrixDashboardApplication.class, args);
}
}
3.所有Provider微服务提供类(8001/8002/8003)都需要监控依赖配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
4.servicecloud-consumer-hystrix-dashboard该微服务监控消费端
(1)启动3个eureka集群(2) 启动servicecloud-provider-dept-hystrix-8001
添加
/**
* 低版本直接启动即可使用 http://ip:port/hystrix.stream 查看监控信息
* 高版本需要添加本方法方可使用 http://ip:port/hystix.stream 查看监控信息
*
* @return
*/
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
完整内容
package com.goolecloud.springcloud;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableCircuitBreaker
public class ServerspringcloudProviderDeptHystrix8001Application {
public static void main(String[] args) {
SpringApplication.run(ServerspringcloudProviderDeptHystrix8001Application.class, args);
}
/**
* 低版本直接启动即可使用 http://ip:port/hystrix.stream 查看监控信息
* 高版本需要添加本方法方可使用 http://ip:port/hystix.stream 查看监控信息
*
* @return
*/
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}
,(3)启动的相关微服务工程
监控测试
多次刷新http://localhost:8001/dept/get/1
观察监控窗口
填写监控地址