Quick guide to integrating apollo with spring boot

Keywords: Programming Spring encoding Database Java

>At present, there are three popular configuration center frameworks on the market: Spring CLoud Config ,Alibaba Nacos as well as Ctrip apollo , we call on the corresponding architecture group to use Apollo.

Work Flow

Simple explanation: In the above figure, there are three environments FAT, UAT and PROD, each of which has two sets of Configservice and Adminservice deployed. Such as our own configuration environment:

To put it bluntly, the meta server (Configservice) is Eureka discovery. Each set of environment corresponds to a different set of meta servers to realize environment isolation, as shown in the following figure:

  #Because of the limited resources, the three environments I configured use the same database, which can not achieve environmental isolation, but does not affect the display effect.
  local.meta=http://localhost:8080
  2 dev.meta=http://localhost:8080
  4 uat.meta=http://localhost:8080
  6 pro.meta=http://localhost:8080

As shown in Figure 2.

Configure Server

The apollo server consists of three Spring Boot projects and two databases:

  • Apollo configservice (default port: 8080)

    >It provides read, push and other functions of configuration. The service object is Apollo client
  • Apollo adminservice (default port: 8090)

    >Provide configuration modification, publishing and other functions. The service object is Apollo Portal (Management Interface)
  • Apollo portal (default port: 8070)

    >Access the Meta Server through the domain name to obtain the Admin Service list (IP+Port), and then directly access the service through IP+Port. At the same time, load balance and error retry will be done on the Portal side
  • apolloportaldb
  • apolloconfigdb

Because the official documents of Apollo are detailed enough, the students who want to know are directly Apollo official website portal

But because the official documents are too detailed, the students who just want to deploy may feel a little cumbersome. Therefore, I start to deploy the server directly here, and I will not talk about it.

  1. Step 1, Download https://github.com/ctripcorp/apollo/releases
  2. Step 2: download the three zip packages of adminservice, configservice and portal and upload them to the server.
  3. The third part is to install mysql in the server and create a database, Script portal
  4. Step 4: configure and start three spring boot services respectively. There are three main configuration points:
    1. Database configuration

        1 # DataSource
        2 spring.datasource.url = jdbc:mysql://localhost:3306/ApolloPortalDB?characterEncoding=utf8
        3 spring.datasource.username = wr
        4 spring.datasource.password = wr
      
    2. Log path

      • (/ root path / apollo-xxxx.conf)
          1 MODE=service
          2 PID_FOLDER=.
          # Here
          3 LOG_FOLDER=/home/jing/software/apollo-portal-1.5.1/logs/100003173/
        
      • /script/startup.sh
          1 #!/bin/bash
          2 SERVICE_NAME=apollo-portal
        

      3 ## Adjust log dir if necessary Here we are. 4 LOG_DIR=/home/jing/software/apollo-portal-1.5.1/logs/100003173 5 ## Adjust server port if necessary 6 SERVER_PORT=${SERVER_PORT:=8070}

    3. Configure the meta service information of Apollo portal

        local.meta=http://localhost:8080
        2 dev.meta=http://localhost:8080
        4 uat.meta=http://localhost:8080
        6 pro.meta=http://localhost:8080
      

      The configuration of apollo.portal.envs in the serverconfig table in the Apollo configdb database must also be modified as: dev,uat,prod

Then the apollo server is configured, and you can start three services respectively! Visit http://172.16.28.177:8070/

