三、配置中心

一、當一個系統中的配置文件發生改變的時候,常常的作法是從新啓動該服務,才能使得新的配置文件生效,spring cloud config能夠實現微服務中的全部系統的配置文件的統一管理,並且還能夠實現當配置文件發生變化的時候,系統會自動更新獲取新的配置。java

 

 

將配置文件放入git或者svn等服務中,經過一個Config Server服務來獲取git或者svn中的配置數據,二其餘服務須要配置數據時在經過Config Client從Config Server獲取。git

二、在git倉庫新建以下圖目錄spring

 

 

具體內容查看:https://gitee.com/hjj520/spring-cloud-2.x/tree/master/config-reposapache

三、 新建maven項目sc-config-server,對應pom.xmlbootstrap

 

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"app

   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">maven

   <modelVersion>4.0.0</modelVersion>svn

 

   <groupId>spring-cloud</groupId>spring-boot

   <artifactId>sc-config-server</artifactId>微服務

   <version>0.0.1-SNAPSHOT</version>

   <packaging>jar</packaging>

 

   <name>sc-config-server</name>

   <url>http://maven.apache.org</url>

 

   <parent>

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-starter-parent</artifactId>

      <version>2.0.4.RELEASE</version>

   </parent>

 

   <dependencyManagement>

      <dependencies>

        <dependency>

           <groupId>org.springframework.cloud</groupId>

           <artifactId>spring-cloud-dependencies</artifactId>

           <version>Finchley.RELEASE</version>

           <type>pom</type>

        </dependency>

      </dependencies>

   </dependencyManagement>

 

   <properties>

      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

      <maven.compiler.source>1.8</maven.compiler.source>

      <maven.compiler.target>1.8</maven.compiler.target>

   </properties>

 

   <dependencies>

      <dependency>

        <groupId>org.springframework.cloud</groupId>

        <artifactId>spring-cloud-config-server</artifactId>

        <version>2.0.1.RELEASE</version>

      </dependency>

 

 

      <dependency>

        <groupId>org.springframework.cloud</groupId>

         <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>

        <version>2.0.1.RELEASE</version>

      </dependency>

   </dependencies>

</project>

 

四、 新建類ConfigServerApplication.java

 

package sc.config.server;

 

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.config.server.EnableConfigServer;

import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

 

@SpringBootApplication

@EnableConfigServer

@EnableEurekaClient

public class ConfigServerApplication {

 

public static void main(String[] args) {

       SpringApplication.run(ConfigServerApplication.class, args);

}

}

 

五、建立bootstrap.yml文件

 

#服務端口

server:

  port: 8100

 

#服務註冊中心

eureka:

  client:

    registerWithEureka: true #是否將本身註冊到Eureka服務中,默認爲true

    fetchRegistry: true #是否從Eureka中獲取註冊信息,默認爲true

    serviceUrl:

      defaultZone: http://localhost:5001/eureka/

  instance:

    prefer-ip-address: true #將本身的ip地址註冊到Eureka服務中

    ipAddress: 127.0.0.1

 

spring:

  application:

    name: sc-config-server #服務名稱

  cloud:

    config:

      label: master #配置文件所在的分支

      server:

        git:

          uri: https://gitee.com/hjj520/spring-cloud-2.x.git #服務的git倉庫地址

          #git倉庫的用戶名

          #username: huangjinjin

          #git倉庫的密碼

          #password: ********

          search-paths: /config-repos/sc-consumer-config  #配置文件所在的目錄

 

備註:search-paths可使用佔位符{application},不過須要注意的必須使用這樣的方式:’{application}’ (單引號引發來),否則可能出現https://blog.csdn.net/weixin_35022258/article/details/79019033帖子說的問題,具體這個佔位符之後會說到。

 

六、啓動註冊中心Eureka,而後在啓動sc-config-server項目

 

 

http請求地址和資源文件映射以下:

/{application}/{profile}[/{label}]

/{application}-{profile}.yml

/{label}/{application}-{profile}.yml

/{application}-{profile}.properties

/{label}/{application}-{profile}.properties

具體可使用哪一種http請求地址和資源文件映射能夠在config server的日誌能夠看到

 

 

七、驗證獲取倉庫中的配置數據

http://127.0.0.1:8100/application/dev

 

 

http://127.0.0.1:8100/application/prd

 

相關文章
相關標籤/搜索