redisson怎么使用apollo配置

avatar
作者
猴君
阅读量:2

要在Redisson中使用Apollo配置,需要按照以下步骤进行配置:

  1. 添加Maven依赖:
<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> 
  1. 创建Apollo配置类:
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);     } } 
  1. 使用Apollo配置初始化Redisson:
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);     } } 
  1. 在应用程序中使用Redisson:
import org.redisson.api.RedissonClient;  public class MyApplication {     private static RedissonClient redissonClient;          public static void main(String[] args) {         // 初始化Redisson         redissonClient = RedissonConfig.getRedissonClient();                  // 使用Redisson         // ...                  // 关闭Redisson         redissonClient.shutdown();     } } 

通过以上步骤,您就可以在Redisson中使用Apollo配置了。

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!