要在Redisson中使用Apollo配置,需要按照以下步驟進行配置:
<dependencies>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.16.2</version>
</dependency>
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>1.7.0</version>
</dependency>
</dependencies>
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigService;
public class ApolloConfig {
private static final String REDIS_HOST_KEY = "redis.host";
private static final String REDIS_PORT_KEY = "redis.port";
private static final String DEFAULT_REDIS_HOST = "localhost";
private static final int DEFAULT_REDIS_PORT = 6379;
private static Config config = ConfigService.getAppConfig();
public static String getRedisHost() {
return config.getProperty(REDIS_HOST_KEY, DEFAULT_REDIS_HOST);
}
public static int getRedisPort() {
return config.getIntProperty(REDIS_PORT_KEY, DEFAULT_REDIS_PORT);
}
}
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
public class RedissonConfig {
public static RedissonClient getRedissonClient() {
Config config = new Config();
config.useSingleServer()
.setAddress("redis://" + ApolloConfig.getRedisHost() + ":" + ApolloConfig.getRedisPort());
return Redisson.create(config);
}
}
import org.redisson.api.RedissonClient;
public class MyApplication {
private static RedissonClient redissonClient;
public static void main(String[] args) {
// 初始化Redisson
redissonClient = RedissonConfig.getRedissonClient();
// 使用Redisson
// ...
// 關(guān)閉Redisson
redissonClient.shutdown();
}
}
通過以上步驟,您就可以在Redisson中使用Apollo配置了。