spring boot 集成apollo 快速指南

目前市面上流行的三大配置中心框架:Spring CLoud ConfigAlibaba Nacos 以及攜程apollo, 咱們相應架構組號召,就使用Apollo吧。java

Work Flow

圖1

簡單解釋:
上圖中有三套環境FATUATPROD,每一套環境都部署了2套ConfigserviceAdminservice.使用統一的Portal Server Cluster對全部環境進行配置管理。如咱們本身的配置環境:
圖2mysql

Meta-Server(Configservice)說白一點,就是Eureka discovery,每一套環境對應不一樣的一套meta server,以此來實現環境隔離,以下圖:
圖3git

#由於資源有限,所以我配置的三套環境使用同一臺數據庫,沒法實現環境隔離,可是不影響展現效果。
  local.meta=http://localhost:8080
  2 dev.meta=http://localhost:8080
  4 uat.meta=http://localhost:8080
  6 pro.meta=http://localhost:8080

如圖2所示。github

Configure Server

apollo 服務端主要有三個Spring Boot項目 和兩個數據庫組成:spring

  • apollo-configservice(默認端口:8080)

    提供配置的讀取、推送等功能,服務對象是Apollo客戶端sql

  • apollo-adminservice(默認端口:8090)

    提供配置的修改、發佈等功能,服務對象是Apollo Portal(管理界面)shell

  • apollo-portal(默認端口:8070)

    經過域名訪問Meta Server獲取Admin Service服務列表(IP+Port),然後直接經過IP+Port訪問服務,同時在Portal側會作load balance、錯誤重試數據庫

  • apolloportaldb
  • apolloconfigdb

由於Apollo官方文檔足夠詳細,想要了解的同窗直接Apollo官網傳送門bootstrap

可是又由於官方文檔太過詳細,以致於若是隻想部署的同窗可能以爲稍顯繁瑣,所以,我這裏直接開始部署服務端,就廢話少說了。springboot

  1. 第一步,下載https://github.com/ctripcorp/apollo/releases
  2. 第二步,下載adminserviceconfigserviceportal三個zip包以後,上傳到服務器。
  3. 第三部,在服務器中安裝mysql,並建立數據庫,腳本傳送門
  4. 第四步,分別配置三個Springboot服務並啓動,主要配置點有3處:
    1. 數據庫配置

      1 # DataSource
        2 spring.datasource.url = jdbc:mysql://localhost:3306/ApolloPortalDB?characterEncoding=utf8
        3 spring.datasource.username = wr
        4 spring.datasource.password = wr
    2. 日誌路徑
      • /根路徑/apollo-xxxx.conf
        properties 1 MODE=service 2 PID_FOLDER=. # 這裏 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
          #這裏
          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. 配置apollo-portal的meta service信息

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

      這裏也得修改apolloconfigdb數據庫中的表serverconfigapollo.portal.envs的配置爲:dev,uat,prod

而後apollo server就配置好了,分別啓動三個服務便可!訪問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 workflow

客戶端使用分步驟:訪問(http://172.16.28.177:8070/)

  1. 第一步:建立項目
    file
    建立以後,以下圖
    file

具體查看使用指南傳送門

上面不是我要說的重點,重點是如何在咱們項目中使用,以broadway-gateway爲例。

  • 第一步:建立bootstrap.yml,在application.yml加載以前要加載配置屬性

    app:
    #重點關聯,id必須是惟一的,每一個service各自不一樣
      id: P000000001 
    apollo:
      meta: http://172.16.28.177:8080/ # apollo-configservice
      bootstrap:
        enabled: true
        namespaces: application
  • 第二步:使用配置的變量

    eureka:
      instance:
        prefer-ip-address: true
      client:
        service-url:
        # 冒號後面的baidu.com是默認值,若是沒法鏈接apollo,可使用默認值防止出錯
          defaultZone: ${broad.gateway.eureka.server-list:http://baidu.com}

運行gateway,能夠看到以下結果:

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)
...

能夠看到,在springboot啓動以前,apollo首先加載配置信息了~

相關profile信息,後續再表,先改bug了~~

相關文章
相關標籤/搜索