SpringBoot統一配置中心

一直使用springboot搭建後端項目,所有的配置都寫到自己的resource目錄下,隨着微服務的項目越來越多,每個項目都需要自己的各種配置文件。而且後期一旦想要修改配置文件,就得重新發布一遍非常的麻煩,現在就來教教大家怎麼統一在github上管理 這些配置,並做到一處修改處處生效,不需要重新發布項目。

1 創建統一服務項目

可以使用STS來初始化項目,選擇自己的以來就好。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.mike</groupId>
    <artifactId>config-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>config-server</name>
    <description>config server</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

創建bootstrap.yml文件,當然你可以使用application.ymlapplication.properties

spring:
  application:
    name: config-repo
  cloud:
    config:
      server:
        git:
          uri: https://github.com/mike/config-repo.git   #github倉庫地址
          username: mike      # 用戶名
          password: 123456      # 密碼

在github上創建一個config-repo倉庫,並添加配置文件:
配置
兩個不同環境的配置
hello-pj-dev.yml

hello:
  text: hello spring dev

hello-pj-uat.yml

hello:
  text: hello spring uat

創建啓動類:

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }

}

啓動應用,訪問:
http://localhost:8080/hello-pj-dev.yml
http://localhost:8080/hello-pj-uat.yml
你就可以看到遠程的配置中心。

2 創建測試項目

pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.mike</groupId>
    <artifactId>hello-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hello-server</name>
    <description>hello server</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

創建bootstrap.yml文件,只能是這個文件,不能是application文件

server:
  port: 8082
spring:
  application:
    name: hello-pj
  cloud:
    config:
      profile: dev
      uri: http://localhost:8080/

這裏面配置了讀取遠程配置文件的uri,注意application.name必須對應github上的文件名,最終訪問的是application.name+profile

創建測試controller


@RestController
public class TestController {
    @Value("${hello.text}")
    private String text;
    
    @GetMapping("/say")
    public String sayHello(){
        return text;
    }
}

訪問 http://localhost:8082/say,你就可以看到對應的配置。這樣你只需要在github上管理所有的配置就行了,但是記得'config-server'工程得一直啓動,通過它我們才能獲取github上的配置。

3 動態刷新配置

目前如果我們修改了github上的配置並不能馬上生效,需要我們的客戶端工程重啓才行,現在需要改造成自動刷新。

在客戶端工程中加入新的依賴:

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>

修改bootstrap.yml文件

server:
  port: 8082
spring:
  application:
    name: hello-pj
  cloud:
    config:
      profile: dev
      uri: http://localhost:8080/
    bus:
      trace:
        enabled: true
rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

注意需要藉助rabbitmq,所以本地需要啓動rabbitmq

在需要刷新的地方加入註解@RefreshScope


@RestController
@RefreshScope
public class TestController {
    @Value("${hello.text}")
    private String text;
    
    @GetMapping("/say")
    public String sayHello(){
        return text;
    }
}

這樣我們既可以隨時修改github上的配置文件,而不需要重啓應用。

如果對你有幫助,希望可以關注下我的公衆號:
mike啥都想搞

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章