從零搭建一個SpringCloud專案之Config(五)

女友在高考發表於2020-04-16

配置中心

一、配置中心服務端

  1. 新建專案study-config-server
  2. 引入依賴
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
</dependency>
  1. 加配置檔案
server:
  port: 9100
spring:
  application:
    name: study-config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/mmcLine/cloudconfigTest.git
          username: mmcLine
          password: ********

  1. 加註解 @EnableConfigServer
@SpringBootApplication
@EnableConfigServer
public class StudyConfigApplication {

    public static void main(String[] args) {
        SpringApplication.run(StudyConfigApplication.class);
    }
}
  1. 建立一個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
  1. 訪問規則

二、配置中心客戶端

  1. 引入依賴
 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
 </dependency>
  1. 配置檔案,新增bootstrap.yml檔案
spring:
  application:
    name: study-trade
  cloud:
    config:
      uri: http://localhost:9100
      profile: test
      label: master

注意:git上的配置yml檔名要對應這裡配置的spring.application.name

  1. git配置檔案演示

  1. 測試配置是否生效
@RestController
public class ConfigTestController {


    @Value("${ordernotiry}")
    private Boolean ordernotiry;

    @RequestMapping("/testconfig")
    public String testconfig(){
        return String.valueOf(ordernotiry);
    }
}

到此最簡單的配置中心搭建完成!!!

三、升級不重啟修改配置檔案

整合bus的方案

服務端修改:

  1. 加依賴
<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>
  1. 啟動一個rabbitmq
  2. 配置檔案
spring:
  cloud:
    bus:
      enabled: true
      trace:
        enabled: true
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

management:
  endpoints:
    web:
      exposure:
        include: "bus-refresh"

客戶端:

  1. 加依賴
<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>
  1. 加註解

在讀取配置的類上加@RefreshScope註解

@RestController
@RefreshScope
public class ConfigTestController {


    @Value("${ordernotiry}")
    private Boolean ordernotiry;

    @RequestMapping("/testconfig")
    public String testconfig(){
        return String.valueOf(ordernotiry);
    }
}
  1. git上修改配置檔案後,post方式呼叫http://localhost:9100/actuator/bus-refresh即可

四、配置加密

  1. 下載jce檔案

https://www.oracle.com/java/technologies/javase-jce8-downloads.html

  1. 放入Java\jdk1.8.0_112\jre\lib\security目錄下替換掉原有jar包
  2. 在config-server的bootstrap.yml(application.yml裡不行)檔案里加配置
encrypt:
  key: trademmc
  1. 測試

http://localhost:9100/encrypt/status

返回OK代表配置成功
5. 測試加密方法

  1. 解密方法

http://localhost:9100/decrypt

  1. 在配置檔案中就可以寫密文了

密文內容前需要寫{cipher},並且用單引號括起來

loginpass: '{cipher}941c15446567e8211931cf0d25f81696871f5a5ea74fac46638eed71db24e76b'
  1. 加上安全配置

避免直接訪問配置檔案

  • 在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

git地址:https://github.com/mmcLine/spring-cloud-study

相關文章