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. [buaa-SE-2017]个人作业-回顾

    个人作业-回顾 提问题的博客:[buaa-SE-2017]个人作业-Week1 Part1: 问题的解答和分析 1.1 问题:根据书中"除了前20的学校之外,计科和软工没有区别"所 ...

  2. 实验三 敏捷开发和XP实验

    课程:Java程序设计实验   班级:1352             姓名: 于佳心           学号:20135206 成绩:               指导教师:娄嘉鹏         ...

  3. 进阶系列(12)—— C#异步编程

    一.What's 异步? 启动程序时,系统会在内存中创建一个新的进程.进程是构成运行程序资源的集合. 在进程内部,有称为线程的内核对象,它代表的是真正的执行程序.系统会在 Main 方法的第一行语句就 ...

  4. 福大软工 Alpha 事后诸葛亮

    写在前面 林燊大哥 一路走来,好不容易,终于完结了. 设想和目标 1. 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 解决的问题 用户在进店之前无法得知店铺的优劣 ...

  5. 软工1816 · 作业(十二)Beta答辩总结

    组长博客 宣传视频 github团队项目仓库 本组成员 队员姓名与学号 124 王彬(组长) 206 赵畅 215 胡展瑞 320 李恒达 131 佘岳昕 431 王源 206 陈文垚 209 陈志炜 ...

  6. 面向对象程序设计第三次作业-Calculator

    题目: 最终代码: Scan.h: Print.h: Calaulator.cpp: 解题过程 看到题目后,在查询之后明白了这是多文件的题目,然后通过翁凯老师的视频讲解知道了.h和.cpp文件的区别和 ...

  7. Git初用心得

    第一次使用git,因为之前操作系统的实验需要,在虚拟机中使用过lniux系统,所以对这种用指令输入而不是图形化的程序感觉不是很陌生.感觉git还是很人性化的,git gui就是图形界面,操作起来也不复 ...

  8. mvc拦截请求IHttpModule

    代码: using System; using System.Collections.Generic; using System.Linq; using System.Web; using Syste ...

  9. boolean类型的按位或||和|的区别

    boolean类型既可以使用&&和||做逻辑运算,也可以使用&和|做逻辑运算,但前者是经过优化的(执行短路运算),后者未优化. 以下代码验证: 逻辑或|| public cla ...

  10. (转)web开发流程

    a.项目经理与公司决策层的沟通,以确定这个需求有没有足够的人手和可行性去实现,以及与现有产品的依存关系. b.公司决策层与市场/策划部门的交流,这个过程将进行的相当充分,并且是反复.长期的,它致力于从 ...