1. 程式人生 > >Apollo Java客戶端

Apollo Java客戶端

1.指定環境
C:\opt\settings\server.properties
env=DEV
2.指定APP.ID
在classpath:/META-INF/app.properties檔案中指定
app.id=100000001
這裡寫圖片描述
3.pom.xml

<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> <groupId>com.travelsky.pss</groupId> <artifactId>apollo-learn</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>apollo-learn</name> <url>http://maven.apache.org</url
>
<properties> <java.version>1.7</java.version> <github.path>${project.artifactId}</github.path> <spring-demo.version>4.3.6.RELEASE</spring-demo.version> <spring-boot.version>1.3.8.RELEASE</spring-boot.version> <spring-cloud.version
>
1.2.3.RELEASE</spring-cloud.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring-demo.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring-demo.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring-demo.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring-demo.version}</version> <exclusions> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring-demo.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-asm</artifactId> <version>${spring-demo.version}</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2.2</version> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>com.ctrip.framework.apollo</groupId> <artifactId>apollo-client</artifactId> <version>0.9.1</version> </dependency> <!-- for spring demo --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <!-- for spring boot demo --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>${spring-boot.version}</version> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> <version>${spring-boot.version}</version> </dependency> <!-- for refresh scope demo --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-context</artifactId> <version>${spring-cloud.version}</version> </dependency> <!--required for spring 3.1.0--> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.4.1</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.4.1</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>2.4.1</version> </dependency> <!-- take over jcl --> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>1.7.21</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.7</version> </dependency> </dependencies> </project>

4.API方式
API方式獲取實時結果,因此不需要快取
(1)程式碼

new ClassPathXmlApplicationContext(
                "application.xml");
Config config = ConfigService.getAppConfig(); //config instance is singleton for each namespace and is never null
String someKey = "redis.ip";
String someDefaultValue = "127.0.0.1";
String value = config.getProperty(someKey, someDefaultValue);
System.out.println(value);

(2)管理中心配置
這裡寫圖片描述
(3)執行結果
這裡寫圖片描述
5.基於java config方式的配置動態更新
管理中心修改配置後,實時更新配置資訊
(1)程式碼

@RefreshScope
@Component("redisConfig")
public class RedisConfig {
    @Value("${redis.ip:127.0.0.1}")
    private String ip;
    public String getIp() {
        return ip;
    }
    public void setIp(String ip) {
        this.ip = ip;
    }
    @PreDestroy
    public void destroy(){
        System.out.println("destroy");
    }
}
@Configuration
@EnableApolloConfig
//@Import(RefreshAutoConfiguration.class)
public class AppConfig {
}
@Component
public class ApolloRefreshConfig {
  private static final Logger logger = LoggerFactory.getLogger(ApolloRefreshConfig.class);
  @Autowired
  private RefreshScope refreshScope;
  @ApolloConfigChangeListener("application")
  private void onChange(ConfigChangeEvent changeEvent) {
    if (changeEvent.isChanged("redis.ip")) {
      //could also call refreshScope.refreshAll();
//      refreshScope.refresh("redisConfig");
      refreshScope.refreshAll();
    }
  }
}
public class ApolloConfigCenter {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext(
                "application.xml");
        RedisConfig config = ac.getBean(RedisConfig.class);
        while (true){
            try {
                System.out.println(config.getIp());
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

(2)application.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:apollo="http://www.ctrip.com/schema/apollo" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.ctrip.com/schema/apollo http://www.ctrip.com/schema/apollo.xsd
       http://www.springframework.org/schema/aop  
        http://www.springframework.org/schema/aop/spring-aop.xsd">
    <apollo:config order="10" />
    <context:annotation-config />
    <context:component-scan base-package="com.travelsky.pss" />
    <!-- to support RefreshScope -->
    <bean class="org.springframework.cloud.autoconfigure.RefreshAutoConfiguration"/>
</beans>

(3)配置中心
這裡寫圖片描述
(4)修改配置併發布
這裡寫圖片描述
(5)監控結果
這裡寫圖片描述
6.基於XML方式
(1)程式碼

public class XmlRedisConfig {
    private String ip;
    public String getIp() {
        return ip;
    }
    public void setIp(String ip) {
        this.ip = ip;
    }
}

//實時重新整理

public class ManualRefreshUtil {
  private static final Logger logger = LoggerFactory.getLogger(ManualRefreshUtil.class);
  @ApolloConfig
  private Config config;
  @Autowired
  private XmlRedisConfig redisConfig;
  @ApolloConfigChangeListener
  private void onChange(ConfigChangeEvent changeEvent) {
    if (changeEvent.isChanged("redis.ip")) {
      logger.info("Manually refreshing redisConfig.ip");
      redisConfig.setIp(config.getProperty("redis.ip", redisConfig.getIp()));
    }
  }
}

(2)application.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:apollo="http://www.ctrip.com/schema/apollo" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.ctrip.com/schema/apollo http://www.ctrip.com/schema/apollo.xsd
       http://www.springframework.org/schema/aop  
        http://www.springframework.org/schema/aop/spring-aop.xsd">
    <apollo:config order="10" />
    <context:annotation-config />
    <context:component-scan base-package="com.travelsky.pss" />
    <bean id="xmlRedisConfig" class="com.travelsky.pss.xml.bean.XmlRedisConfig">
        <property name="ip" value="${redis.ip}" />
    </bean>

    <bean class="com.travelsky.pss.xml.refresh.ManualRefreshUtil"/>
</beans>

(3)配置中心
這裡寫圖片描述
(4)修改配置併發布
這裡寫圖片描述
(5)監控結果
這裡寫圖片描述