一)Hystrix缓存解决雪崩
Hystrix 为了降低访问服务的频率,支持将一个请求与返回结果做缓存处理。如果再次 请求的 URL 没有变化,那么 Hystrix 不会请求服务,而是直接从缓存中将结果返回。这样可 以大大降低访问服务的压力。
Hystrix 自带缓存。有两个缺点:
- 是一个本地缓存。在集群情况下缓存是不能同步的。
- 不支持第三方缓存容器。Redis,memcache 不支持的。 可以使用 spring 的 cache。
本例中使用Redis进行缓存
1.创建项目
2.pom文件
1.eureka客户端
2.spring-data-redis
3.hystrix
<?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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sxt</groupId>
<artifactId>18-hystrix-cache</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>18-hystrix-cache</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<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-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.修改全局配置文件
1.添加Redis的配置
spring.application.name=18-hystrix-cache
server.port=9095
eureka.client.service-url.defaultZone=http://peer1:8081/eureka/,http://peer2:8082/eureka/,http://peer3:8083/eureka/
# Redis配置
spring.redis.database=0
#Redis 服务器地址
spring.redis.host=192.168.177.135
#Redis 服务器连接端口
spring.redis.port=6379
#Redis 服务器连接密码(默认为空)
spring.redis.password=
#连接池最大连接数(负值表示没有限制)
spring.redis.jedis.pool.max-active=100
#连接池最大阻塞等待时间(负值表示没有限制)
spring.redis.jedis.pool.max-wait=3000
#连接池最大空闭连接数
spring.redis.jedis.pool.max-idle=200
#连接汉最小空闲连接数
spring.redis.jedis.pool.min-idle=50
#连接超时时间(毫秒)
spring.redis.pool.timeout=600
4.修改启动类
@SpringBootApplication
@EnableEurekaClient //开启eureka客户端
@EnableCaching //开启缓存
public class Application
{
public static void main(String[] args)
{
SpringApplication.run(Application.class, args);
}
}
5.使用假数据进行测试缓存
修改ProviderService,添加方法
使用假数据进行测试的缓存,使用到的注解
1.@Cacheable:该注解用户缓存,将方法的返回值进行缓存
2.@CacheEvict:用于清除缓存
3.@CachaConfig:使用天机缓存配置设置统一的前缀,对应前两的注解的value值
@CacheConfig(cacheNames = "com.sxt")
@Service
public class ProductService
{
/**
* @param null
* @description:@Cacheable:添加指定的缓存数据将返回的值进行缓存
* key:是存在Redis中的值的key值
* @return: a
* @author: shinelon
* @time: 2019/9/3:15:58
*/
@Cacheable(key = "'product'+ #id")
public Product getProduct(Integer id){
System.out.println("========GET=========");
return new Product("新的商品",id);
}
/**
* @param null
* @description: @CacheEvict:注解的作用是删除指定缓存数据
* @return:
* @author: shinelon
* @time: 2019/9/3:15:58
*/
@CacheEvict(key = "'product'+ #id")
public void delProduct(Integer id){
System.out.println("=========DEL==========");
}
6.修改Controller
//测试添加
@GetMapping(value = "/get")
public Product getProduct(Integer id){
return this.productService.getProduct(id);
}
//测试删除
@GetMapping(value = "/del")
public void delProduct(Integer id){
this.productService.delProduct(id);
}
7.测试
1.测试GET
缓存中也有值
2.测试删除
以上就是通过缓存解决雪崩,其实概念跟简单,就是将Provider返回的结果进行缓存,Hystirx默认是将结果和请求的URL进行缓存,如果的在此来了请求,不在访问Provider,而是直接在缓存中取出即可。以此减少对Provider的访问。