1. spring cloud配置中心server

1.1 创建git仓库

首先在github上搭建一个存储配置中心的仓库,需要创建两个分支,一个是master,一个是dev分支。自己学习可以用公开库,真实环境使用的话,还是需要私库,或者自己搭建git服务器。

1.2 搭建server

使用spring cloud搭建服务器,工具使用idea,新建maven工程,配置pom.xml文件

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>

创建springboot启动类,启动类要加上@SpringBootApplication 和 @EnableConfigServer 这两个注解。

@SpringBootApplication: springboot启动注解

@EnableConfigServer: springcloud config server的注解,必须要加上

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

创建配置文件bootstrap.yml

spring:
cloud:
config:
server:
git:
uri: https://github.com/hanggle/ConfigCenter.git #git仓库地址,就是刚才创建的git仓库
skipSslValidation: true #跳过校验
basedir: d:///myspace///config-center///config #从git仓库拉取到的文件在本地存储的位置,可自行修改或删掉,默认存储在C盘
# bootstrap: true
server:
port: 8889

config-server的目录结构:

启动springboot,启动成功后可以在浏览器查看拉取到的配置信息,路径的访问有以下几种:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

我创建的文件名是:application-dev.yml ,application-prod.yml

路径中占位符的表示是:

application 对应我文件中的 application
profile 对应 dev或prod
label 对应分支 master(默认是master 分支) 访问示例:
默认master分支

dev分支:

有兴趣的同学可以都试试,配置没问题都可以访问得到。

到此单机配置服务中心搭建完成了。

2. spring cloud配置中心client

使用spring cloud搭建服务器,工具使用idea,新建maven工程,配置pom.xml文件

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>

创建springboot启动类

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

创建测试controller

@RestController
public class TestController { @Value("${mydev}")
private String userName;
@Value("${profile}")
private String profile;
@Value("${name}")
private String name; @GetMapping("/test")
public String home() {
return "mydev: " + userName +" profile:" + profile + " name:" + name;
} }

配置文件

application.yml

server:
port: 8081
name: config-client
mydev: ${profile}

bootstrap.yml

spring:
application:
name: application # 指的是application-dev.xml中的application
cloud:
config:
uri: http://localhost:8889 # config-server 地址
profile: dev # 后缀 指的是application-dev.xml中的dev
label: dev # git 分支

项目结构

启动成功后访问

我在Github上的dev分支配置

在上面我通过两种方式读取配置属性:

1、直接在java文件中使用(peofile属性)

2、是在xml文件中陪之后再在java中使用(mydev属性)

比较推荐第二种,虽然多了一步,但是可以清除的知道来源,方便找问题。

3. 总结

到此初步完成简单的使用和测试,复杂的使用还需要再研究官方文档:http://cloud.spring.io/spring-cloud-config/single/spring-cloud-config.html

单一服务肯定没办法保证高可用性,具体方案待续。。。。。。

