Apollo:分布式配置中心


參考文章:https://www.cnblogs.com/zhangyjblogs/p/14163702.html

一、官方參考指南

https://github.com/ctripcorp/apollo/wiki/%E5%88%86%E5%B8%83%E5%BC%8F%E9%83%A8%E7%BD%B2%E6%8C%87%E5%8D%97#

二、執行流程

執行流程圖如下:

流程說明:

A:用戶在配置中心發布或修改配置

B:Apollo客戶端定時拉取配置中心的配置。若配置中心發生故障,則apollo客戶端會從本地緩存中獲取配置信息

C:應用程序從客戶端獲取配置信息並更新通知

D:除了apollo客戶端定時從配置中心拉取之外,配置中心還可以實時的推送配置更新信息到apollo客戶端,那么客戶端再把配置給應用程序。

本地文件緩存位置在:C:\opt\data目錄下。可以通過獲取別人的本地文件中的配置信息,然后將配置一個接一個的拷貝到自己的apollo中。

注意:拷貝的時候要刪除轉義符\,否則,你本地生成的配置文件就會有三個\\\,導致你找不到數據庫。

三、虛擬機部署

1、創建數據庫

Apollo的唯一依賴是數據庫,所以需要首先把數據庫准備好。Apollo服務端共需要兩個數據庫:ApolloPortalDBApolloConfigDB

1)、創建ApolloPortalDB數據庫

執行apolloportaldb.sql腳本

2)、創建ApolloConfigDB數據庫

執行apolloConfigdb.sql腳本

注意:apollo-configservice不以8080端口啟動的情況下,要修改ApolloConfigDB庫的ServerConfig表的eureka.service.url的value值為http://localhost:18080/eureka/,我是單機環境,因此是localhost,端口從數據庫默認的8080改為18080

 2、下載安裝包

GitHub Release頁面下載最新版本的apollo-configservice-x.x.x-github.zipapollo-adminservice-x.x.x-github.zipapollo-portal-x.x.x-github.zip即可。

 

 3、配置數據庫連接信息

Apollo服務端需要知道如何連接到你前面創建的數據庫,數據庫連接串信息位於上一步下載的壓縮包中的config/application-github.properties中。

1)、配置apollo-configservice的數據庫連接信息

# DataSource
spring.datasource.url = jdbc:mysql://192.168.21.138:3306/ApolloConfigDB?characterEncoding=utf8
spring.datasource.username = root
spring.datasource.password = root

2)、配置apollo-adminservice的數據庫連接信息

# DataSource
spring.datasource.url = jdbc:mysql://192.168.21.138:3306/ApolloConfigDB?characterEncoding=utf8
spring.datasource.username = root
spring.datasource.password = root

3)、 配置apollo-portal的數據庫連接信息

# DataSource
spring.datasource.url = jdbc:mysql://192.168.21.138:3306/ApolloPortalDB?characterEncoding=utf8
spring.datasource.username = root
spring.datasource.password = root

4)、修改三個服務端口,端口設置如下

apollo-configservice 18080 注冊中心地址
apollo-adminservice 18090 
apollo-portal 18070 門戶登陸端口

打開apollo-configservice-1.8.1-github/scripts/startup.sh文件,修改如下:

SERVER_PORT=${SERVER_PORT:=18080}

打開apollo-adminservice-1.8.1-github/scripts/startup.sh文件,修改如下:

SERVER_PORT=${SERVER_PORT:=18090}

打開apollo-portal-1.8.1-github/scripts/startup.sh文件,修改如下:

SERVER_PORT=${SERVER_PORT:=18070}

注意:apollo-configservice不以8080端口啟動的情況下,要修改ApolloConfigDB庫的ServerConfig表的eureka.service.url的value值為http://localhost:18080/eureka/,我是單機環境,因此是localhost,端口從數據庫默認的8080改為18080

5)、配置apollo-portal的meta service信息

打開apollo-portal-1.8.1-github.zipconfig目錄下的apollo-env.properties文件。我自己只是使用了dev環境,其他沒使用。

