六、如何獲取配置中心的配置

在《配置中心》這一篇博文裏學習瞭如何git獲取配置文件。大概的流程能夠用下圖來歸納。java

clipboard.png

《配置中心》這篇博文說的是Config Server,本篇將和你們看看如何編寫一個Config Client從Config Server獲取配置。
一、 先在倉庫中建立以下配置文件(具體參考下面地址)git

https://gitee.com/hjj520/spring-cloud-2.x/tree/master/config-repos/sc-config-client

二、 建立maven項目sc-config-client,對應的pom.xml以下web

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

    <groupId>spring-cloud</groupId>
    <artifactId>sc-config-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>sc-config-client</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-starter-config</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>

        <!-- 
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>
         -->

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>

    </dependencies>
</project>

其中:spring-cloud-starter-config與spring-cloud-config-client能夠二選一,可是根據選擇的依賴不一樣對應的配置文件有些許不同。spring-cloud-starter-config已經包含spring-cloud-config-client,因此選擇依賴spring-cloud-starter-config。spring

三、 建立配置文件bootstrap.ymlapache

#服務端口
server:
  port: 8200

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:5001/eureka/
      
spring:
  application:
    name: sc-config-client
  cloud:
    config:
      label: master # 配置文件所在分支
      #uri: http://127.0.0.1:8100/  #配置服務中心
      profile: dev  # dev根據具體狀況來修改
      discovery:
        serviceId: sc-config-server #配置服務實例名稱
        enabled: true  #開啓配置服務發現

備註:sc-config-server爲配置服務實例名稱,對應sc-config-server項目的bootstrap.yml配置文件的以下配置項bootstrap

clipboard.png

四、 建立啓動類ConfigClientApplication.javarestful

package sc.config.client;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
//@EnableDiscoveryClient
@EnableEurekaClient
public class ConfigClientApplication {

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

五、 爲了驗證是否能不能在config server獲取到配置項,建立一個restful類型的controller:ConfigController.javaapp

package sc.config.client.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConfigController {

    // git配置文件裏的key
    @Value("${jdbc.driverClassName}") 
    private String driverClassName;
    
    @Value("${jdbc.url}") 
    private String url;
    
    @Value("${jdbc.username}") 
    private String username;
    
    @Value("${jdbc.password}") 
    private String password;
    
    @RequestMapping(value="/config/getValue")
    public Map<String, Object> getConfigFromGit() {
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("code", "000000");
        result.put("msg", "ok");
        Map<String, Object> body = new HashMap<String, Object>();
        body.put("driverClassName", driverClassName);
        body.put("url", url);
        body.put("username", username);
        body.put("password", password);
        result.put("body", body);
        return result;
    }

}

六、 先啓動註冊中心,對應的項目爲sc-eureka-server;再啓動config sever,對應的項目爲sc-config-server。而後驗證一下config sever是否啓動成功
方式一:訪問註冊中心,能夠看到config sever已經註冊到註冊中心了maven

clipboard.png

方式二:訪問配置文件對應的路徑看看是否能夠獲取配置文件,若是能獲取到說明啓動成功spring-boot

clipboard.png

給你們一一對應一下yml問下的訪問方式,這些在config server那篇博文只是大概提了一下:

{[/{name}-{profiles}.yml || /{name}-{profiles}.yaml],methods=[GET]}:
http://127.0.0.1:8100/application-dev.yml
{[/{name}/{profiles:.1.}],methods=[GET]}:
http://127.0.0.1:8100/application/dev
{[/{name}/{profiles}/{label:.*}],methods=[GET]}: http://127.0.0.1:8100/application/dev/master
{[/{label}/{name}-{profiles}.yml || /{label}/{name}-{profiles}.yaml],methods=[GET]}:
http://127.0.0.1:8100/master/application-dev.yml

七、 啓動config client對應的項目sc-config-client
當spring.cloud.config.profile的值爲dev時訪問http://127.0.0.1:8200/config/getValue

clipboard.png

當spring.cloud.config.profile的值爲prd時訪問http://127.0.0.1:8200/config/getValue

clipboard.png

能夠看到spring.cloud.config.profile配置不同時,分配獲取到git倉庫的application-dev.yml和application-prd.yml配置文件的內容


  1. -
相關文章
相關標籤/搜索