博客信息

Config Server及Client的使用

发布时间:『 2019-11-05 21:56』  博客类别:SpringCloud  阅读(811)


Config Server基本使用

根据前面SpringCloud架构图,首先第一步,要搞个 configServer来联通远程GIT仓库,来读取远程配置;

 

这里GIT仓库,我们一般选用GitHub https://github.com/,或者码云  https://gitee.com/  

我们这里GitHub演示

 

建个仓库 microservice-config  然后 Git下载本地;

 

上传一个配置文件上到git仓库,application.yml 记住要utf-8编码,否则乱码,解析各种问题;

 

文件内容:

profile: hello


小李飞刀_springcloud

随便搞,目前只要能读取到配置即可;

 

新建module:microservice-config-server-4001

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


完成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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.javaxl</groupId>
        <artifactId>testSpringcloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>microservice-config-server-4001</artifactId>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

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

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

</project>


启动类ConfigServerApplication_4001:

package com.javaxl.microserviceconfigserver4001;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class MicroserviceConfigServer4001Application {

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

}


这里要加下注解:@EnableConfigServer

 

这里我们搞下仓库的Http地址:

小李飞刀_springcloud

然后项目的application.yml配置下:

server:
  port: 4001

spring:
  application:
    name:  microservice-config
  cloud:
    config:
      server:
        git:
          uri: https://github.com/lixiao12/microservice-config.git


主要是要配置一个git请求地址:

 

本地Hosts加个本地域名映射:

127.0.0.1  configserver.javaxl.com


然后我们请求:http://configserver.javaxl.com:4001/application-xxx.yml

 

返回结果了正确的文本结果;

 

至于请求路径,有匹配规则:

The HTTP service has resources in the form:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties


Config Client 使用

根据前面的config原理图,我们需要建立Client端调用server端,最终实现client端获取远程git配置信息;

 

为了后面演示方便,我们提交三个配置文件到远程git库;

 

application.yml:

---
spring:
  profiles:
    active: dev
---
spring:
  profiles: dev
port: 111
---
spring:
  profiles: test
port: 222


crm-dev.yml

port:
  777


crm-test.yml

port:
  888


然后我们新建一个module  microservice-config-client-5001

加下依赖:

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


我们项目启动的时候,就要调用server config端,获取配置信息,所以这里要bootstrap.yml配置文件,优先级最高:

spring:
  application:
    name: application-dev
  cloud:
    config:
      name: crm
      uri: http://configserver.javaxl.com:4001
      profile: test
      label: master
      fail-fast: true


application.yml:

server:
  port: 5001
  context-path: /


再搞一个 ConfigClientController 类 测试显示端口:

package com.javaxl.microserviceconfigclient5001.controller;

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

@RestController
public class ConfigClientController {
 
    @Value("${port}")
    private String port;
 
    @GetMapping("/getPort")
    public String getPort() {
        return "测试你访问的yml文件的端口是:【"+port+"】";
    }

}


启动类:ConfigClientApplication_5001

package com.javaxl.microserviceconfigclient5001;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MicroserviceConfigClient5001Application {

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

}


最后 本地hosts我们加给配置:

127.0.0.1  client-config.javaxl.com


我们启动项目:然后页面访问:

http://client-config.javaxl.com:5001/getPort

即可获取远程端口配置信息;


小李飞刀_springcloud



小李飞刀_springcloud


小李飞刀_springcloud



小李飞刀_springcloud



over......


关键字:     springcloud       config       server       client  

备案号:湘ICP备19000029号

Copyright © 2018-2019 javaxl晓码阁 版权所有