本文的示例代码参考ApolloDemo
Download
java -version
# openjdk version "1.8.0_212"
unzip apollo-quick-start-1.5.0.zip -d apollo && cd apollo
MySQL
docker run --name apollo-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.17
docker exec -i apollo-mysql mysql -uroot -p123456 < ./sql/apolloportaldb.sql
docker exec -i apollo-mysql mysql -uroot -p123456 <<< "select Id, AppId, Name from ApolloPortalDB.App;"
# Id AppId Name
# 1 SampleApp Sample App
docker exec -i apollo-mysql mysql -uroot -p123456 < ./sql/apolloconfigdb.sql
docker exec -i apollo-mysql mysql -uroot -p123456 <<< "select NamespaceId, Value, Comment from ApolloConfigDB.Item;"
# NamespaceId Value Comment
# 1 100 sample timeout??
Apollo
vim demo.sh
# apollo_config_db_url=jdbc:mysql://localhost:3306/ApolloConfigDB?characterEncoding=utf8
# apollo_config_db_username=root
# apollo_config_db_password=123456
# apollo_portal_db_url=jdbc:mysql://localhost:3306/ApolloPortalDB?characterEncoding=utf8
# apollo_portal_db_username=root
# apollo_portal_db_password=123456
./demo.sh start
# Config service started. You may visit http://localhost:8080 for service status now!
# Portal started. You can visit http://localhost:8070 now!
jps
# 14769 apollo-service.jar
# 14876 apollo-portal.jar
- 浏览器打开http://localhost:8070 => 账号apollo 密码admin
SpringBoot
spring --version
# Spring CLI v2.0.6.RELEASE
spring init -b 2.0.6.RELEASE -dweb --build gradle ApolloDemo && cd ApolloDemo
vim build.gradle
dependencies {
compile group: 'com.ctrip.framework.apollo', name: 'apollo-client', version: '1.4.0'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
vim application.properties
# server.port=7777
# app.id=SampleApp
# apollo.meta=http://127.0.0.1:8080
# apollo.cacheDir=./config
vim src/main/java/com/example/ApolloDemo/DemoApplication.java
package com.example.ApolloDemo;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@GetMapping("/timeout")
public String timeout() {
Config config = ConfigService.getAppConfig();
return config.getProperty("timeout", "-1");
}
}
./gradlew bootrun
curl localhost:7777/timeout
# 100
Issues
- 本地开发模式
Apollo只会从本地文件读取配置信息 不会从Apollo服务器读取配
- 托管Logback配置
Apollo的加载顺序放到日志系统加载之前
# apollo.bootstrap.eagerLoad.enabled = true
网友评论