準備工作:一個註冊中心項目、一個配置中心服務端及一個配置中心客戶端項目。需要將配置中心服務端及客戶端註冊到註冊中心去。使用碼雲作爲倉庫管理配置信息。
【1】配置中心服務端依賴:pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<!-- 管理依賴 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--spring-cloud 整合 config-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- SpringBoot整合eureka客戶端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<!-- 注意: 這裏必須要添加, 否者各種依賴有問題 -->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
application.yml:
####端口號
server:
port: 8010
###服務註冊到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:8000/eureka
spring:
application:
####註冊中心應用名稱,也是配置中心的名稱,在配置中心客戶端需要用到該name
name: config-server
cloud:
config:
server:
git:
###git環境地址
uri: https://gitee.com/zhoulunwu/config.git
#如果是私有倉庫,需要配置賬號及密碼
##username: 用戶名
#password: 密碼
###下面的skip-ssl-validation爲了解決: Cannot clone or checkout repository
skip-ssl-validation: true
####搜索目錄
search-paths:
- config
####讀取分支
label: master
啓動類: 啓動之後,可以通過訪問:http://localhost:8010/config-client-dev.properties是否能正常訪問。
@SpringBootApplication
// 開啓配置中心服務
@EnableConfigServer
public class SpringCloudConfigServerApplication {
public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(SpringCloudConfigServerApplication.class, args);
}
}
【2】配置中心客戶端依賴:pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<!-- 管理依賴 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- SpringBoot整合Web組件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<!-- SpringBoot整合eureka客戶端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- actuator監控中心 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<!-- 注意: 這裏必須要添加, 否者各種依賴有問題 -->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
bootstrap.yml:
server:
port: 8050
##### eureka服務註冊地址
eureka:
client:
service-url:
defaultZone: http://localhost:8000/eureka
spring:
application:
####註冊中心應用名稱
name: config-client
cloud:
config:
####讀取後綴
profile: dev
####讀取config-server註冊地址
discovery:
###這裏的service-id就是配置中心服務端的name
service-id: config-server
enabled: true
##開啓監控斷點
management:
endpoints:
web:
exposure:
include: "*"
啓動類:
@SpringBootApplication
@EnableEurekaClient
public class SpringCloudConfigClientApplication {
public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(SpringCloudConfigClientApplication.class, args);
}
}
測試類:獲取gitee中新建的config-client-dev.properties的myTestName屬性。
@RestController
@RefreshScope
public class ConfigClient {
@Value("${myTestName}")
private String myTestName;
@RequestMapping("/getMyTestName")
public String getMyTestName() {
return myTestName;
}
}
如果能正常訪問到表示配置成功
【3】手動刷新配置信息。
此時去更改gitee上面的信息,訪問配置中心服務端http://localhost:8010/config-client-dev.properties可以看到內容已經更改了。但是通配置中心客戶端的路徑去獲取,依舊是前一次的內容,並沒有更改。
需要手動刷新同步:使用postman軟件去同步,使用post請求;
然後再訪問配置中心客戶端的路徑,就可以看到改變了。