美文网首页
09、SpringCloud Config-Server(git

09、SpringCloud Config-Server(git

作者: adced06edef5 | 来源:发表于2020-03-29 13:08 被阅读0次

一、代码示例

说明:此处使用的SpringBoot版本为2.1.13.RELEASE,SpringCloud版本为Greenwich.SR5
SpringCloud Config为SpringCloud的分布式配置中心,包含Server和Client端,支持git和svn等方式,此处介绍的为Server。
SpringCloud Config git方式需要有一个git远程仓库。此处的远程仓库地址为:https://github.com/panli1988/config-server
此目录下有两个文件夹,分别为cloud01和cloud02:

image.png
image.png
到此准备工作结束,下面为java代码部分。
1、maven依赖
<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>

2、application.yml
git公开仓库不需要用户名和密码

server:
  port: 6001
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/panli1988/config-server
          username:
          password:
          #读取文件路径
          search-paths: /cloud01,/cloud02
      #分支
      label: master

3、启动类
启动类添加注解@EnableConfigServer

package org.example;

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

@SpringBootApplication
@EnableConfigServer
public class ConfigServer6001Application {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServer6001Application.class,args);
    }
}

二、测试验证

配置服务中心可以从远程程序获取配置信息,http请求地址和资源文件映射如下:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
此处以第二种为例进行测试。
先启动服务,先测试下读取cloud01下的配置文件,先后访问
http://localhost:6001/application-dev.yml
http://localhost:6001/application-test.yml

image.png
image.png
再尝试读取cloud02下的配置文件,先后访问
http://localhost:6001/provider-dev.yml
http://localhost:6001/provider-test.yml
image.png
image.png
以上均成功,说明代码生效。
github:
https://github.com/panli1988/cloud01
https://github.com/panli1988/cloud02
参考:
https://blog.csdn.net/forezp/article/details/70148833
http://www.itmuch.com/spring-cloud/spring-cloud-index/
还有尚硅谷周阳老师的视频

相关文章

网友评论

      本文标题:09、SpringCloud Config-Server(git

      本文链接:https://www.haomeiwen.com/subject/nlkyuhtx.html