首页 > 编程语言 > 详细

SpringCloud学习(八):集中配置组件Spring Cloud Config

时间:2020-02-17 12:39:40      阅读:82      评论:0      收藏:0      [点我收藏+]

8.1 Spring Cloud Config 简介

在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件Spring Cloud Config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在Spring Cloud Config 组件中,分两个角色,一是Config Server,二是Config Client。Config Server是一个可横向扩展、集中式的配置服务器,它用于集中管理应用程序各个环境下的配置,默认使用Git存储配置文件内容,也可以使用SVN存储,或者是本地文件存储。Config Client是Config Server的客户端,用于操作存储在Config Server中的配置内容。微服务在启动时会请求Config Server获取配置文件的内容,请求到后再启动容器。
 
详细内容看在线文档: https://springcloud.cc/spring-cloud-config.html 
 
8.2 配置服务端
本文选择码云作为git的远程仓库,理由如下:
使用GitHub时,国内的用户经常遇到的问题是访问速度太慢,有时候还会出现无法连接的情况。如果我们希望体验Git飞一般的速度,可以使用国内的Git托管服务——码云(gitee.com)。和GitHub相比,码云也提供免费的Git仓库。此外,还集成了代码质量检测、项目演示等功能。对于团队协作开发,码云还提供了项目管理、代码托管、文档管理的服务。
 
8.2.1 将配置文件提交到码云 
步骤:
(1) 注册账号(略)
(2) 创建项目 springCloudDemo-config (点击右上角的加号 ,下拉菜单选择创建项目)
技术分享图片

 

 (3) 上传配置文件,将springCloudDemo_eurekaClient_A工程的application.yml改名为ClientA-dev.yml后 上传

技术分享图片

 

 技术分享图片

技术分享图片

可以再次编辑此文件
技术分享图片文件命名规则:
{application}-{profile}.yml或{application}-{profile}.propertiesapplication为应用名称 profile指的开发环境(用于区分开发环境,测试环境、生产环境等)
 
(4) 复制git地址 ,备用
技术分享图片

 

 地址为: https://gitee.com/longQiudao/springCloudDemo-config.git

 

8.2.2 配置中心微服务

(1) 创建工程模块 配置中心微服务 springCloudDemo_config ,pom.xml引入依赖 

    <dependencies>
     <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
     </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>

(2) 创建启动类ConfigServerApplication 

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

@EnableConfigServer //开启配置服务
@SpringBootApplication
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class,args);
    }
}

(3) 创建application.yml 

spring:
  application:
    name: config
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/longQiudao/springCloudDemo-config.git
server:
  port: 12000
eureka:
  client:
    service-url:
     defaultZone: http://127.0.0.1:6868/eureka/
  instance:
    prefer-ip-address: true

(4) 启动项目测试:浏览http://localhost:12000/ClientA-dev.yml 可以看到配置内容 

技术分享图片

 8.2.3 配置客户端

(1) 在springCloudDemo_eurekaClient_A中添加依赖

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

(2) 将application.yml重命名为bootstrap.yml并修改其内容为如下

eureka:
client:
service-url:
defaultZone: http://127.0.0.1:6868/eureka/
instance:
prefer-ip-address: true
spring:
application:
name: clienta
cloud:
config:
name: ClientA
profile: dev
label: master
discovery:
enabled: true
service-id: config

(3) 测试:启动springCloudDemo_eurekaClient_A查看是否可以正常运行

 技术分享图片

SpringCloud学习(八):集中配置组件Spring Cloud Config

原文:https://www.cnblogs.com/tukoushuidedanianyu/p/12308362.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!