spring boot整合apollo

时间:2022-07-23
本文章向大家介绍spring boot整合apollo,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

由于apollo是提供配置管理的服务,即项目的配置需要统一存放在apollo上进行管理。对于单体项目来说需要与apollo进行通信并获取项目本身需要的配置信息。所以我们需要使用apollo提供的客户端apollo-client用于配置的获取和装配,以下详细介绍整合的过程步骤。

Springboot整合apollo

1.在pom.xml中导入apollo-client.jar

       <dependency>
           <groupId>com.ctrip.framework.apollo</groupId>
           <artifactId>apollo-client</artifactId>
           <version>1.3.0</version>
       </dependency>

        <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-context</artifactId>
           <version>2.0.1.RELEASE</version>
       </dependency>

2. 创建META-INF文件夹,在该文件夹中创建app.properties文件。

3. 在app.properties文件中添加app.id= (您要拉去的配置中心项目的appid)

问题:apollo在这里写appid的意义?

答:用于apollo配置中心与应用本身拉去配置的识别,不至于拉去不需要的配置文件,apollo-client会默认读取/META-INF/app.properties文件中的app.id属性。注意:这里的app.id的值不能用引号括起来。

4. 在项目的yml文件中配置apollo配置集群内治的注册中心

1.dev config 获取dev环境的配置

apollo:
 meta: http://localhost:8080
 bootstrap:
   enabled: true
   eagerLoad:
     enabled: true
   namespaces: application,TEST1.curd-common

meta是apollo配置中心内置的注册中心地址。这里有dev、fat、uat、pro四个环境。分别对应的地址为:

开发环境 dev:http://127.0.0.1:8080

测试环境 fat: http://127.0.0.1:8180

渗透环境 uat:http://127.0.0.1:8280

生产环境 pro: http://127.0.0.1:8380

5. 打包发版本

打包发版的时候如何发?我本地测试和线上测试来回改,就比较太麻烦了。所以我们pom文件中设置打包的相关配置。

1.在编译的时候指定maven可以扫描到的文件夹。所以在<build>标签里添加资源路径。

  <resources>
           <resource>
               <directory>src/main/resources</directory>     //这里文件夹
               <filtering>true</filtering>     //过滤
                <includes>
                   <include>mapper/*.xml</include>  //将mybats生成的xml打入包内
                    <include>META-INF/*.properties</include>  //打入app.properties
                   <include>application.properties</include>  //打入项目的application
                   <include>static/**</include>       //打入静态资源
                   <include>templates/**</include>   //打入拦截器拦截的目录(index)
                </includes>
           </resource>
       </resources>

2.设置打包的配置

   <profiles>
       <profile>
           <id>dev</id>    
           <properties>
               <defaultApollo>http://127.0.0.1:8080</defaultApollo>
           </properties>
           <activation>
               <activeByDefault>true</activeByDefault>
           </activation>
       </profile>
       <profile>
           <id>fat</id>
           <properties>
                <defaultApollo>http://127.0.0.1:8180</defaultApollo>
           </properties>
       </profile>
       <profile>
           <id>uat</id>
           <properties>
               <defaultApollo>http://127.0.0.1:8280</defaultApollo>
           </properties>
       </profile>
       <profile>
           <id>pro</id>
           <properties>
               <defaultApollo>http://127.0.0.1:8380</defaultApollo>
           </properties>
       </profile>
</profiles>

6. 开启apollo服务

在启动类上添加注解:@EnableApolloConfig

7. 测试从apollo配置中心拉去的配置

1.编写获取apollo配置的controller

/**
 *apollo测试
 */
@RequestMapping(value ="/apollo")
@RestController
public class AolloTestController {
 
   @Value(value = "${application:获取私有配置失败}")
   private String application;
 
   @Value(value = "${common:获取共有配置失败}")
   private String common;
 
   //获取私有的application的配置
   @ApolloConfig(value = "application")
   private Config applicationConfig;
 
   //获取公共配置
   @ApolloConfig(value = "TEST1.curd-common")
   private Config commonConfig;
 
   /**
    * 测试获取到的apollo信息
    */
   @GetMapping(value = "/config")
   public StringBuffer getController() {
       StringBuffer stringBuffer=new StringBuffer();
       Set<String> properties =applicationConfig.getPropertyNames();
       for (String property : properties) {
           System.err.println(property + "=" +applicationConfig.getProperty(property,"123"));
           stringBuffer.append(property + "=" + applicationConfig.getProperty(property,"123")+"n");
       }
       Set<String> commonProperties=commonConfig.getPropertyNames();
       commonProperties.forEach(key->{
           System.err.println(key+"="+commonConfig.getProperty(key,"1"));
           stringBuffer.append(key +"=" + commonConfig.getProperty(key,"3")+"n");
       });
       return stringBuffer;
    }
 
}

2.启动项目

在浏览器中打开http://localhost:9001/apollo/config

测试apollo拉取配置中心的内容。

后台打印的配置信息:

获取apollo配置成功!

8. 修改配置文件热发布

测试配置对象得配置

@Data  //get、set
@RefreshScope   //必要,否则spring 容器刷新不了
@AllArgsConstructor   //必要的
@NoArgsConstructor   //必要的
@Configuration     //  加载到容器
@ConfigurationProperties(prefix = "com.test.demo")  
public class ApolloRefresh {
    private String wcfService;
    private String cnkiService;
}

9. 热发布监听

Apollo能通过长连接来监听配置文件得修改,并能发布到应用中,对一般得属性,apollo是热发布得,但是对对象类型得配置bean来说,需要刷新spring ioc容器。下面是该demo的热发布测试代码。

 
@Component
public class ContextApolloRefresh {
    /**
     * apollo配置更新时,刷新applicationcontext容器
     */
    private final RefreshScope refreshScope;
    /**
     * 注入测试,打印修改前后得配置
     */
    @Autowired
    private ApolloRefresh apolloRefresh;
    /**
     * 初始化刷新
     * @param refreshScope
     */
    public ContextApolloRefresh(RefreshScope refreshScope) {
        this.refreshScope = refreshScope;
    }
    /**
     * apollo配置监听
     * @param changeEvent
     */
    @ApolloConfigChangeListener(value = {"application", "TEST1.apollo"},interestedKeyPrefixes = {"net.cnki.demo"})
    public void onChange(ConfigChangeEvent changeEvent) {
        /**
         * 这是可以打印出刷新前读取配置
         */
        System.out.println(apolloRefresh.toString());
        refreshScope.refreshAll();
        System.out.println(apolloRefresh.toString());
        /**
         * 这里可以打印刷新后读取的配置
         */
    }
}

在apollo中修改配置之后,热发布前后打印的属性:

至此apollo整合的基本过程完毕,包含热发布和基本的配置内容,谢谢观看!