springcloud config(配置管理)

创建config server 

首先创建一个springboot项目,添加依赖eureka server  、config server

启动类添加注解:@EnableConfigServer 

package com.example.config;



import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

import org.springframework.cloud.config.server.EnableConfigServer;

import org.springframework.cloud.openfeign.EnableFeignClients;



@SpringBootApplication

@EnableDiscoveryClient

@EnableFeignClients

@EnableConfigServer   这个注解用于使config server启动

public class ConfigApplication {



    public static void main(String[] args) {

        SpringApplication.run(ConfigApplication.class, args);

    }

}

配置文件添加:

spring:

  application:

    name: config

  cloud:

    config:

      server:

        git:

          uri: https://gitee.com/tf5488/test.git    配合文件存放的仓库地址

          username: 1174742234@qq.com    用户名

          password: tf13994574546        用户密码



eureka:

  client:

    service-url:

      defaultZone: http://localhost:4000/eureka



server:

  port: 4040

测试:

可以通过访问该端口下的 localhost:4040/{applicationName}/{profile}/{label}

Config支持我们使用的请求的参数规则为:

  • / { 应用名 } / { 环境名 } [ / { 分支名 } ]

  • / { 应用名 } - { 环境名 }.yml

  • / { 应用名 } - { 环境名 }.properties

  • / { 分支名 } / { 应用名 } - { 环境名 }.yml

  • / { 分支名 } / { 应用名 } - { 环境名 }.properties

这样就可以获取到文件中的内容了。

 

config client 的使用

首先在需要配置动态配置文件的服务端,添加依赖:

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-config</artifactId>

</dependency>

添加一个多余的配置文件,用于获取线上配置文件信息:

spring:

  application:

    name: zuulserver    这个名字需要慎重,仓库文件名字 zuulserver-状态信息.yml

  cloud:

    config:

      uri: http://localhost:4040   config server的地址

      label: master 分支名

      profile: dev  状态信息

eureka:

  client:

    service-url:

      defaultZone: http://localhost:4000/eureka

添加一个主配置文件:

server:

  port: ${server.port}  这个位置的名字就是配置文件中写的名字



zuul:

  routes:

    client:

      path: /user/**

      serviceId: client

以上就是单个服务器回去配置的方法,如果是多个服务器的话,在目标仓库下配置多个符合命名规范的配置文件,然后进行获取就可以了。配置文件中还可以配置其他的内容,通过不同的${server.port} 进行获取。

 

 

匹配并配置多个仓库

spring: 

    cloud: 

        config: 

            server: 

                git: 

                    uri: https://github.com/spring-cloud-samples/config-repo #默认的仓库 

                    repos: 

                        simple: https://github.com/simple/config-repo 

                    special: 

                        pattern: special*/dev*,*special*/dev* 

                        uri: https://github.com/special/config-repo 

                    local: 

                        pattern: local* 

                        uri: file:/home/configsvc/config-repo 

                    test: 

                        pattern: 

                            - '*/development' 

                            - '*/staging' 

                            uri: https://github.com/development/config-repo

上边的例子中,

simple 仓库自动匹配到 simple/*

special 仓库的pattern,第一个是应用名以special开头,环境名以dev开头;第二个是应用名包含special,环境名以dev开头;多个匹配到同一uri的pattern用逗号分割

local 仓库的的pattern也会自动补全为local*/*

test仓库中的 pattern 是以通配符开始的,需要使用单引号

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值