local.meta=http://localhost:18080
dev.meta=http://localhost:18080
#fat.meta=http://fill-in-fat-meta-server:8080
#uat.meta=http://fill-in-uat-meta-server:8080
lpt.meta=${lpt_meta}
#pro.meta=http://fill-in-pro-meta-server:8080

4、部署Apollo服務端

先確保mysql容器啟動,否則下面的服務啟動失敗。mysql容器服務在另外一台虛擬機上面,即CentOS7-protainer+mysql,ip為192.168.21.138.

注意:MySQL服務一定要與apollo服務分別放到兩個服務器,因為我放到一個服務器時,apollo服務總是無法啟動

啟動順序configservice->adminservice->portal,每個命令輸入后等2min再啟動下一個服務,因為有依賴關系。

啟動方式一:如果不是雲服務器可以按如下啟動:

1)、部署apollo-configservice

將對應環境的apollo-configservice-1.8.1-github.zip上傳到服務器上,解壓后執行scripts/startup.sh即可。如需停止服務,執行scripts/shutdown.sh.

cd apollo-configservice-1.8.1-github/scripts
./startup.sh

如果報如下錯:

chmod u+x *.sh

這里的u 這里指文件所有者,+x 添加可執行權限,*.sh表示所有的sh文件。

如果報錯如下:

 查看日志發現mysql服務未啟動。

啟動需要幾分鍾,看到下圖的執行結果說明啟動成功!

 停止服務

./shutdown.sh

2)、部署apollo-adminservice

將對應環境的apollo-adminservice-1.8.1-github.zip上傳到服務器上,解壓后執行scripts/startup.sh即可。如需停止服務,執行scripts/shutdown.sh.

cd apollo-adminservice-1.8.1-github/scripts
./startup.sh

3)、部署apollo-portal

apollo-portal-1.8.1-github.zip上傳到服務器上,解壓后執行scripts/startup.sh即可。如需停止服務,執行scripts/shutdown.sh.

cd apollo-portal-1.8.1-github/scripts
./startup.sh

 

啟動方式二:如果是雲服務器,按如下方法啟動

注意:1、先要檢查數據庫所在的服務器能否ping通,如果ping不同,項目即使啟動了也無法正常運行。2、安全組和服務器都要放開端口。3、盡量用熱點,有些公司的內網禁用了22端口。

cd /root/apollo-configservice-1.8.1-github/
nohup java -Xmx256m -Xms256m -Xmn128m -Dserver.port=18080 -Deureka.instance.ip-address=雲服務器公網IP  -jar apollo-configservice-1.8.1.jar &

cd /root/apollo-adminservice-1.8.1-github/
nohup java -Xmx256m -Xms256m -Xmn128m -Dserver.port=18090 -Deureka.instance.ip-address=雲服務器公網IP -jar apollo-adminservice-1.8.1.jar &
IP
cd /root/apollo-portal-1.8.1-github/
nohup java -Xmx512m -Xms512m -Xmn256m -Dserver.port=18070 -jar apollo-portal-1.8.1.jar &

當出現如下內容:nohup: ignoring input and appending output to ‘nohup.out’,點擊回車即可。

執行結果:

[root@VM-0-10-centos apollo-configservice-1.8.1-github]# nohup java -Xmx256m -Xms256m -Xmn128m -Dserver.port=18080 -Deureka.instance.ip-address=雲服務器公網IP  -jar apollo-configservice-1.8.1.jar &
[1] 27729
[root@VM-0-10-centos apollo-configservice-1.8.1-github]# nohup: ignoring input and appending output to ‘nohup.out’

[root@VM-0-10-centos apollo-configservice-1.8.1-github]#
[root@VM-0-10-centos apollo-configservice-1.8.1-github]# cd /root/apollo-adminservice-1.8.1-github/
[root@VM-0-10-centos apollo-adminservice-1.8.1-github]# nohup java -Xmx256m -Xms256m -Xmn128m -Dserver.port=18090 -Deureka.instance.ip-address=雲服務器公網IP -jar apollo-adminservice-1.8.1.jar &
[2] 28235
[root@VM-0-10-centos apollo-adminservice-1.8.1-github]# nohup: ignoring input and appending output to ‘nohup.out’

