springcloud實戰之14動態重新整理分散式配置中心屬性配置(config)
阿新 • • 發佈:2019-02-16
springcloud實戰之10 分散式配置中心(config)介紹瞭如何用spring cloud config和git倉庫讀取配置檔案,當配置檔案裡的屬性發生變化,按照以往的慣例,必須重啟服務才能夠讀取到新的配置屬性。但springcloud config為我們提供了springcloud bus(訊息匯流排)能夠實現動態重新整理配置檔案。
重構(springcloud-server-config)工程
新增依賴
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId >spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath />
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId >spring-cloud-dependencies</artifactId>
<version>Edgware.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency >
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
改造入口類
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class SpringCloudServerConfigApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudServerConfigApplication.class,args) ;
}
}
修改配置檔案
server:
port: 8001
management:
security:
enabled: false
spring:
application:
name: springcloud-server-config
cloud:
config:
server:
git:
uri: https://github.com/shiyuan2he/springcloud
search-paths: springcloud-repo-config
username:
password:
force-pull: true
label: master
rabbitmq:
host: 192.168.175.128
port: 5672
username: admin
password: admin
eureka:
client:
service-url:
defaultZone: http://peer1:8080/eureka/
重構springcloud-service-config專案
新增依賴
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath />
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
重構入口類
@SpringBootApplication
@EnableDiscoveryClient
public class SpringCloudServiceConfigApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudServiceConfigApplication.class,args) ;
}
}
改造web層介面
@RestController
@RefreshScope
@RequestMapping(value = "/api/rest")
public class RestfulController {
@Value("${user.name}")
String name;
@Value("${user.age}")
String age ;
@GetMapping("/name")
public String getName(){
return name +":"+ age;
}
}
新建配置檔案bootstrap.properties
spring.application.name=springcloud-service-config
spring.cloud.config.label=master
spring.cloud.config.profile=dev
spring.cloud.config.uri= http://localhost:8001/
server.port=8003
eureka.client.serviceUrl.defaultZone=http://peer1:8080/eureka/
## 從配置中心讀取檔案
spring.cloud.config.discovery.enabled=true
## 配置中心的servieId,即服務名。
spring.cloud.config.discovery.serviceId=springcloud-server-config
spring.cloud.config.fail-fast=true
spring.rabbitmq.host=192.168.175.128
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin
management.security.enabled=false
啟動專案
1.啟動服務註冊中心叢集(springcloud-server-eureka)
2.啟動服務配置中心(springcloud-server-config)
3.啟動springcloud-service-config叢集
啟動後的服務註冊圖如下:
分別呼叫兩個讀取配置檔案得叢集:
去github倉庫修改配置檔案,並訪問之前的叢集服務,還沒有立馬生效
再次檢視叢集服務,如下圖