目录
1、商品详情页缓存数据生产服务(Java服务)的工作流程分析
2、spring boot+mybatis+redis框架整合搭建
1、商品详情页缓存数据生产服务(Java服务)的工作流程分析
(1)监听多个kafka topic,每个kafka topic对应一个服务(简化一下,监听一个kafka topic)
(2)如果一个服务发生了数据变更,那么就发送一个消息到kafka topic中
(3)缓存数据生产服务监听到了消息以后,就发送请求到对应的服务中调用接口以及拉取数据,此时是从mysql中查询的
(4)缓存数据生产服务拉取到了数据之后,会将数据在本地缓存中写入一份,就是ehcache中
2、spring boot+mybatis+redis框架整合搭建
(1)pom.xml 略
(2)Application 略
(3)resources/Application.properties 连MySQL
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
(4)resources/mybatis 略
(5)整合Jedis Cluster
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
Application
@Bean
public JedisCluster JedisClusterFactory() {
Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
jedisClusterNodes.add(new HostAndPort("192.168.31.19", 7003));
jedisClusterNodes.add(new HostAndPort("192.168.31.19", 7004));
jedisClusterNodes.add(new HostAndPort("192.168.31.227", 7006));
JedisCluster jedisCluster = new JedisCluster(jedisClusterNodes);
return jedisCluster;
}
3、服务本地堆缓存ehcache
主要配置
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.8.3</version>
</dependency>
@Configuration
@EnableCaching
public class CacheConfiguration {
@Bean
public EhCacheManagerFactoryBean ehCacheManagerFactoryBean(){
EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean();
cacheManagerFactoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
cacheManagerFactoryBean.setShared(true);
return cacheManagerFactoryBean;
}
@Bean
public EhCacheCacheManager ehCacheCacheManager(EhCacheManagerFactoryBean bean){
return new EhCacheCacheManager(bean.getObject());
}
}
ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false">
<diskStore path="java.io.tmpdir/Tmp_EhCache" />
<defaultCache
eternal="false"
maxElementsInMemory="1000"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
memoryStoreEvictionPolicy="LRU" />
<cache
name="local"
eternal="false"
maxElementsInMemory="1000"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
memoryStoreEvictionPolicy="LRU" />
</ehcache>
CacheService
其实这一步可以用@Cacheable 注解实现
@Service("cacheService")
public class CacheServiceImpl implements CacheService {
public static final String CACHE_NAME = "local";
@Cacheable(value = CACHE_NAME, key = "'key_'+#id")
public ProductInfo findById(Long id){
return null;
}
@CachePut(value = CACHE_NAME, key = "'key_'+#productInfo.getId()")
public ProductInfo saveProductInfo(ProductInfo productInfo) {
return productInfo;
}
}
Controller测试一下ehcache的整合
@Controller
public class CacheTestController {
@Resource
private CacheService cacheService;
@RequestMapping("/testPutCache")
@ResponseBody
public void testPutCache(ProductInfo productInfo) {
System.out.println(productInfo.getId() + ":" + productInfo.getName());
cacheService.saveProductInfo(productInfo);
}
@RequestMapping("/testGetCache")
@ResponseBody
public ProductInfo testGetCache(Long id) {
ProductInfo productInfo = cacheService.findById(id);
System.out.println(productInfo.getId() + ":" + productInfo.getName());
return productInfo;
}
}