Series of articles


@TOC


preface


I. Main points of this paper

We have integrated SpringBoot with Mybatis +Hikari+es+ Redis +kafka+ Dubbo. We have basically integrated the common components. This article will introduce the integration of Apollo configuration center. A complete catalog of articles in the series

  • Apollo configuration Center

  • Springboot integrates Apollo Client

  • springboot + mybatis + Hikari + elasticsearch + redis + dubbo + apollo

Second, development environment

  • JDK 1.8
  • Maven 3.6.2
  • Springboot 2.4.3
  • Apollo, version 1.8.1
  • Apollo cleint 1.8.0 comes with
  • idea 2020

Install Apollo server

Reference: “Build large Distributed Services (15) Docker build development environment installation Apollo”

Modify the POM. XML file


      
<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 - the 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 comes with</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>

Copy the code

Modify the configuration file

Modify the application-dev.properties file, and also modify the corresponding configuration file for later release to test or formal environments. Add Apollo configuration here.

#################### APOLLO ####################
app.id=member-config
apollo.meta=http://9.135.xx.xxx:8080 # Set up the Eureka address of Apollo
apollo.bootstrap.enabled=true
apollo.bootstrap.eagerLoad.enabled=true
logging.level.root=Debug # Trace Apollo logs
Copy the code

Vi. Modify the project code

Modify ApolloDemoApplication @enableApolloConfig to enable configuration center.

@EnableApolloConfig
@SpringBootApplication
public class ApolloDemoApplication {

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

2, Add IndexController, read Apollo configuration.

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

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

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

        returnversion; }}Copy the code

Add member. Version configuration item to Apollo development environment.

Seven, run it

1. Run the project and observe the startup log. You can see that Apollo has been connected properly.

2021-05-04 17:45:41937. 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 & notifications 5 b = % 22% % 7 b % 22 namespacename % 3 a % 22 application % 22% 2 c % 22 noti ficationId%22%3A-1%7D%5D
2021-05-04 17:45:41938. 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=1043.30.35.&notifications=%5B%7B%22namespaceName%22%3A%22application%22%2C%22notificationId%22%3A-1%7D%5D HTTP/11.: null}{User-Agent: Java/18.. 0 _221}{Host: 9135..xxx.xxx:8080}{Accept: text/html, image/gif, image/jpeg, *; q=2., */ *; q=.2}{Connection: Keep - alive} 17:45:41 2021-05-04. 3443-951 the DEBUG [ngPollService - 1] S.N.W ww. 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} {the set - cookies: x_host_key=17936c4ddba-9fefb9fb9a64eaca5b786ac29a6405288daca4f6; path=/; HttpOnly}{x-rio-seq: 17:45:41 ko9ujah0-166548695} 2021-05-04. 3443-952 the DEBUG [ngPollService - 1] C.C.F.A.I.R emoteConfigLongPollService: Long polling response: 200, url: http://9.135.xxx.xxx:8080/notifications/v2? Cluster = default&appId = member - config&ip = 10.43.30.35 & notifications 5 b = % 22% % 7 b % 22 namespacename % 3 a % 22 application % 22% 2 c % 22 noti ficationId%22%3A-1%7D%5DCopy the code

2, visit the localhost: 8080 / API/member/version.

3, Modify Apollo configuration, change member.version to 2.1 and publish configuration. Note that there is no need to restart the project.4. Looking at the project log, you can see that the application immediately senses that the configuration has been changed and has been updated again.

2021-05-04 17:52:58531. 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=21., tips=hello javaer.}, releaseKey='20210504175258-6c23026d6e0625ab'}
2021-05-04 17:52:58532. DEBUG 3443 --- [figRepository-1] c.c.f.a.i.RemoteConfigRepository         : Remote Config refreshed!
2021-05-04 17:52:58537. 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:58538. 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:58538.  INFO 3443 --- [Apollo-Config-1] c.f.a.s.p.AutoUpdateConfigChangeListener : Auto update apollo changed value successfully, new value: 21., key: member.version.Copy the code

5, to visit the localhost: 8080 / API/member/version, configuration has been updated.

Eight, summary

This is just a brief introduction to how to integrate Apollo. For more details on how to host configurations such as Redis, Kafka, ES, dubbo, etc. SpringBoot Project Configuration hosted by Apollo

Add me to exchange learning!