系列文章目录


springboot单体项目 接入阿波罗 apollo springboot整合_springboot



文章目录

  • 系列文章目录
  • 前言
  • 一、本文要点
  • 二、开发环境
  • 三、安装apollo服务端
  • 四、修改pom.xml文件
  • 五、修改配置文件
  • 六、修改项目代码
  • 七、运行一下
  • 八、小结



前言


Apollo(阿波罗)是携程框架部门研发的开源配置管理中心,能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性。使用配置中心,可以更加集中化方便管理项目的配置,脱敏安全问题,热更新等好处。


一、本文要点

接上文,我们已经把SpringBoot整合mybatis+Hikari+es+redis+kafka+dubbo了,基本上已经把常用的组件整合完毕,本文将介绍整合apollo配置中心。系列文章完整目录

  • Apollo 配置中心
  • springboot 整合 apollo client
  • springboot + mybatis + Hikari + elasticsearch + redis + dubbo + apollo

二、开发环境

  • jdk 1.8
  • maven 3.6.2
  • springboot 2.4.3
  • apollo 1.8.1
  • apollo cleint 1.8.0
  • idea 2020

三、安装apollo服务端

参考:《搭建大型分布式服务(十五)Docker搭建开发环境安装Apollo》

四、修改pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.mmc.lesson</groupId>
    <artifactId>apollo-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>apollo-demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


        <!-- https://mvnrepository.com/artifact/com.ctrip.framework.apollo/apollo-client -->
        <dependency>
            <groupId>com.ctrip.framework.apollo</groupId>
            <artifactId>apollo-client</artifactId>
            <version>1.8.0</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
            <scope>test</scope>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

五、修改配置文件

修改application-dev.properties文件,同理,后面发布到测试、正式环境的话,修改对应的配置文件。这里增加Apollo的配置。

#################### APOLLO ####################
app.id=member-config
apollo.meta=http://9.135.xx.xxx:8080  # 搭建apollo的eureka地址
apollo.bootstrap.enabled=true
apollo.bootstrap.eagerLoad.enabled=true
logging.level.root=debug  # 跟踪一下apollo日志

六、修改项目代码

1、修改ApolloDemoApplication,@EnableApolloConfig启用配置中心。

@EnableApolloConfig
@SpringBootApplication
public class ApolloDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(ApolloDemoApplication.class, args);
    }

}

2、增加IndexController,读取apollo配置。

@RequestMapping("/api")
@RestController
public class IndexController {

    @Value("${member.version}")
    private String version;

    @GetMapping("/member/version")
    public String version() {

        return version;
    }
}

3、在apollo开发环境增加member.version配置项。

springboot单体项目 接入阿波罗 apollo springboot整合_apollo_02

七、运行一下

1、运行项目,观察启动日志,可以看到已经能正常连接apollo了。

2021-05-04 17:45:41.937 DEBUG 3443 --- [ngPollService-1] c.c.f.a.i.RemoteConfigLongPollService    : Long polling from http://9.135.xxx.xxx:8080/notifications/v2?cluster=default&appId=member-config&ip=10.43.30.35¬ifications=%5B%7B%22namespaceName%22%3A%22application%22%2C%22notificationId%22%3A-1%7D%5D
2021-05-04 17:45:41.938 DEBUG 3443 --- [ngPollService-1] s.n.www.protocol.http.HttpURLConnection  : sun.net.www.MessageHeader@4210fcbe5 pairs: {GET /notifications/v2?cluster=default&appId=member-config&ip=10.43.30.35¬ifications=%5B%7B%22namespaceName%22%3A%22application%22%2C%22notificationId%22%3A-1%7D%5D HTTP/1.1: null}{User-Agent: Java/1.8.0_221}{Host: 9.135.xxx.xxx:8080}{Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2}{Connection: keep-alive}
2021-05-04 17:45:41.951 DEBUG 3443 --- [ngPollService-1] s.n.www.protocol.http.HttpURLConnection  : sun.net.www.MessageHeader@36a60b1e16 pairs: {null: HTTP/1.1 200 OK}{x-proxy-by: SmartProxy (IDC-CDN-Gate)}{X-Content-Type-Options: nosniff}{X-XSS-Protection: 1; mode=block}{Cache-Control: no-cache, no-store, max-age=0, must-revalidate}{Pragma: no-cache}{Expires: 0}{X-Frame-Options: SAMEORIGIN}{Content-Type: application/json;charset=UTF-8}{Transfer-Encoding: chunked}{Date: Tue, 04 May 2021 09:45:42 GMT}{Keep-Alive: timeout=60}{Connection: keep-alive}{x-forwarded-for: 9.135.xxx.xxx}{set-cookie: x_host_key=17936c4ddba-9fefb9fb9a64eaca5b786ac29a6405288daca4f6; path=/; HttpOnly}{x-rio-seq: ko9ujah0-166548695}
2021-05-04 17:45:41.952 DEBUG 3443 --- [ngPollService-1] c.c.f.a.i.RemoteConfigLongPollService    : Long polling response: 200, url: http://9.135.xxx.xxx:8080/notifications/v2?cluster=default&appId=member-config&ip=10.43.30.35¬ifications=%5B%7B%22namespaceName%22%3A%22application%22%2C%22notificationId%22%3A-1%7D%5D

2、访问一下localhost:8080/api/member/version。

springboot单体项目 接入阿波罗 apollo springboot整合_apollo_03

3、修改apollo配置,把member.version改为2.1并发布配置,注意不用重启项目。

springboot单体项目 接入阿波罗 apollo springboot整合_spring_04


4、观察项目日志,可以看到应用立刻感知到配置被修改,已经重新更新了。

2021-05-04 17:52:58.531 DEBUG 3443 --- [figRepository-1] c.c.f.a.i.RemoteConfigRepository         : Loaded config for application: ApolloConfig{appId='member-config', cluster='default', namespaceName='application', configurations={member.version=2.1, tips=hello javaer.}, releaseKey='20210504175258-6c23026d6e0625ab'}
2021-05-04 17:52:58.532 DEBUG 3443 --- [figRepository-1] c.c.f.a.i.RemoteConfigRepository         : Remote Config refreshed!
2021-05-04 17:52:58.537 DEBUG 3443 --- [Apollo-Config-1] o.s.c.e.PropertySourcesPropertyResolver  : Found key 'member.version' in PropertySource 'ApolloBootstrapPropertySources' with value of type String
2021-05-04 17:52:58.538 DEBUG 3443 --- [Apollo-Config-1] o.s.c.e.PropertySourcesPropertyResolver  : Found key 'member.version' in PropertySource 'environmentProperties' with value of type String
2021-05-04 17:52:58.538  INFO 3443 --- [Apollo-Config-1] c.f.a.s.p.AutoUpdateConfigChangeListener : Auto update apollo changed value successfully, new value: 2.1, key: member.version,

5、重新访问一下localhost:8080/api/member/version,配置已更新。

springboot单体项目 接入阿波罗 apollo springboot整合_spring_05

八、小结

这里只是简单介绍如何整合apollo,更加详细的用法请关注后续文章,例如怎样把redis、kafka、es、dubbo等配置交由apollo托管。下一篇《搭建大型分布式服务(十七)SpringBoot项目配置交由Apollo托管》