SpringCloud快速开始 | 统一配置中心(三)Greenwich版本

源码地址

原文连接:莫问博客

一、Spring Cloud Config基本概念

    Spring Cloud Config用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持。

  • 服务端:分布式配置中心,独立的微服务应用,用来连接配置仓库(GIT)并为客户端提供获取配置信息、加密/解密等访问接口。、
  • 客户端:微服务架构中各个微服务应用和基础设施,通过指定配置中心管理应用资源与业务相关的配置内容,启动时从配置中心获取和加载配置信息

二、创建config-server工程,添加依赖,删除src目录,并在工程下创建config-server1,config-server2

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

   2.1、config-server1,config-server2启动类加入@EnableConfigServer注解,代码如下

  

package com.lxd.config;

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

/**
 * @author liuxiaoding
 * @data 2019/5/4
 */
@SpringBootApplication
@EnableConfigServer
public class Config_1Application {
    public static void main(String[] args) {
        SpringApplication.run(Config_1Application.class,args);
    }
}

   2.2、config-server1,config-server2 yml配置文件如下,注意端口分别是7001、7002

   

server:
  port: 7002

spring:
  application:
    name: service-config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/xdliu0417/spingcloud-config.git
          username: 463800202@qq.com
          password: xxx
          search-paths: service-config
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:7081/eureka/,http://localhost:7082/eureka/,http://localhost:7083/eureka/

 2.3、依次启动config-server1,config-server2后查看注册中心http://localhost:7081/,发现已注册成功

 2.4、git配置文件配置方法

         2.4.1、配置中心的配置文件有个属性search-paths表示在git仓库中的文件夹路径

         2.4.2、在search-paths下创建yml文件,命名格式为(服务名-env.yml) 如文章示例中的external-api-service-dev.yml

2.5、mysql配置中心配置方法,如下application.yml文件配置

server:
   port: 5001

spring:
   application:
     name: config-server-jdbc
   cloud:
     config:
       server:
         jdbc:
           sql: SELECT ckey , cvalue FROM config where app=? and profile=? and label=?
   datasource:
     driver-class-name: com.mysql.jdbc.Driver
     url: jdbc:mysql://127.0.0.1:3306/config
     username: root
     password: root
   profiles:
     active: jdbc
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:7081/eureka/,http://localhost:7082/eureka/,http://localhost:7083/eureka/

三、依次启动config-server1、config-server2,检查是否注册到eureka中

      3.1、接入配置中心,首先在项目中需加入spring-cloud-context依赖,这样springcloud项目启动后会加载bootstrap.yml文件,并通过启动配置的配置中心地址拉取配置,并且bootstrap文件中取到的配置项不会被覆盖

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

    3.2、在src/main/resources中新建bootstrap.yml文件,加入如下配置

 spring:
  application:
    name: user-service
  cloud:
    config:
      name: ${spring.application.name}
      profile: ${spring.profiles.active}
      label: master
      discovery:
        service-id: service-config-server
        enabled: true
  profiles:
    active: dev

     3.3、准备工作完成,启动项目之后服务会根据注册中心地址,通过配置中心的service-id查找服务,根据name(服务名),profile(环境),label(分支名称),从git仓库拉取配置,在项目中可以通过注解获取配置项,如

@Value("${spring.datasource.url}")
private String url;

四、配置中心搭建完成之后我们可以将所有可变的配置项如数据库配置、redis连接配置等放到配置中心,极大简化了配置文件的书写

   

     

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值