parent
fd5938cae3
commit
e4d5bb5ae1
@ -0,0 +1,37 @@
|
||||
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>beacon-cloud</artifactId>
|
||||
<groupId>com.mashibing</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>beacon-cache</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<!-- web项目-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<!-- nacos注册中心-->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
<!-- nacos的配置中心-->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||
</dependency>
|
||||
<!-- data系列的redis依赖-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,19 @@
|
||||
package com.mashibing.cache;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
|
||||
/**
|
||||
* @author zjw
|
||||
* @description
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableDiscoveryClient
|
||||
public class CacheStarterApp {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(CacheStarterApp.class,args);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
package com.mashibing.cache.config;
|
||||
|
||||
import com.fasterxml.jackson.databind.Module;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
|
||||
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
|
||||
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
|
||||
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
/**
|
||||
* 设置RedisTemplate的序列化方式
|
||||
* @author zjw
|
||||
* @description
|
||||
*/
|
||||
@Configuration
|
||||
public class RedisConfig {
|
||||
|
||||
@Bean
|
||||
public <T> RedisTemplate<String,T> redisTemplate(RedisConnectionFactory factory,RedisSerializer<Object> redisSerializer){
|
||||
//1. 构建RedisTemplate对象
|
||||
RedisTemplate<String,T> redisTemplate = new RedisTemplate<>();
|
||||
|
||||
//2. 设置RedisConnectionFactory到RedisTemplate
|
||||
redisTemplate.setConnectionFactory(factory);
|
||||
|
||||
//3. 设置Redis的key的序列化方式
|
||||
redisTemplate.setKeySerializer(RedisSerializer.string());
|
||||
redisTemplate.setHashKeySerializer(RedisSerializer.string());
|
||||
|
||||
//4. 设置Redis的value的序列化方式 Date
|
||||
redisTemplate.setValueSerializer(redisSerializer);
|
||||
redisTemplate.setHashValueSerializer(redisSerializer);
|
||||
|
||||
//5. 保证生效
|
||||
redisTemplate.afterPropertiesSet();
|
||||
|
||||
//6. 返回RedisTemplate
|
||||
return redisTemplate;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RedisSerializer<Object> redisSerializer(){
|
||||
//1. 构建Jackson的ObjectMapper
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
//2. 设置JDK8日期格式的支持
|
||||
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
||||
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
Module timeModule = new JavaTimeModule()
|
||||
.addDeserializer(LocalDate.class,new LocalDateDeserializer(dateFormatter))
|
||||
.addDeserializer(LocalDateTime.class,new LocalDateTimeDeserializer(dateTimeFormatter))
|
||||
.addSerializer(LocalDate.class,new LocalDateSerializer(dateFormatter))
|
||||
.addSerializer(LocalDateTime.class,new LocalDateTimeSerializer(dateTimeFormatter));
|
||||
objectMapper.registerModule(timeModule);
|
||||
|
||||
//3. 构建Jackson2JsonRedisSerializer
|
||||
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
|
||||
|
||||
//4. 设置好对JDK8日期的支持
|
||||
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
|
||||
|
||||
//5. 返回设置好的Jackson2JsonRedisSerializer
|
||||
return jackson2JsonRedisSerializer;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
# 服务名称
|
||||
spring:
|
||||
application:
|
||||
name: beacon-cache
|
||||
# 多环境
|
||||
profiles:
|
||||
active: dev
|
||||
# nacos注册中心地址
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
server-addr: 114.116.226.76:8848
|
||||
# nacos配置中心地址:
|
||||
config:
|
||||
server-addr: 114.116.226.76:8848
|
||||
file-extension: yml
|
||||
# beacon-cache-dev.yml
|
@ -0,0 +1,21 @@
|
||||
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>beacon-cloud</artifactId>
|
||||
<groupId>com.mashibing</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>beacon-common</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
Loading…
Reference in new issue