[root@VM-0-10-centos apollo-adminservice-1.8.1-github]# cd /root/apollo-portal-1.8.1-github/
[root@VM-0-10-centos apollo-portal-1.8.1-github]# nohup java -Xmx512m -Xms512m -Xmn256m -Dserver.port=18070 -jar apollo-portal-1.8.1.jar &
[3] 28523
[root@VM-0-10-centos apollo-portal-1.8.1-github]# nohup: ignoring input and appending output to ‘nohup.out’

[root@VM-0-10-centos apollo-portal-1.8.1-github]#

注意:當在雲服務器中部署apollo時,需要指定公網Ip,否則應用程序無法獲取配置文件。這就是虛擬機中可以通過startup.sh進行啟動,而雲服務器需要通過java -jar的方式啟動的原因,java -jar 方式可以指定jvm參數。

-Deureka.instance.ip-address=雲服務器公網IP

其中nohup是no hangup的縮寫。

(1)、nohup:不掛斷地運行命令

語法:nohup Command [ Arg … ] [ & ]

無論是否將 nohup 命令的輸出重定向到終端,輸出都將附加到當前目錄的 nohup.out 文件中。

  如果當前目錄的 nohup.out 文件不可寫,輸出重定向到 $HOME/nohup.out 文件中。

  如果沒有文件能創建或打開以用於追加,那么 Command 參數指定的命令不可調用。

退出狀態:該命令返回下列出口值:   
  126 可以查找但不能調用 Command 參數指定的命令。   
  127 nohup 命令發生錯誤或不能查找由 Command 參數指定的命令。   
  否則,nohup 命令的退出狀態是 Command 參數指定命令的退出狀態。
(2)、&:在后台運行

一般兩個一起用

nohup command &

configService啟動成功后,輸入命令查看日志

tail -f nohup.out

結果如下:

2022-06-15 14:06:55.645  INFO 18112 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Application is null : false
2022-06-15 14:06:55.645  INFO 18112 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Registered Applications size is zero : true
2022-06-15 14:06:55.645  INFO 18112 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Application version is -1: true
2022-06-15 14:06:55.645  INFO 18112 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Getting all instance registry info from the eureka server
2022-06-15 14:06:55.674  INFO 18112 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : The response status is 200
2022-06-15 14:06:55.898  INFO 18112 --- [      Thread-13] c.n.e.registry.AbstractInstanceRegistry  : Registered instance APOLLO-CONFIGSERVICE/172.17.0.10:apollo-configservice:18080 with status UP (replication=true)
2022-06-15 14:06:55.898  INFO 18112 --- [      Thread-13] c.n.e.r.PeerAwareInstanceRegistryImpl    : Got 1 instances from neighboring DS node
2022-06-15 14:06:55.898  INFO 18112 --- [      Thread-13] c.n.e.r.PeerAwareInstanceRegistryImpl    : Renew threshold is: 1
2022-06-15 14:06:55.898  INFO 18112 --- [      Thread-13] c.n.e.r.PeerAwareInstanceRegistryImpl    : Changing status to UP
2022-06-15 14:06:55.905  INFO 18112 --- [      Thread-13] e.s.EurekaServerInitializerConfiguration : Started Eureka Server
2022-06-15 14:07:25.548  INFO 18112 --- [trap-executor-0] c.n.d.s.r.aws.ConfigClusterResolver      : Resolving eureka endpoints via configuration

adminservice啟動后,查看日志

tail -f nohup.out

結果如下:

