部署Apollo
apollo單機部署(快速安裝)
Apollo
官網:https://www.apolloconfig.com/#/zh/deployment/quick-start-docker
官網單機部署的方式分爲兩種:普通部署和docker部署。
普通部署
需要安裝MySQL
和Java
環境。
Apollo
服務端共需要兩個數據庫:ApolloPortalDB
(apollo
頁面)和ApolloConfigDB
(apollo
使用的),把數據庫、表的創建和樣例數據都分別準備了sql
文件,只需要導入數據庫即可。
然後建立數據庫連接,進入到快速包下面(apollo-quick-start
),下面有demo.sh
,進行編輯:
將對應的數據庫名和地址修改成自己的。
在啓動服務之前需要確認8070、8080、8090端口未被佔用,然後直接啓動./demo.sh
==== starting service ====
Service logging file is ./service/apollo-service.log
Started [10768]
Waiting for config service startup.......
Config service started. You may visit http://localhost:8080 for service status now!
Waiting for admin service startup....
Admin service started
==== starting portal ====
Portal logging file is ./portal/apollo-portal.log
Started [10846]
Waiting for portal startup......
Portal started. You can visit http://localhost:8070 now!
首先會啓動erueka
服務,然後啓動portal
配置中心,最後訪問[http://服務器外網地址:8070](http://服務器外網地址:8070)
即可;賬號:apollo,密碼:admin;具體如下圖所示:
docker部署
基於上述環境,我們暫停demo.sh
的執行:./demo.sh stop
,在快速包中(apollo-quick-start
)使用docker-compose up
運行;
SpringBoot集成Apollo
這一步就是個坑😂,創建SpringBoot
不多說,在maven
倉庫找到客戶端;
取最新版即可(截至2022年10月21日):
<!-- https://mvnrepository.com/artifact/com.ctrip.framework.apollo/apollo-client -->
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>2.0.1</version>
</dependency>
錯誤配置:
server:
port: 8000
app:
id: apolloTest
apollo:
bootstrap:
enabled: true
meta: http://服務器外網地址:8080
在啓動類上開啓apollo
:@EnableApolloConfig
@SpringBootApplication
@EnableApolloConfig
public class ApolloTestApplication {
public static void main(String[] args) {
SpringApplication.run(ApolloTestApplication.class, args);
}
}
在服務器部署的Apollo
會有問題,客戶端請求的時候地址會出現服務器的內網地址,那肯定是訪問不到的。
所以我們進行以下設置:
指定應用的AppId
app.id=apolloTest
在服務啓動設置上配置apollo
參數;
-Dapollo.configService=http://服務器外網地址:8080
需要對應起來,新增配置key:service,value:hello world,並且是已發佈
public class TestController {
@Value(value = "${service}")
private String name;
@RequestMapping("test")
public String test(){
return "hello world -->"+name;
}
}