SpringCloud????????
?????????? ???????[ 2017/4/20 14:37:43 ] ?????????????????? ???ù???
???????
??????????????У??????????????????????????????????????????????????????????????????????ò????з?????????????????provider?????????????????????????鷳?????????ò????????????????????????????е?provider????????????????????????2???provider?????á??????????????????????й?????????????????SpringCloudConfig??SpringCloudBus???????????????????????????Щ?????????????????????disconf??
???????????????SpringCloudConfig??????????Spring?????????Spring????Environment??PropertySource??????????????е?Spring??ó????????????????????????????????????£????SpringBoot??????????и????????????????????汾??????淶?????????????????????????????????汾?????
?????. ???
????SpringCloudConfig????????????????????????????????????????????????ó????????????????????????????????????????????????SpringCloudConfig????????????????????git??svn???д洢???????????????REST?????????????????REST????????á???????????????????????????仯??????????????μ??????????????????????POST?????????????/refresh??
????SpringCloudBus???????????????????????????????????????????????????????????????????????SpringCloudBus???????POST?????????endpoint/bus/refresh?????????????git??????????????????????SpringCloudConfig??????????????????á?
?????????SpringCloudConfig???SpringCloudBus????????????????
????????????????????????????????SpringCloudBus??????????????????????????г??????????????????????????????
??????. SpringCloudConfig Server
????SpringCloudConfig??????????3?????????ù????
????· ???
????????????????????????????????????
????· ????
??????????????????????????dev?? test?? prod??
????· ?汾
????????????????????????????????????????????????汾????
????Spring Cloud Config???汾?????????????????????????????????????????????????汾?????????????Щ???????磺????????A/B????????????????
????2.1 ConfigServer ????
????????????pom??????spring-cloud-config-server??spring-cloud-starter-bus-kafka
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-monitor</artifactId>
</dependency>
????application.properties??????ò??????????????е?????????????????????????????????
spring.cloud.config.server.git.uri=https://github.com/seagrape/SpringCloudConfig.git
spring.cloud.config.server.git.searchPaths=alan-config-repo
#spring.cloud.config.server.git.username=sihan2
#spring.cloud.config.server.git.password=MYPASSWORD
spring.cloud.stream.kafka.binder.brokers=10.79.96.52:9092
spring.cloud.stream.kafka.binder.zk-nodes=10.79.96.52:2182
??????????????@EnableConfigServer???
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class?? args);
}
}
????2.2 ConfigServer ???
????Server??????????????μ??????????????
????· application???????
????· profile??????
????· label???汾
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
????NOTE?????????????????????????????????????????????£?alan-provider-data-config-dev.properties??????alan-provider-data-config??????????????????????????????????????????????????????????????????spring.cloud.config.name=alan-provider-data-config?????????????????????????????????????????dev???????????????????????????????????????????????????????????????????
?????????????? curl http://localhost:8888/alan-provider-data-config/dev/master
{
"name": "alan-provider-data-config"??
"profiles": ["dev"]??
"label": "master"??
"version": "78dce2b71473749a5298e11ef0d004ffa8d26bd1"??
"propertySources": [{
"name": "https://github.com/seagrape/SpringCloudConfig.git/alan-config-repo/alan-provider-data-config-dev.properties"??
"source": {
"spring.datasource.driver-class-name": "com.mysql.jdbc.Driver"??
"spring.datasource.username": "username"??
"spring.datasource.password": "password"??
"spring.datasource.url": "jdbc:mysql://DEVIP:PORT/DBNAME?characterEncoding=UTF-8"
}
}]
}
?????????????? curl http://localhost:8888/alan-provider-data-config-dev.properties
????spring.datasource.driver-class-name: com.mysql.jdbc.Driver
????spring.datasource.password: password
????spring.datasource.url: jdbc:mysql://DEVIP:PORT/DBNAME?characterEncoding=UTF-8
????spring.datasource.username: username
????2.3 ConfigServer ?????
????GIT????????????????clone????????????У??????Щ????????????config-repo-???????????????? linux ??????? /tmp/config-repo-??????????????????????????????????????????spring.cloud.config.server.git.basedir?????????????????
????Config Server?У????????????classpath ????????м???????????????????????spring.cloud.config.server.native.searchLocations???????á??????????GIT????????У????????????......
??????. SpringCloudConfig Client
????3.1 ConfigClient ????
????????????pom??????spring-cloud-starter-config??spring-cloud-starter-bus-kafka
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
????bootstrap.properties????????????????????????е??
????1.?????? ???????????????????bootstrap.properties?У???????????????“??”????????????????????????????????????????????????????????????????????????????????????????? 2.??application.properties???????????????????????????????????????????????????????????????????Щ????????????????????????????????????????????????? 3.??????δ?????????????У??????????????????????spring.application.name???????????????????????default??????${spring.application.name}.properties 4.??????????????????????????????????д???--spring.cloud.config.profile=dev|prod|test??????????????????????????????????????????????? 5.bus??????????????????kafka????????????????
spring.cloud.config.uri=http://127.0.0.1:${config.port:8888}
spring.cloud.config.name=alan-provider-data-config
spring.cloud.config.profile=${config.profile:dev}
spring.cloud.stream.kafka.binder.brokers=10.79.96.52:9092
spring.cloud.stream.kafka.binder.zk-nodes=10.79.96.52:2182
??????????????????@RefreshScope???
@SpringBootApplication
@RestController
@RefreshScope
public class ConfigClientApplication {
@Value("${spring.datasource.username}")
String name = "World";
@RequestMapping("/")
public String home() {
System.out.println(name);
return name;
}
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class?? args);
}
}
????3.2 RefreshScope ???
???????????Spring????????Щscope????singleton??prototype??request??? ????????????o???????bean????????μ?????Spring Cloud????????μ?scope - RefreshScope??
????Spring Cloud??RefreshScope????????£?
????A Scope implementation that allows for beans to be refreshed dynamically at runtime (see refresh(String) and refreshAll()). If a bean is refreshed then the next time the bean is accessed (i.e. a method is executed) a new instance is created.
???????????????Щ????????bean?????????????????RefreshScope?????????????????????????μ???????????RefreshScope.refresh(beanName)??RefreshScope.refreshAll()??????′???Щbean???????????3?????????????????????????????????μ?????
????3.3 ConfigClient ??????
????ConfigClient?????ConfigServer????????Spring??????????????????????????????????????????????????????????????ConfigServer???????????ConfigClient????????????????????棬????????????????????п?????????????????????????????????±?????????????????á?????????????????????ú???????????????????????????????/??????????
????????????????????????????
??????. ??????
????4.1 Spring?е?Environment??PropertySource
????· Environment
????Spring??ApplicationContext????????Environment
????Environment????????????PropertySource
????· PropertySource
?????????
????????????????Key - Value??????????
?????????????????磺
??????????????PropertySource?????????????????????????Key????property source?ж???????????????property source????????????????????
????· env.getProperty(“key1”) -> value1
????· env.getProperty(“key2”) -> value2
????· env.getProperty(“key3”) -> value4
??????ConfigClient?????Σ???ConfigServer????????????????PropertySource????????????????????????ù????У?????Config Server?????ú?????????????????????????????κβ????????????????ɡ?
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11