2022-06-15 14:09:46.630  INFO 18656 --- [           main] c.n.discovery.InstanceInfoReplicator     : InstanceInfoReplicator onDemand update allowed rate per min is 4
2022-06-15 14:09:46.635  INFO 18656 --- [           main] com.netflix.discovery.DiscoveryClient    : Discovery Client initialized at timestamp 1655273386634 with initial instances count: 1
2022-06-15 14:09:46.644  INFO 18656 --- [           main] o.s.c.n.e.s.EurekaServiceRegistry        : Registering application APOLLO-ADMINSERVICE with eureka with status UP
2022-06-15 14:09:46.644  INFO 18656 --- [           main] com.netflix.discovery.DiscoveryClient    : Saw local status change event StatusChangeEvent [timestamp=1655273386644, current=UP, previous=STARTING]
2022-06-15 14:09:46.645  WARN 18656 --- [           main] c.n.discovery.InstanceInfoReplicator     : Ignoring onDemand update due to rate limiter
2022-06-15 14:09:46.681  INFO 18656 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 18090 (http) with context path ''
2022-06-15 14:09:46.682  INFO 18656 --- [           main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 18090
2022-06-15 14:09:46.709  INFO 18656 --- [           main] c.c.f.a.a.AdminServiceApplication        : Started AdminServiceApplication in 11.695 seconds (JVM running for 12.81)
2022-06-15 14:09:46.807  INFO 18656 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_APOLLO-ADMINSERVICE/172.17.0.10:apollo-adminservice:18090: registering service...
2022-06-15 14:09:46.867  INFO 18656 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_APOLLO-ADMINSERVICE/172.17.0.10:apollo-adminservice:18090 - registration status: 204
2022-06-15 14:10:46.384  INFO 18656 --- [trap-executor-0] c.n.d.s.r.aws.ConfigClusterResolver      : Resolving eureka endpoints via configuration

portal啟動后,查看日志:

tail -f nohup.out

結果:

2022-06-15 14:11:53.927  INFO 19009 --- [           main] f.a.p.e.DatabasePortalMetaServerProvider : Loaded meta server addresses from portal config: {}
2022-06-15 14:11:53.929  INFO 19009 --- [           main] .f.a.p.e.DefaultPortalMetaServerProvider : Loaded meta server addresses from system property, os environment and properties file: {DEV=http://localhost:18080, LOCAL=http://localhost:18080, LPT=${lpt_meta}}
2022-06-15 14:11:54.588  WARN 19009 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2022-06-15 14:11:54.663  INFO 19009 --- [erviceLocator-1] c.c.f.a.p.e.PortalMetaDomainService      : Located meta server address [http://localhost:18080] for env [DEV]
2022-06-15 14:11:55.025  INFO 19009 --- [           main] o.s.s.web.DefaultSecurityFilterChain     : Will secure any request with [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@344426bf, org.springframework.security.web.context.SecurityContextPersistenceFilter@4441d567, org.springframework.security.web.header.HeaderWriterFilter@616b241a, org.springframework.security.web.authentication.logout.LogoutFilter@79d743e6, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@17b64941, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@26a4551a, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@62b969c4, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@47ec7422, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@5411dd90, org.springframework.security.web.session.SessionManagementFilter@1f387978, org.springframework.security.web.access.ExceptionTranslationFilter@2116b68b, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@4992613f]
2022-06-15 14:11:55.114  INFO 19009 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2022-06-15 14:11:55.246  INFO 19009 --- [           main] o.s.b.a.w.s.WelcomePageHandlerMapping    : Adding welcome page: class path resource [static/index.html]
2022-06-15 14:11:55.698  INFO 19009 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 4 endpoint(s) beneath base path ''
2022-06-15 14:11:55.789  INFO 19009 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 18070 (http) with context path ''
2022-06-15 14:11:55.814  INFO 19009 --- [           main] c.c.f.apollo.portal.PortalApplication    : Started PortalApplication in 9.382 seconds (JVM running for 10.374)

4)、啟動configservice服務后,瀏覽器輸入http://192.168.21.136:18080/ 就可以看到信息

如果啟動成功,那么點擊上圖中紅框內的內容,會顯示如下內容,則說明config或admin啟動成功。

 5)、登錄Apollo

訪問http://192.168.21.136:18070/,這里的ip是虛擬機的ip。看到apollo的登錄頁面,說明服務均部署成功。

 

 用戶默認密碼是apollo/admin,用戶名和密碼存儲在apolloportaldb數據庫的Users表中,可修改。

登錄成功后進入如下界面:

Apollo在docker中部署和在kubernetes中部署這里省略,可以參考https://www.cnblogs.com/zys2019/p/14798944.html

 四、使用Apollo

點擊創建項目,進入如下頁面

 輸入如下內容后點擊提交

 部門:選擇應用所在的部門。

應用AppId:用來標識應用身份的唯一id,格式為string

應用名稱:應用名,僅用於界面展示。

應用負責人:選擇的人默認會成為該項目的管理員,具備項目權限管理、集群創建、Namespace創建等權限。

 進入如下頁面:

 點擊新增配置按鈕,配置需要管理的 application.properties 中的屬性

 在彈框中輸入key和value,這里以spring.freemarker.cache=false為例,點擊提交

 提交后列表會展示出來:

 發布配置

選擇發布按鈕,發布配置,在發布的彈框中選擇發布按鈕即可

 發布后界面就會顯示已發布

添加Namespace

點擊“添加Namespace”,進入如下界面

 

點擊“創建Namespace”,進入如下界面

 

輸入名稱,點擊提交,如下所示:

 

關聯公共Namespace

 五、項目使用Apollo

項目地址:F:\prism\svn\springboot

1、添加Apollo依賴

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

2、在配置文件application.properties配置apollo的相關信息

#阿波羅配置
app.id = apollo-application
apollo.meta=http://192.168.21.136:18080
apollo.bootstrap.enabled = true
apollo.bootstrap.eagerLoad.enabled=true

app.id:AppId是應用的身份信息,是配置中心獲取配置的一個重要信息。這個必須和配置中心保持一致

apollo.meta:指定config服務地址

apollo.bootstrap.enabled:在應用啟動階段,向Spring容器注入被托管的application.properties文件的配置信息。

apollo.bootstrap.eagerLoad.enabled:將Apollo配置加載提到初始化日志系統之前。

3、在啟動類上添加注解@EnableApolloConfig

@SpringBootApplication
@EnableApolloConfig public class MySpringBootApplication extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(MySpringBootApplication.class);

    }
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(this.getClass());
    }
}

