配置中心
一、配置中心服務端
- 新建專案study-config-server
- 引入依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
- 加配置檔案
server:
port: 9100
spring:
application:
name: study-config-server
cloud:
config:
server:
git:
uri: https://github.com/mmcLine/cloudconfigTest.git
username: mmcLine
password: ********
- 加註解 @EnableConfigServer
@SpringBootApplication
@EnableConfigServer
public class StudyConfigApplication {
public static void main(String[] args) {
SpringApplication.run(StudyConfigApplication.class);
}
}
- 建立一個git專案,並在根目錄下新建一個application.yml檔案
spring:
profiles:
active: test
---
spring:
profiles: test
application:
name: config-test
server:
port:9101
---
spring:
profiles: prod
application:
name: config-prod
server:
port:9102
- 訪問規則
- 通過application-{profiles}.yml來訪問
例:http://localhost:9100/application-test.yml - 通過application/{profiles}/{label}
例:http://localhost:9100/application/test/master - 通過/label/application-{profiles}.yml
例:http://localhost:9100/master/application-test.yml
二、配置中心客戶端
- 引入依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
- 配置檔案,新增bootstrap.yml檔案
spring:
application:
name: study-trade
cloud:
config:
uri: http://localhost:9100
profile: test
label: master
注意:git上的配置yml檔名要對應這裡配置的spring.application.name
- git配置檔案演示
- 測試配置是否生效
@RestController
public class ConfigTestController {
@Value("${ordernotiry}")
private Boolean ordernotiry;
@RequestMapping("/testconfig")
public String testconfig(){
return String.valueOf(ordernotiry);
}
}
到此最簡單的配置中心搭建完成!!!
三、升級不重啟修改配置檔案
整合bus的方案
服務端修改:
- 加依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
- 啟動一個rabbitmq
- 配置檔案
spring:
cloud:
bus:
enabled: true
trace:
enabled: true
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
management:
endpoints:
web:
exposure:
include: "bus-refresh"
客戶端:
- 加依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
- 加註解
在讀取配置的類上加@RefreshScope註解
@RestController
@RefreshScope
public class ConfigTestController {
@Value("${ordernotiry}")
private Boolean ordernotiry;
@RequestMapping("/testconfig")
public String testconfig(){
return String.valueOf(ordernotiry);
}
}
- git上修改配置檔案後,post方式呼叫http://localhost:9100/actuator/bus-refresh即可
四、配置加密
- 下載jce檔案
https://www.oracle.com/java/technologies/javase-jce8-downloads.html
- 放入Java\jdk1.8.0_112\jre\lib\security目錄下替換掉原有jar包
- 在config-server的bootstrap.yml(application.yml裡不行)檔案里加配置
encrypt:
key: trademmc
- 測試
返回OK代表配置成功
5. 測試加密方法
- 解密方法
- 在配置檔案中就可以寫密文了
密文內容前需要寫{cipher},並且用單引號括起來
loginpass: '{cipher}941c15446567e8211931cf0d25f81696871f5a5ea74fac46638eed71db24e76b'
- 加上安全配置
避免直接訪問配置檔案
- 在config-server里加依賴
<!-- 安全配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
- bootstrap.yml配置檔案裡開啟
spring:
security:
user:
name: root
password: 123456
basic:
enable: true
- 客戶端裡也要配置相應的密碼
spring:
cloud:
config:
uri: http://localhost:9100
profile: test
label: master
username: root
password: 123456