6、如何获取配置中心的配置-创新互联

在《配置中心》这一篇博文里学习了如何git获取配置文件。大概的流程可以用下图来概括。
6、如何获取配置中心的配置
《配置中心》这篇博文说的是Config Server,本篇将和大家看看如何编写一个Config Client从Config Server获取配置。
1、 先在仓库中创建如下配置文件(具体参考下面地址)

创新互联长期为上千多家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为雁塔企业提供专业的成都网站制作、成都做网站、外贸营销网站建设雁塔网站改版等技术服务。拥有10余年丰富建站经验和众多成功案例,为您定制开发。
https://gitee.com/hjj520/spring-cloud-2.x/tree/master/config-repos/sc-config-client

2、 创建maven项目sc-config-client,对应的pom.xml如下


    4.0.0

    spring-cloud
    sc-config-client
    0.0.1-SNAPSHOT
    jar

    sc-config-client
    http://maven.apache.org

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.4.RELEASE
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Finchley.RELEASE
                pom
            

        
    

    
        UTF-8
        1.8
        1.8
    
    

        
            org.springframework.cloud
            spring-cloud-starter-config
            2.0.1.RELEASE
        

        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
            2.0.1.RELEASE
        

    

其中:spring-cloud-starter-config与spring-cloud-config-client可以二选一,但是根据选择的依赖不同对应的配置文件有些许不一样。spring-cloud-starter-config已经包含spring-cloud-config-client,所以选择依赖spring-cloud-starter-config。

3、 创建配置文件bootstrap.yml

#服务端口
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配置文件的如下配置项
6、如何获取配置中心的配置

4、 创建启动类ConfigClientApplication.java

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);
    }
}

5、 为了验证是否能不能在config server获取到配置项,创建一个restful类型的controller:ConfigController.java

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 getConfigFromGit() {
        Map result = new HashMap();
        result.put("code", "000000");
        result.put("msg", "ok");
        Map body = new HashMap();
        body.put("driverClassName", driverClassName);
        body.put("url", url);
        body.put("username", username);
        body.put("password", password);
        result.put("body", body);
        return result;
    }

}

6、 先启动注册中心,对应的项目为sc-eureka-server;再启动config sever,对应的项目为sc-config-server。然后验证一下config sever是否启动成功
方式一:访问注册中心,可以看到config sever已经注册到注册中心了
6、如何获取配置中心的配置

方式二:访问配置文件对应的路径看看是否可以获取配置文件,如果能获取到说明启动成功
6、如何获取配置中心的配置

给大家一一对应一下yml问下的访问方式,这些在config server那篇博文只是大概提了一下:

{[/{name}-{profiles}.yml || /{name}-{profiles}.yaml],methods=[GET]}:
http://127.0.0.1:8100/application-dev.yml
{[/{name}/{profiles:.*[^-].*}],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

7、 启动config client对应的项目sc-config-client
当spring.cloud.config.profile的值为dev时访问http://127.0.0.1:8200/config/getValue
6、如何获取配置中心的配置
当spring.cloud.config.profile的值为prd时访问http://127.0.0.1:8200/config/getValue
6、如何获取配置中心的配置
可以看到spring.cloud.config.profile配置不一样时,分配获取到git仓库的application-dev.yml和application-prd.yml配置文件的内容

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


文章名称:6、如何获取配置中心的配置-创新互联
网页地址:http://pwwzsj.com/article/dcossd.html