SpringCloud(Finchley版)6 - Config-Client

一, 簡介

既然項目中已經有了配置中心 config-server , 那麼咱們怎麼去消費配置服務呢? 固然就是 config-client ;java

二, 建立 config-client 服務 cloud-g

1, pom.xml

<?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>com.gy.cloud</groupId>
        <artifactId>cloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>cloud-g</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>${project.artifactId}</name>
    <description>Demo project for config-client</description>

    <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.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- 配置刷新 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

</project>

2, bootstrap.properties

# 和 application 配置文件相比, bootstrap 配置文件具備如下幾個特性:
# 1, bootstrap 由父 ApplicationContext 加載,比 application 優先加載;
# 2, bootstrap 裏面的屬性不能被覆蓋;
# 3, bootstrap 和application 的應用場景:
#       application: 主要用於spring boot 項目的自動化配置;
#       bootstrap:
#           a, 使用 spring Cloud config 配置中心時, 這時須要在 bootstrap 配置文件中添加鏈接到配置中心的配置屬性來加載外部配置中心的配置信息;
#           b, 一些固定的不能被覆蓋的配置; c, 一些加密/解密的場景;

server.port=8767
spring.application.name=config-client
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

# 該配置在 bootstrap.properties(.yml) 文件才能生效 (配置服務)
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server

# 該配置在 bootstrap.properties(.yml) 文件才能生效 (配置服務)
#spring.cloud.config.uri= http://localhost:8766/

spring.cloud.config.label=master
spring.cloud.config.profile=dev

# 開放配置刷新接口 /actuator/refresh
management.endpoints.web.exposure.include=refresh

3, CloudGApplication

package com.gy.cloud.cloudg;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RefreshScope // 配置刷新
@RestController
@EnableDiscoveryClient
@SpringBootApplication
public class CloudGApplication {

    public static void main(String[] args) {
        SpringApplication.run(CloudGApplication.class, args);
        System.out.println("=== 服務G config-client 啓動SUCCESS ===");
    }

    @Value("${testUsername}")
    private String param;

    @GetMapping("getConfigParam")
    public String getConfigParam() {
        return param;
    }

}

4, 啓動 cloud-g

啓動日誌 : git

2019-01-11 11:03:04.093  INFO 10844 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://XUQ3937QAYKT5WO:8766/
2019-01-11 11:03:04.729  INFO 10844 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=config-client, profiles=[dev], label=master, version=f2f23650522db4787e8a2a7d3b030058a8598899, state=null
2019-01-11 11:03:04.730  INFO 10844 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='configClient'}, MapPropertySource {name='https://gitee.com/ge.yang/SpringBoot//src/main/resources/config/config-client-dev.properties'}]}

......

2019-01-11 11:03:09.462  INFO 10844 --- [           main] com.netflix.discovery.DiscoveryClient    : Completed shut down of DiscoveryClient
2019-01-11 11:03:09.474  INFO 10844 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 1 endpoint(s) beneath base path '/actuator'
2019-01-11 11:03:09.486  INFO 10844 --- [           main] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/actuator/refresh],methods=[POST],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)

根據啓動日誌, 發現 POST 接口 :  /actuator/refreshweb

1,   訪問 :  http://localhost:8767/getConfigParamspring

2,   修改Git倉庫 testUsername 值  訪問 :  http://localhost:8767/getConfigParamapache

3,   POST 訪問 :  http://localhost:8767/actuator/refreshjson

4,   訪問 :  http://localhost:8767/getConfigParambootstrap

三, 總結

注意 1, bootstrap 與 application 的區別;bash

        2, 注意 POST /actuator/refresh 刷新接口的使用;app

學習文檔

方誌朋的博客 :  https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f7-config/maven

項目源碼:  https://gitee.com/ge.yang/spring-demo/tree/master/cloud

相關文章
相關標籤/搜索