4、編寫一個Controller

@Controller
public class QuickController {
    @Value("${spring.freemarker.cache}")
    private Boolean cache;
    @RequestMapping("/quick")
    @ResponseBody
    public String quick(){
        return "hi,springbootzwh!"+cache;
    }
}

5、啟動項目,訪問http://localhost:8080/quick,結果如下:

注意:項目啟動后,在C:\opt\data\apollo-application-test\config-cache目錄下生成一個文件,則說明apollo獲取到配置文件了。

否則獲取配置文件失敗。

 6、在配置中心修改spring.freemarker.cache的值,將其改為true並發布

 六、使用本地電腦的mysql

1、修改數據庫連接信息

# DataSource
spring.datasource.url = jdbc:mysql://192.168.21.1:3306/ApolloPortalDB?characterEncoding=utf8&serverTimezone=CTT
spring.datasource.username = root
spring.datasource.password = 123456

注意:

1)、本地電腦的ip的網段要與虛擬機的網段保持一致。

2)、由於數據庫驅動是com.mysql.cj.jdbc.Driver,故要添加時區。

3)、先關閉防火牆才能ping 192.168.21.1通。telnet查某個ip的某個端口是否打開:telnet 192.168.21.1 3306,安裝Telnet的命令為:yum install -y telnet

2、如果報錯:java.sql.SQLException: Access denied for user 'root'@'192.168.21.139' (using password: YES),則要創建遠程登陸用戶並授權

1)、在本地電腦打開cmd,輸入命令mysql -u root -p,輸入密碼123456,輸入命令:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.21.139' IDENTIFIED BY '123456' WITH GRANT OPTION;

第一個*表示本地數據庫的數據庫名  *.*號表示而本地的所有數據庫都授權   test.*表示授權test數據庫里面的所有表。root是用戶名,123456表示root對應的密碼。192.168.21.136表示host主機IP,這里的%表示正常的IP地址都可以

2)、執行 flush privileges;

3)、執行exit

 此時,Navicat中會多出一個用戶

 3、本地防火牆已經開啟了3306端口

控制面板----系統和安全----windows Defender 防火牆----高級設置----點擊左邊的“入站規則”----在列表中找到“port 3306”

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM