SpringCloud config分布式配置

目录

什么是SpringCloud config

分布式系统面临的配置文件问题

SpringCloud config

 如何使用远程SpringCloud config配置

新建远程配置文件 

server端连接远程config

client端连接远程config 


 

什么是SpringCloud config

 

分布式系统面临的配置文件问题

每个微服务都需要必要的配置信息才能运行,所以一套集中式的,动态的配置管理设施是必不可少的。SpringCloud提供了ConfigServer来解决这个问题。
 

SpringCloud config

它为每个微服务提供了集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环节提供一个中心化的外部配置。 

 

 

 如何使用远程SpringCloud config配置

 

新建远程配置文件 

我们的远程配置基于码云。需要先在码云上建立一个仓库 

然后我们clone到本地

 

新建一个application.yml文件,写入如下内容

然后把对这个项目的更改提交到码云上

 

server端连接远程config

这个微服务我们叫springcloud-config-server-3344,编写配置文件,注意关联的uri指的是码云上的https克隆路径

server:
  port: 3344

spring:
  application:
    name: springcloud-config-server
    #连接远程仓库
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/LiuTongssss/springcloud-config.git
          username: xxxxx
          password: xxxxx
          skip-ssl-validation: true

导入依赖 

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>
    </dependencies>

新建启动类 

package com.lt;

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

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

启动项目,当你请求下面的路径时,惊奇的发现这不是git上的项目吗?~~~

到此你的git项目上的配置文件和你的微服务项目的配置文件连接起来了,想改什么直接在git上改就能生效啦~ 

请求路径还可以通过下面几种方式写

 

client端连接远程config 

在从git上clone下来的项目中新建一个config-client.yml文件

spring:
    profiles:
        active: dev

---
server:
    port: 8201
#spring配置
spring:
    profiles: dev
    application:
        name: springcloud-provider-dept

#eureka的配置
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/

---
server:
    port: 8202
#spring配置
spring:
    profiles: test
    application:
        name: springcloud-provider-dept

#eureka的配置
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/


编写好之后我们给它提交到远程上。然我们新建一个微服务springcloud-config-client-3355,导入依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

新建两个配置文件,分别是系统级别的配置和用户级别的配置

新建启动类

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

注意,我们虽然新建了3355服务,但是没有设置3355端口号。我们在config-client.yml中默认启动的是dev的8201端口,我们访问下8201端口

对于client端,连接的是server端,通过server端连接git上的配置文件 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值