redis微服务搭建

发布于:2021-10-17 20:38:10

#### 二.缓存微服务的搭建


1.搭建项目结构

hrm-redis-parent
hrm-redis-client
hrm-redis-service-2030

2.搭建 hrm-redis-service-2030 导入依赖




io.springfox
springfox-swagger2


io.springfox
springfox-swagger-ui



org.springframework.cloud
spring-cloud-config-client


org.springframework.cloud
spring-cloud-starter-netflix-eureka-client



org.springframework.boot
spring-boot-starter-web


redis.clients
jedis
2.9.0


cn.itsource.hrm
hrm-basic-utils



2.准备Redis工具类

配置文件 redis.properties


redis.host=127.0.0.1
redis.port=6379
redis.password=123456
redis.timeout=5000

RedisUtil


package cn.itsource.hrm.utils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.util.Properties;

/**
* 获取连接池对象
*/
public enum RedisUtils {
INSTANCE;
static JedisPool jedisPool = null;

static {
//1 创建连接池配置对象
JedisPoolConfig config = new JedisPoolConfig();
//2 进行配置-四个配置
config.setMaxIdle(1);//最小连接数
config.setMaxTotal(11);//最大连接数
config.setMaxWaitMillis(10 * 1000L);//最长等待时间
config.setTestOnBorrow(true);//测试连接时是否畅通
//3 通过配置对象创建连接池对象
Properties properties = null;
try {
properties = new Properties();
properties.load(RedisUtils.class.getClassLoader().getResourceAsStream("redis.properties"));
} catch (IOException e) {
e.printStackTrace();
}
String host = properties.getProperty("redis.host");
String port = properties.getProperty("redis.port");
String password = properties.getProperty("redis.password");
String timeout = properties.getProperty("redis.timeout");

jedisPool = new JedisPool(config, host, Integer.valueOf(port),Integer.valueOf(timeout), password);
}

//获取连接
public Jedis getSource() {
return jedisPool.getResource();
}

//关闭资源
public void closeSource(Jedis jedis) {
if (jedis != null) {
jedis.close();
}

}

/**
* 设置字符值
*
* @param key
* @param value
*/
public void set(String key, String value) {
Jedis jedis = getSource();
jedis.set(key, value);
closeSource(jedis);
}

/**
* 设置
* @param key
* @param value
*/
public void set(byte[] key, byte[] value) {
Jedis jedis = getSource();
jedis.set(key, value);
closeSource(jedis);
}

/**
*
* @param key
* @return
*/
public byte[] get(byte[] key) {
Jedis jedis = getSource();
try {
return jedis.get(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
closeSource(jedis);
}
return null;

}

/**
* 设置字符值
*
* @param key
*/
public String get(String key) {
Jedis jedis = getSource();
try {
return jedis.get(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
closeSource(jedis);
}

return null;

}
}


3.编写RedisController

/**
* redis的接口
*/
@RestController
@RequestMapping("/redis")
public class RedisController {

@GetMapping("/get/{key}")
public AjaxResult get(@PathVariable("key")String key){
String result = RedisUtils.INSTANCE.get(key);
return AjaxResult.me().setResultObj(result);
}
@PostMapping("/set")
public AjaxResult set(@RequestParam("key")String key,@RequestParam("value")String value){
RedisUtils.INSTANCE.set(key,value);
return AjaxResult.me();
}
@PostMapping("/setex")
public AjaxResult setex(@RequestParam("key")String key,
@RequestParam("value")String value,
@RequestParam("seconds")int seconds){
RedisUtils.INSTANCE.setex(key,value,seconds);
return AjaxResult.me();
}
}


4.配置类
主配置类swagger配置类
5.配置文件

eureka:
client:
serviceUrl:
defaultZone: http://localhost:1010/eureka/ #注册中心地址
instance:
prefer-ip-address: true #使用ip地址注册
instance-id: hrm-redis-service #指定服务的id
server:
port: 2030
spring:
application:
name: hrm-redis-service


6.修改zuul
zuul路由swagger


三.搭建Redis的Feign的客户端接口模块

hrm-redis-client



1.导入依赖



cn.itsource.hrm
hrm-basic-utils


org.springframework.cloud
spring-cloud-starter-openfeign



2.编写接口 RedisClient

@FeignClient(value = "hrm-redis-service",fallback = RedisClientFallback.class )
public interface RedisClient {

@GetMapping("/redis/get/{key}")
AjaxResult get(@PathVariable("key")String key);

@PostMapping("/redis/set")
AjaxResult set(@RequestParam("key")String key, @RequestParam("value")String value);

@PostMapping("/redis/setex")
AjaxResult setex(@RequestParam("key")String key,
@RequestParam("value")String value,
@RequestParam("seconds")int seconds);
}

3.编写 RedisClientFallback

@Component
public class RedisClientFallback implements RedisClient {
@Override
public AjaxResult get(String key) {
return AjaxResult.me().setSuccess(false).setMessage("Redis服务不可用");
}

@Override
public AjaxResult set(String key, String value) {
return AjaxResult.me().setSuccess(false).setMessage("Redis服务不可用");
}

@Override
public AjaxResult setex(String key, String value, int seconds) {
return AjaxResult.me().setSuccess(false).setMessage("Redis服务不可用");
}
}

四.缓存课程类型
1.依赖 hrm-redis-client模块
2.开启Feign

...
@EnableFeignClients("cn.itsource.hrm.client")
public class CourseServiceApplication2020{
...}

3.配置文件开启hystrix

...............
feign:
hystrix:
enabled: true #开启熔断支持
client:
config:
remote-service: #服务名,填写default为所有服务
connectTimeout: 30000
readTimeout: 30000
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 30000

4.修改coursetTypeService

public class CourseTypeServiceImpl extends ServiceImpl implements ICourseTypeService {

@Autowired
private RedisClient redisClient;

private List getAllCourseTypeFromRedis(){
//要返回的数据列表
List allCourseTypes = null;

//从redis中获取课程分类
AjaxResult ajaxResult = redisClient.get(RedisConstants.KEY_ALL_COURSE_TYPE);
//如果有数据
if(ajaxResult.isSuccess() && null != ajaxResult.getResultObj()){
String allCourseTypeJson = (String)ajaxResult.getResultObj();
if("null".equals(allCourseTypeJson)){
return new ArrayList();
}
//把json格式的课程分类转换成 List
allCourseTypes = JSON.parseArray(allCourseTypeJson, CourseType.class);

return allCourseTypes;
}
//如果redis中没有走msyql
//把查询到的数据放到redis
return restCourseTypeForRedis();
}


//重置redis中的课程分类
private List restCourseTypeForRedis(){
//如果redis中没有走msyql
List allCourseTypes = baseMapper.selectList(null);
if(allCourseTypes.size() == 0){
redisClient.setex(RedisConstants.KEY_ALL_COURSE_TYPE,"null",30);
}else{
//把查询到的数据放到redis
redisClient.set(RedisConstants.KEY_ALL_COURSE_TYPE,JSON.toJSONString(allCourseTypes));
}
return allCourseTypes;
}

5.添加,删除,修改方法重置Redis

@Override
public boolean updateById(CourseType entity) {
super.updateById(entity);
//重置redis中的课程分类
restCourseTypeForRedis();
return true;
}
...

三.机构类型
一.生成机构类型基本的代码
二.前台页面增加机构类型导航
三.前台修改机构类型页面
四.完成机构的分页查询

相关推荐

最新更新

猜你喜欢