spring cloud 配置中心的更多相关文章

  1. 记录一个 spring cloud 配置中心的坑,命令行端口参数无效,被覆盖,编码集问题无法读取文件等.

    spring cloud 配置中心 结合GIT , 可以运行时更新配置文件.发送指令让应用重新读取配置文件. 最近在测试服务器实现了一套,结果CPU 实用率暴增,使用docker compose启动 ...

  2. Spring Cloud配置中心(Config)

    Spring Cloud配置中心(Config) Spring Cloud是现在流行的分布式服务框架,它提供了很多有用的组件.比如:配置中心.Eureka服务发现. 消息总线.熔断机制等. 配置中心在 ...

  3. springcloud(五):Spring Cloud 配置中心的基本用法

    Spring Cloud 配置中心的基本用法 1. 概述 本文介绍了Spring Cloud的配置中心,介绍配置中心的如何配置服务端及配置参数,也介绍客户端如何和配置中心交互和配置参数说明. 配置中心 ...

  4. springcloud(六):Spring Cloud 配置中心采用数据库存储配置内容

    Spring Cloud 配置中心采用数据库存储配置内容 转自:Spring Cloud Config采用数据库存储配置内容[Edgware+] Spring Cloud Server配置中心采用了G ...

  5. (七)Spring Cloud 配置中心config

      spring cloud config是一个基于http协议的远程配置实现方式. 通过统一的配置管理服务器进行配置管理,客户端通过http协议主动的拉取服务的的配置信息,完成配置获取. 下面我们对 ...

  6. Spring Cloud配置中心搭建(集成Git)

    1. 在Github(或其他)创建配置中心仓库bounter-config-repo,然后在仓库创建两个配置文件:simon.properties.susan.properties,链接如下: htt ...

  7. Spring Cloud配置中心内容加密

    从配置获取的配置默认是明文的,有些像数据源这样的配置需要加密的话,需要对配置中心进行加密处理. 下面使用对称性加密来加密配置,需要配置一个密钥,当然也可以使用RSA非对称性加密,但对称加密比较方便也够 ...

  8. Spring Cloud配置中心高可用搭建

    本文通过config server连接git仓库来实现配置中心,除了git还可以使用svn或者系统本地目录都行. 引入依赖 <dependencies> <dependency> ...

  9. Spring Cloud配置中心客户端读取配置

    微服务连接配置中心来实现外部配置的读取. 引入依赖 <dependencies> <dependency> <groupId>org.springframework ...

随机推荐

  1. <<梦断代码>>读后感

    <梦断代码>中对软件工程所面临的种种困难与艰难的描述,即便再过5年读也许都不过时.因为正如原作者所说,书中描写的是一队人马并肩扛起代码大石,虽历经磨难仍欲将其推上山顶的故事,而正是这种故事 ...

  2. GridView的控件说明[字典]-----方便查询

    GridView 控件以表格的形式显示数据,并提供对数据进行排序,选择,编辑,删除等功能. GridView能够完成的功能具体可以总结如下: 1,通过数据源控件将数据绑定到GridView控件 2,对 ...

  3. 打开ubuntu终端的两个方法【最快速】

    两种快捷方法: 1. ctrl+alt+T. 2. 桌面右击,再点击终端.

  4. svm小问题

    1.没有报错但是结果是pedicttestlabel = [] accuracy = [] 举例:(前提是装了工具箱libsvm-3.21) data=[178,80;172,75;160,50;15 ...

  5. yii 验证码 CCaptcha的总结(转)

    今天用到yii的验证码 ccaptcha,经过在网上搜寻 找到以下例子: 1.在controller中加入代码 (1)启用 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 &l ...

  6. 在sql server ide里数据修改数据

    在sql server 的客户端工具ssms里,只有在工具里打开后直接修改. 除了用这种方法外,还有其它方法可以改吗?比如像pl/sql里的for update sql server的客户端功能比较差 ...

  7. java中的==操作符和equals函数

    基本规则 “==”操作符的使用需要分成两种情况 判值类型相等 这一点很好理解,两个值类型代表的数值相等,则“==”表达式返回true “==”可以用与不同值类型的比较,语言会自动进行类型转换 判引用类 ...

  8. 一些基于jQuery开发的控件

    基于jQuery开发,非常简单的水平方向折叠控件.主页:http://letmehaveblog.blogspot.com/2007/10/haccordion-simple-horizontal-a ...

  9. Python 变量 (上)

    Python通过变量引用内存中的值,变量的值占用多少空间是由变量的类型决定的.声明变量不需要指定变量的类型,解释器会自动根据值来判断.变量名称必须符合标识符的定义 标识符 标识符是由字母,数字和下划线 ...

  10. Common Substrings POJ - 3415(长度不小于k的公共子串的个数)

    题意: 给定两个字符串A 和 B, 求长度不小于 k 的公共子串的个数(可以相同) 分两部分求和sa[i-1] > len1  sa[i] < len1  和  sa[i-1] < ...