本文的示例代码参考apollo-config-client / cloud-config-server / cloud-config-client / alibaba-config-client
Contents
Ctrip Apollo Config
Ctrip Apollo Config Server
wget http://file.nuozhilin.site/5_macos/apollo-quick-start-1.7.1.zip
unzip apollo-quick-start-1.7.1.zip -d apollo && cd apollo
docker run --name mysql-apollo -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.17
docker exec -i mysql-apollo mysql -uroot -p123456 < ./sql/apolloportaldb.sql
docker exec -i mysql-apollo mysql -uroot -p123456 <<< "select Id, AppId, Name from ApolloPortalDB.App;"
# Id AppId Name
# 1 SampleApp Sample App
docker exec -i mysql-apollo mysql -uroot -p123456 < ./sql/apolloconfigdb.sql
docker exec -i mysql-apollo mysql -uroot -p123456 <<< "select NamespaceId, Value, Comment from ApolloConfigDB.Item;"
# NamespaceId Value Comment
# 1 100 sample timeout??
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
# 42824 apollo-portal.jar
# 42635 apollo-service.jar
- 浏览器打开http://localhost:8070 => 账号apollo 密码admin
Ctrip Apollo Config Client
spring init -b=2.2.10.RELEASE -j=1.8 -l=kotlin -d=web --build=gradle apollo-config-client && cd apollo-config-client
vim build.gradle.kts
# implementation("com.ctrip.framework.apollo:apollo-client:1.4.0")
vim src/main/resources/application.properties
server.port=8001
app.id=SampleApp
apollo.meta=http://127.0.0.1:8080
apollo.cacheDir=./config
vim src/main/kotlin/com/example/apolloconfigclient/DemoController.kt
package com.example.apolloconfigclient
import com.ctrip.framework.apollo.ConfigService
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
@RestController
class DemoController {
@GetMapping("/")
fun demo(): String {
val config = ConfigService.getAppConfig()
return config.getProperty("timeout", "-1")
}
}
./gradlew bootrun
curl localhost:8001
# 100
Spring Cloud Config
Git Repository
-
目录结构和文件内容
.
└── config
├── cloud-config-client-dev.yml
├── cloud-config-client-prod.yml
├── cloud-config-server-dev.yml
└── cloud-config-server-prod.yml
1 directory, 4 files
cat config/cloud-config-server-dev.yml
# data:
# env: server dev
cat config/cloud-config-server-prod.yml
# data:
# env: server prod
Spring Cloud Config Server
spring init -b=2.2.10.RELEASE -j=1.8 -l=kotlin -d=web,cloud-config-server --build=gradle cloud-config-server && cd cloud-config-server
vim src/main/kotlin/com/example/cloudconfigserver/DemoApplication.kt
package com.example.cloudconfigserver
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.cloud.config.server.EnableConfigServer
@EnableConfigServer
@SpringBootApplication
class DemoApplication
fun main(args: Array<String>) {
runApplication<DemoApplication>(*args)
}
vim src/main/resources/application.properties
server.port=8001
vim src/main/resources/bootstrap.yml
spring:
application:
name: cloud-config-server
cloud:
config:
server:
git:
uri: https://github.com/yl33643/spring-cloud-config-file.git
search-paths: config
default-label: master
./gradlew bootrun
curl http://localhost:8001/cloud-config-server-dev.yml
# data:
# env: server dev
curl http://localhost:8001/cloud-config-server-prod.yml
# data:
# env: server prod
curl http://localhost:8001/cloud-config-client-dev.yml
# data:
# env: client dev
curl http://localhost:8001/cloud-config-client-prod.yml
# data:
# env: client prod
Spring Cloud Config Client
spring init -b=2.2.10.RELEASE -j=1.8 -l=kotlin -d=web,cloud-config-client --build=gradle cloud-config-client && cd cloud-config-client
vim src/main/resources/application.properties
server.port=8101
vim src/main/resources/bootstrap.yml
spring:
application:
name: cloud-config-client
cloud:
config:
uri: http://localhost:8001
label: master
profile: dev
vim src/main/kotlin/com/example/cloudconfigclient/DemoController.kt
package com.example.cloudconfigclient
import org.springframework.beans.factory.annotation.Value
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
@RestController
class DemoController {
@Value("\${data.env}")
private lateinit var env: String
@GetMapping("/")
fun demo(): String {
return env
}
}
./gradlew bootrun
curl localhost:8101
# client dev
vim src/main/resources/bootstrap.yml
spring:
application:
name: cloud-config-client
cloud:
config:
uri: http://localhost:8001
label: master
profile: prod
./gradlew bootrun
curl localhost:8101
# client prod
Auto Refresh Config
# cloud-config-client
vim build.gradle.kts
# implementation("org.springframework.boot:spring-boot-starter-actuator")
vim src/main/resources/application.properties
server.port=8101
management.endpoints.web.exposure.include=*
vim src/main/kotlin/com/example/cloudconfigclient/DemoController.kt
package com.example.cloudconfigclient
import org.springframework.beans.factory.annotation.Value
import org.springframework.cloud.context.config.annotation.RefreshScope
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
@RestController
@RefreshScope
class DemoController {
@Value("\${data.env}")
private lateinit var env: String
@GetMapping("/")
fun demo(): String {
return env
}
}
./gradlew bootrun
curl localhost:8101
# client prod
- 更新"config/cloud-config-client-prod.yml"内容"env: client prod refresh"
curl -X POST localhost:8101/actuator/refresh
# ["config.client.version","data.env"]
curl localhost:8101
# client prod refresh
- 配置Git Repository的Webhooks自动触发上述更新配置API
Alibaba Nacos Config
Alibaba Nacos Config Server
wget http://file.nuozhilin.site/5_macos/nacos-server-1.4.0-BETA.tar.gz
tar xf nacos-server-1.4.0-BETA.tar.gz && cd nacos
sh bin/startup.sh -m standalone
-
新建配置
Data ID => alibaba-config-client
配置内容 => data.env=client dev
Alibaba Nacos Config Client
spring init -b=2.2.10.RELEASE -j=1.8 -l=kotlin -d=web,alibaba-nacos-config --build=gradle alibaba-config-client && cd alibaba-config-client
vim src/main/resources/application.properties
server.port=8201
vim src/main/resources/bootstrap.yml
spring:
application:
name: alibaba-config-client
cloud:
nacos:
config:
server-addr: http://127.0.0.1:8848
vim src/main/kotlin/com/example/alibabaconfigclient/DemoController.kt
package com.example.alibabaconfigclient
import org.springframework.beans.factory.annotation.Value
import org.springframework.cloud.context.config.annotation.RefreshScope
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
@RestController
@RefreshScope
class DemoController {
@Value("\${data.env}")
private lateinit var env: String
@GetMapping("/")
fun demo(): String {
return env
}
}
./gradlew bootrun
curl localhost:8201
# client dev
- 更新配置
配置内容 => data.env=client dev refresh
curl localhost:8201
# client dev refresh
网友评论