parent
60ee08880c
commit
b98af4be36
@ -0,0 +1,2 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4" />
|
@ -1,11 +1,9 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="Encoding">
|
<component name="Encoding">
|
||||||
<file url="file://$PROJECT_DIR$/beacon-api/beacon-common/src/main/java" charset="UTF-8" />
|
|
||||||
<file url="file://$PROJECT_DIR$/beacon-api/src/main/java" charset="UTF-8" />
|
<file url="file://$PROJECT_DIR$/beacon-api/src/main/java" charset="UTF-8" />
|
||||||
<file url="file://$PROJECT_DIR$/beacon-api/src/main/resources" charset="UTF-8" />
|
<file url="file://$PROJECT_DIR$/beacon-api/src/main/resources" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/beacon-cache/src/main/java" charset="UTF-8" />
|
||||||
<file url="file://$PROJECT_DIR$/beacon-common/src/main/java" charset="UTF-8" />
|
<file url="file://$PROJECT_DIR$/beacon-common/src/main/java" charset="UTF-8" />
|
||||||
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
|
||||||
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
Binary file not shown.
@ -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 dch
|
||||||
|
* @create 2024-03-19 14:34
|
||||||
|
*/
|
||||||
|
@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 dch
|
||||||
|
* @create 2024-03-19 11:31
|
||||||
|
*/
|
||||||
|
@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,31 @@
|
|||||||
|
package com.mashibing.cache.controller;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author dch
|
||||||
|
* @create 2024-03-19 14:37
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
public class TestController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RedisTemplate<String,Object> redisTemplate;
|
||||||
|
|
||||||
|
// 写测试 hash结构
|
||||||
|
@PostMapping("/test/set/{key}")
|
||||||
|
public String set(@PathVariable String key, @RequestBody Map map){
|
||||||
|
redisTemplate.opsForHash().putAll(key,map);
|
||||||
|
return "ok";
|
||||||
|
}
|
||||||
|
// 读测试
|
||||||
|
@GetMapping("/test/get/{key}")
|
||||||
|
public Map get(@PathVariable String key){
|
||||||
|
Map<Object, Object> result = redisTemplate.opsForHash().entries(key);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
# 服务名称
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: beacon-cache
|
||||||
|
# 多环境
|
||||||
|
profiles:
|
||||||
|
active: dev
|
||||||
|
# nacos注册中心地址
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
discovery:
|
||||||
|
server-addr: 192.168.1.113:8848
|
||||||
|
# nacos配置中心地址:
|
||||||
|
config:
|
||||||
|
server-addr: 192.168.1.113:8848
|
||||||
|
file-extension: yml
|
||||||
|
# beacon-cache-dev.yml
|
||||||
|
|
Loading…
Reference in new issue