前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Cloud(4)——分布式配置中心

Spring Cloud(4)——分布式配置中心

作者头像
会跳舞的机器人
发布2018-09-21 16:10:09
3790
发布2018-09-21 16:10:09
举报

一、简介

Spring Cloud Config是一个配置管理工具包,让你可以把配置放到远程服务器,集中化管理集群配置,目前支持本地存储、Git以及Subversion。 Spring Cloud Config分为两部分

  • config-server:配置服务端,负责管理配置信息
  • config-client:配置客户端,通过调用server端暴露的接口来换取配置信息

二、项目实例

创建maven工程microservice-config-server

1、在pom.xml中加入以下依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.4.RELEASE</version>
  </parent>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>

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

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Camden.SR5</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

2、创建ConfigServerApplication.java

package com.baibei.config.server;

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

/**
 * @author: 会跳舞的机器人
 * @email:2268549298@qq.com
 * @date: 17/2/19 下午2:35
 * @description:分布式配置中心启动主类
 */
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

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

添加@EnableConfigServer注解开启Config Server

3、在resource文件夹下创建application.properties文件

#config-server对外提供的端口
server.port=9001

#git repo的url地址
spring.cloud.config.server.git.uri=https://git.oschina.net/dreyer/microservice-config-repo.git
#指定搜索路径,config-server会自动搜索根目录和指定目录(逗号分隔)下的文件
spring.cloud.config.server.git.searchPaths=api,backend
#有读取权限的git用户
spring.cloud.config.server.git.username=username
#git用户密码
spring.cloud.config.server.git.password=password

4、服务端验证

为了完成服务端的验证,我们需要在git上创建一个项目作为配置仓库,目录结构如下:

. ├── api │ ├── business-dev.properties │ ├── business-prod.properties │ └── business-test.properties ├── exchange │ ├── business-dev.properties │ ├── business-prod.properties │ └── business-test.properties ├── component-dev.properties ├── component-prod.properties ├── component-test.properties ├── database-dev.properties ├── database-prod.properties ├── database-test.properties 接下来,我们就可以通过URL来访问到我们配置的内容了。 例如我们可以通过 **URL与配置文件的映射关系如下:** - /{application}/{profile}[/{label}] 如: http://localhost:9001/database/dev - /{application}-{profile}.yml 如: http://localhost:9001/database-dev.yml - /{label}/{application}-{profile}.yml 如: http://localhost:9001/f-branch/database-dev.yml (f-branch分支下的database-dev.yml文件) - /{application}-{profile}.properties 如: http://localhost:9001/database-dev.properties (**不指定分支默认是MASTER分支**) - /{label}/{application}-{profile}.properties 如: http://localhost:9001/f-branch/database-dev.properties 例如database-dev.properties 对应{application}-{profile}.properties, {application} 就是 database, {profile}就是dev。 {label}对应git上不同的分支,默认为master。 ### 5.使用本地配置 开发人员在本机进行开发时,可能会用到自已本地的一些配置信息,如果把这些配置提交至git上的话,又会影响到其他同事的开发,那么在这种场景下,开发人员是可以选择使用本地配置,而不是git上的配置信息的,做法也很简单,只需要在config-server的application.properties中添加spring.profiles.active=native,然后把相关的配置文件放在src/main/resource下即可 ### 6、配置中心客户端 以 [Spring Cloud(2)—服务提供者](http://www.jianshu.com/p/ff8db428d365) 中的microservice-provider-user作为配置中心客户端 #### 6.1. 在pom.xml中增加Config Server所需要的jar包

org.springframework.cloud spring-cloud-starter-config


#### 6.2. 创建bootstrap.properties来指定Config Server配置,内容如下:

spring.cloud.config.uri=http://localhost:9001/ spring.cloud.config.profile=dev spring.cloud.config.label=master spring.cloud.config.name=database,component,business

内容含义如下:
- spring.application.name:对应前配置文件中的{application}部分
- spring.cloud.config.profile:对应前配置文件中的{profile}部分
- spring.cloud.config.label:对应前配置文件的git分支
- spring.cloud.config.uri:配置中心的地址

以上工作完成后,在代码中我们就可以使用@Value注解来调用相关的配置信息,

@Value(“${datasource.url}”) private String dataSourceUrl;

配置中心完成之后,在microservice-provider-user工程的application.properties中的数据库配置就可以换为

spring.datasource.url=${datasource.url} spring.datasource.username=${datasource.username} spring.datasource.password=${datasource.password}

在配置中心客户端启动的时候,我们也可以看到相关的日志信息输出

2017-02-19 15:17:28.220 INFO 11206 — [ main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at: http://localhost:9001/ 2017-02-19 15:17:29.565 INFO 11206 — [ main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=database,component,business, profiles=[dev], label=master, version=a9f8f18e682a03f4d7f046c754472f394313fa82, state=null 2017-02-19 15:17:29.565 INFO 11206 — [ main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource [name=’configService’, propertySources=[MapPropertySource [name=’configClient’], MapPropertySource [name=’https://git.oschina.net/dreyer/microservice-config-repo.git/database-dev.properties‘]]] 2017-02-19 15:17:29.595 INFO 11206 — [ main] c.b.p.user.UserProviderApplication : No active profile set, falling back to default profiles: default ```

附项目目录截图:

image
image
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-11-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、简介
  • 二、项目实例
  • 1、在pom.xml中加入以下依赖
  • 2、创建ConfigServerApplication.java
  • 3、在resource文件夹下创建application.properties文件
  • 4、服务端验证
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档