jing@sysdep:~/software/apollo-adminservice-1.5.1$ ps -ef|grep apollo
jing     25348     1 19 08:12 pts/2    00:01:31 /home/jing/.jenv/versions/1.8/bin/java -Dsun.misc.URLClassPath.disableJarChecking=true -XX:ParallelGCThreads=4 -XX:MaxTenuringThreshold=9 -XX:+DisableExplicitGC -XX:+ScavengeBeforeFullGC -XX:SoftRefLRUPolicyMSPerMB=0 -XX:+ExplicitGCInvokesConcurrent -XX:+HeapDumpOnOutOfMemoryError -XX:-OmitStackTraceInFastThrow -Duser.timezone=Asia/Shanghai -Dclient.encoding.override=UTF-8 -Dfile.encoding=UTF-8 -Djava.security.egd=file:/dev/./urandom -Dserver.port=8080 -Dlogging.file=/home/jing/software/apollo-configservice-1.5.1/logs/100003171/apollo-configservice.log -XX:HeapDumpPath=/home/jing/software/apollo-configservice-1.5.1/logs/100003171/HeapDumpOnOutOfMemoryError/ -XX:+UseParNewGC -Xloggc:/home/jing/software/apollo-configservice-1.5.1/logs/100003171/gc.log -XX:+PrintGCDetails -XX:+UseConcMarkSweepGC -XX:+UseCMSCompactAtFullCollection -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=60 -XX:+CMSClassUnloadingEnabled -XX:+CMSParallelRemarkEnabled -XX:CMSFullGCsBeforeCompaction=9 -XX:+CMSClassUnloadingEnabled -XX:+PrintGCDateStamps -XX:+PrintGCApplicationConcurrentTime -XX:+PrintHeapAtGC -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=5M -jar /home/jing/software/apollo-configservice-1.5.1/apollo-configservice.jar
jing     27274     1 37 08:17 pts/2    00:01:23 /home/jing/.jenv/versions/1.8/bin/java -Dsun.misc.URLClassPath.disableJarChecking=true -XX:ParallelGCThreads=4 -XX:MaxTenuringThreshold=9 -XX:+DisableExplicitGC -XX:+ScavengeBeforeFullGC -XX:SoftRefLRUPolicyMSPerMB=0 -XX:+ExplicitGCInvokesConcurrent -XX:+HeapDumpOnOutOfMemoryError -XX:-OmitStackTraceInFastThrow -Duser.timezone=Asia/Shanghai -Dclient.encoding.override=UTF-8 -Dfile.encoding=UTF-8 -Djava.security.egd=file:/dev/./urandom -Dserver.port=8090 -Dlogging.file=/home/jing/software/apollo-adminservice-1.5.1/logs/100003172/apollo-adminservice.log -XX:HeapDumpPath=/home/jing/software/apollo-adminservice-1.5.1/logs/100003172/HeapDumpOnOutOfMemoryError/ -XX:+UseParNewGC -Xloggc:/home/jing/software/apollo-adminservice-1.5.1/logs/100003172/gc.log -XX:+PrintGCDetails -XX:+UseConcMarkSweepGC -XX:+UseCMSCompactAtFullCollection -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=60 -XX:+CMSClassUnloadingEnabled -XX:+CMSParallelRemarkEnabled -XX:CMSFullGCsBeforeCompaction=9 -XX:+CMSClassUnloadingEnabled -XX:+PrintGCDateStamps -XX:+PrintGCApplicationConcurrentTime -XX:+PrintHeapAtGC -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=5M -jar /home/jing/software/apollo-adminservice-1.5.1/apollo-adminservice.jar
jing     28964     1 99 08:19 pts/3    00:01:00 /home/jing/.jenv/versions/1.8/bin/java -Dsun.misc.URLClassPath.disableJarChecking=true -XX:ParallelGCThreads=4 -XX:MaxTenuringThreshold=9 -XX:+DisableExplicitGC -XX:+ScavengeBeforeFullGC -XX:SoftRefLRUPolicyMSPerMB=0 -XX:+ExplicitGCInvokesConcurrent -XX:+HeapDumpOnOutOfMemoryError -XX:-OmitStackTraceInFastThrow -Duser.timezone=Asia/Shanghai -Dclient.encoding.override=UTF-8 -Dfile.encoding=UTF-8 -Djava.security.egd=file:/dev/./urandom -Dserver.port=8070 -Dlogging.file=/home/jing/software/apollo-portal-1.5.1/logs/100003173/apollo-portal.log -XX:HeapDumpPath=/home/jing/software/apollo-portal-1.5.1/logs/100003173/HeapDumpOnOutOfMemoryError/ -XX:+UseParNewGC -Xloggc:/home/jing/software/apollo-portal-1.5.1/logs/100003173/gc.log -XX:+PrintGCDetails -XX:+UseConcMarkSweepGC -XX:+UseCMSCompactAtFullCollection -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=60 -XX:+CMSClassUnloadingEnabled -XX:+CMSParallelRemarkEnabled -XX:CMSFullGCsBeforeCompaction=9 -XX:+CMSClassUnloadingEnabled -XX:+PrintGCDateStamps -XX:+PrintGCApplicationConcurrentTime -XX:+PrintHeapAtGC -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=5M -jar /home/jing/software/apollo-portal-1.5.1/apollo-portal.jar

Configure Client

Client usage steps: access( http://172.16.28.177:8070/)

  1. Step 1: create a project After creation, as shown below

Specific view Use guide portal

The above is not what I want to say, but how to use it in our project. Take Broadway gateway as an example.

  • Step 1: create bootstrap.yml and load configuration properties before application.yml is loaded

    app:
    #Key Association. id must be unique. Each service is different
      id: P000000001 
    apollo:
      meta: http://172.16.28.177:8080/ # apollo-configservice
      bootstrap:
        enabled: true
        namespaces: application
    
  • Step 2: use the configured variables

    eureka:
      instance:
        prefer-ip-address: true
      client:
        service-url:
        # baidu.com after the colon is the default value. If you cannot connect to apollo, you can use the default value to prevent errors
          defaultZone: ${broad.gateway.eureka.server-list:http://baidu.com}
    

Running gateway, you can see the following results:

2020-01-17 17:06:56.623  INFO 12444 --- [           main] c.c.f.f.i.p.DefaultApplicationProvider   : App ID is set to P000000001 by app.id property from System Property
2020-01-17 17:06:56.634  INFO 12444 --- [           main] c.c.f.f.i.p.DefaultServerProvider        : Environment is set to null. Because it is not available in either (1) JVM system property 'env', (2) OS env variable 'ENV' nor (3) property 'env' from the properties InputStream.
2020-01-17 17:06:56.711  INFO 12444 --- [           main] c.c.f.a.i.DefaultMetaServerProvider      : Located meta services from apollo.meta configuration: http://172.16.28.177:8080/!
2020-01-17 17:06:56.719  INFO 12444 --- [           main] c.c.f.apollo.core.MetaDomainConsts       : Located meta server address http://172.16.28.177:8080/ for env UNKNOWN from com.ctrip.framework.apollo.internals.DefaultMetaServerProvider
2020-01-17 17:06:57.761  INFO 12444 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$bc952272] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::       (v2.1.10.RELEASE)
...

As you can see, before spring boot, apollo first loads the configuration information ~

The related profile information will be listed later, and the bug will be changed first

Running life | Blog Garden | segmentfault | spring4all | csdn | Nuggets | OSChina | Brief book | Headlines | Know about | 51CTO

Posted by sbinkerd1 on Sun, 19 Jan 2020 18:53:25 -0800