有两种集成方式
- 将服务器引入到项目中,让它在应用程序的上下文中运行,通常被称为嵌入式(embedded)服务器。这意味着它们与您的应用程序共享同一个 Java 虚拟机(JVM)进程。这种方式更适合小型项目或希望简化部署和维护的情况。
- 如果选择远程连接,服务与应用程序分别在不同的进程中运行,这样可以更好地实现解耦和横向扩展,但可能需要更多的配置和管理。
redis和kafka都没有嵌入式的服务器版本,都只能通过连接redis进程,kafka进程的服务器。
redis键的命名规则
命名空间
项目名:表名:字段名:key
key=oauth:user:userName:haidong value=0227
Jedis: Jedis 类是主要的操作类,用于与 Redis 建立连接并执行各种操作,如设置值、获取值、执行命令等。
单例模式
public class JedisFactory {
private static JedisPool pool;
private static final JedisFactory instance=new JedisFactory();
static {
Properties properties=new Properties();
String host;
int port ;
int maxTotal ;
int maxWaitMillis;
Duration maxWaitMillisDuration;
int maxIdle ;
int minIdle;
InputStream inputStream= JedisFactory.class.getClassLoader().getResourceAsStream("jedis-config.properties");
try {
properties.load(inputStream);
host = properties.getProperty("redis.host", "localhost");
port = Integer.parseInt(properties.getProperty("redis.port", "6379"));
maxTotal = Integer.parseInt(properties.getProperty("redis.maxTotal", "10"));
maxIdle = Integer.parseInt(properties.getProperty("redis.maxIdle", "5"));
minIdle=Integer.parseInt(properties.getProperty("redis.minIdle","1"));
maxWaitMillis = Integer.parseInt(properties.getProperty("redis.maxWaitMillis", "10000"));
maxWaitMillisDuration=Duration.ofMillis(maxWaitMillis);
} catch (IOException e) {
throw new RuntimeException(e);
}
JedisPoolConfig poolConfig=new JedisPoolConfig();
poolConfig.setMinIdle(minIdle);
poolConfig.setMaxIdle(maxIdle);
poolConfig.setMaxTotal(maxTotal);
poolConfig.setMaxWait(maxWaitMillisDuration);
pool=new JedisPool(poolConfig,host,port);
}
private JedisFactory(){}
public static JedisFactory getInstance(){
return instance;
}
public Jedis getJedis(){
Jedis jedis=pool.getResource();
return jedis;
}
}