构建beacon-cache缓存模块

main
heqijun 4 months ago
parent 9a611b8cf0
commit e8ee0ff8c8

@ -0,0 +1,47 @@
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.mashibing</groupId>
<artifactId>beacon-cloud</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<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>
<!-- openFeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- &lt;!&ndash; redis &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-data-redis</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>com.msb.cloud</groupId>
<artifactId>horse-framework-starter-redis</artifactId>
</dependency>
</dependencies>
</project>

@ -0,0 +1,22 @@
package com.mashibing.cache;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @author heqijun
* @ClassName: CacheApplication
* @Description: CacheApplication
* @date 2025/6/5 13:35
*/
@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class CacheApplication {
public static void main(String[] args) {
SpringApplication.run(CacheApplication.class, args);
}
}

@ -0,0 +1,83 @@
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 org.springframework.data.redis.serializer.StringRedisSerializer;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* @author heqijun
* @ClassName: RedisConfig
* @Description: RedisTemplate
* @date 2025/6/5 13:51
*/
//
//@Configuration
//public class RedisConfig {
// // 因为默认使用data-redis提供的RedisTemplate对象时
// // 针对key和value的序列化方式都是byte[],这种方式在图形化界面上的查看不友好,
// // 将key做String的序列化将Value做JSON的序列化。
// // 同时也是为了让RedisTemplate支持JDK8的日期格式
// @Bean
// public <T> RedisTemplate<String, T> redisTemplate(RedisConnectionFactory factory, RedisSerializer<Object> redisSerializer) {
// //1. 构建RedisTemplate对象
// RedisTemplate<String, T> redisTemplate = new RedisTemplate<>();
//
// //2. 设置RedisConnectionFactory
// 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 mapper = new ObjectMapper();
//
// //2. 设置JDK8日期格式的支持
// DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
// DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// JavaTimeModule javaTimeModule = new JavaTimeModule();
// javaTimeModule
// .addSerializer(LocalDate.class, new LocalDateSerializer(dateFormatter))
// .addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(dateTimeFormatter))
// .addDeserializer(LocalDate.class, new LocalDateDeserializer(dateFormatter))
// .addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(dateTimeFormatter));
// mapper.registerModule(javaTimeModule);
//
// //3. 构建Jackson2JsonRedisSerializer
// Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
//
// //4. 设置好对JDK8日期的支持
// jackson2JsonRedisSerializer.setObjectMapper(mapper);
//
// return jackson2JsonRedisSerializer;
// }
//
//}

@ -0,0 +1,45 @@
package com.mashibing.cache.controller;
import com.msb.framework.redis.RedisClient;
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 heqijun
* @ClassName: CacheController
* @Description: TODO()
* @date 2025/6/5 13:46
*/
@RestController
@RequestMapping("cache")
public class CacheController {
@Autowired
private RedisClient redisClient;
@GetMapping("getString/{key}")
public String getString(@PathVariable String key) {
return redisClient.get(key);
}
@GetMapping("setString/{key}/{value}")
public String setString(@PathVariable String key, @PathVariable String value) {
redisClient.set(key, value);
return "保存成功";
}
@GetMapping("getHash/{key}")
public Map getHash(@PathVariable String key) {
return redisClient.hGetAll(key);
}
@PostMapping("setHash/{key}")
public String setHash(@PathVariable String key, @RequestBody Map hash) {
redisClient
return "保存成功";
}
}

@ -0,0 +1,18 @@
# 服务名称
spring:
application:
name: beacon-cache
# 多环境
profiles:
active: dev
# nacos注册中心地址
cloud:
nacos:
discovery:
server-addr: 192.168.1.13:8848
# nacos配置中心地址:
config:
server-addr: 192.168.1.13:8848
file-extension: yml
# beacon-cache-dev.yml

@ -11,6 +11,7 @@
<modules> <modules>
<module>beacon-api</module> <module>beacon-api</module>
<module>beacon-common</module> <module>beacon-common</module>
<module>beacon-cache</module>
</modules> </modules>
@ -24,6 +25,7 @@
<properties> <properties>
<spring.cloud-version>Hoxton.SR12</spring.cloud-version> <spring.cloud-version>Hoxton.SR12</spring.cloud-version>
<spring.cloud.alibaba-version>2.2.7.RELEASE</spring.cloud.alibaba-version> <spring.cloud.alibaba-version>2.2.7.RELEASE</spring.cloud.alibaba-version>
<horse-version>1.0.0</horse-version>
</properties> </properties>
<dependencyManagement> <dependencyManagement>
@ -42,6 +44,13 @@
<type>pom</type> <type>pom</type>
<scope>import</scope> <scope>import</scope>
</dependency> </dependency>
<dependency>
<groupId>com.msb.cloud</groupId>
<artifactId>horse-framework-starters</artifactId>
<version>${horse-version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>

Loading…
Cancel
Save