将一个系统中各个应用的配置文件集中起来,方便管理。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication
@EnableConfigServer
public class ConfigApplication { public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}

如上,对一个spring boot应用加上@EnableConfigServer就可以将其变成一个集中配置服务。

build.gradle文件:

buildscript {
ext {
springBootVersion = '1.4.2.RELEASE'
}
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
// Apply the java-library plugin to add support for Java Library
apply plugin: 'java-library'
apply plugin: 'java'
apply plugin: 'org.springframework.boot' version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
// In this section you declare where to find the dependencies of your project
repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
mavenCentral()
maven { url "http://dl.bintray.com/oembedler/maven" }
maven { url "https://repo.spring.io/libs-release" }
maven { url "http://10.100.122.249:8881/nexus/content/groups/public/" }
} dependencyManagement {
imports {
//mavenBom 'org.springframework.cloud:spring-cloud-netflix:1.2.0.M1'
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Camden.SR2'
}
} dependencies {
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
// https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-config-server
compile("org.springframework.cloud:spring-cloud-starter-parent:Camden.SR2")
compile("org.springframework.boot:spring-boot-starter-security")
compile("org.springframework.cloud:spring-cloud-config-server")
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-actuator")
}

config server自己的配置文件如下:

spring:
cloud:
config:
server:
native:
searchLocations: classpath:/shared
profiles:
active: native server:
port: 8888 security:
user:
password: root
native代表将各个接入应用的配置文件放在config server的本地目录,这里放在classpath:/shared ;并对接入应用开启验证,用户名user密码root; 
启动config server,验证http://localhost:8888/demo/application.property (这里假设在shared目录放入了demo应用的配置文件demo.property) 再来看看接入应用如何使用这个config server
在demo应用中:
//在dependencies中加入
compile("org.springframework.cloud:spring-cloud-starter-parent:Camden.SR2")
compile("org.springframework.cloud:spring-cloud-starter-config")
//在build.gradle根下加入如下依赖项
dependencyManagement {
imports {
  mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Camden.SR2'
  }
}

* 将项目resource下application.properties文件复制到server-config resource下的shared文件夹中
并重命名为项目名.properties,如demo项目,命名为demo.properties
* 清空application.properties内容
3. 在resource下新建bootstrap.yml文件(以demo项目举例)

spring:
application:
name: demo
profiles:
active: default
cloud:
config:
uri: http://localhost:8888/
name: demo
fail-fast: true
password: root
username: user

Spring Cloud实践之集中配置Spring-config的更多相关文章

  1. Spring Cloud 系列之 Consul 配置中心

    前面我们已经学习过 Spring Cloud Config 了: Spring Cloud 系列之 Config 配置中心(一) Spring Cloud 系列之 Config 配置中心(二) Spr ...

  2. Spring Cloud 系列之 Apollo 配置中心(三)

    本篇文章为系列文章,未读前几集的同学请猛戳这里: Spring Cloud 系列之 Apollo 配置中心(一) Spring Cloud 系列之 Apollo 配置中心(二) 本篇文章讲解 Apol ...

  3. Spring Cloud 系列之 Apollo 配置中心(四)

    本篇文章为系列文章,未读前几集的同学请猛戳这里: Spring Cloud 系列之 Apollo 配置中心(一) Spring Cloud 系列之 Apollo 配置中心(二) Spring Clou ...

  4. Spring Cloud 入门教程 - 搭建配置中心服务

    简介 Spring Cloud 提供了一个部署微服务的平台,包括了微服务中常见的组件:配置中心服务, API网关,断路器,服务注册与发现,分布式追溯,OAuth2,消费者驱动合约等.我们不必先知道每个 ...

  5. Spring Cloud:多环境配置、eureka 安全认证、容器宿主机IP注册

    记录一下搭建 Spring Cloud 过程中踩过的一些坑,测试的东西断断续续已经弄了好多了,一直没有时间整理搭建过程,时间啊~时间~ Spring 版本 Spring Boot:2.0.6.RELE ...

  6. spring cloud连载第二篇之Spring Cloud Config

    Spring Cloud Config Spring Cloud Config为分布式服务提供了服务侧和客户侧的外部配置支持.通过Spring Cloud Config你可以有一个统一的地方来管理所有 ...

  7. Spring Cloud(九):分布式配置中心和消息总线

    我们在Spring Cloud(七):使用SVN存储分布式配置中心文件和实现refresh中讲到,如果需要客户端获取到最新的配置信息需要执行refresh,我们可以利用webhook的机制每次提交代码 ...

  8. Spring Cloud 基于Consul 实现配置服务

    Spring Cloud体系中提供了Config组件来进行配置服务管理.而Consul除了提供服务注册与发现功能外,同时也提供配置管理功能.本位将介绍如何结合Spring Cloud + Consul ...

  9. spring cloud深入学习(十)-----配置中心和消息总线(配置中心终结版)

    如果需要客户端获取到最新的配置信息需要执行refresh,我们可以利用webhook的机制每次提交代码发送请求来刷新客户端,当客户端越来越多的时候,需要每个客户端都执行一遍,这种方案就不太适合了.使用 ...

随机推荐

  1. ros 编程习惯

    1.设置ros的info,warning,debug,error等编写的时候要思考,何时该使用,以及在开头要使用设置rosconsole的级别来对应输出,以方便调试. 2.在使用ros_info等的时 ...

  2. 移动开发学习touchmove

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  3. Python : locals and globals

    Python有两个内置的函数,locals() 和globals(),它们提供了基于字典的访问局部和全局变量的方式.Python使用叫做名字空间的东西来记录变量的轨迹.名字空间只是一个 字典,它的键字 ...

  4. python入门之文件处理

    1.读取文件 f=open(file="C:\BiZhi\新建文本文档.txt",mode="r",encoding="utf-8") da ...

  5. 8月的list

    多校的list: 第一周的多校list: k路归并 (思想大概理解了,还没实现 莫比乌斯 树归 第三场的多校list: 斯坦纳树 第四场多校: Pollard_rho算法和Miller_Rabin   ...

  6. kbmmw 5.05.00 发布

    新年前最后几天,kbmmw 发布了新版本,增加一大波功能.we are happy to announce v5.05.50 of our popular middleware for Delphi ...

  7. Explain结果解读与实践

    MySQL的EXPLAIN命令用于SQL语句的查询执行计划(QEP).这条命令的输出结果能够让我们了解MySQL 优化器是如何执行SQL 语句的.这条命令并没有提供任何调整建议,但它能够提供重要的信息 ...

  8. c# 产生随机数 程序所在路径

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  9. vue的cli中引入css文件

    在public文件中创建一个文件夹css,放进reset.css 在main.js中引入即可 import '../public/css/reset.css'就可以啦

  10. poj 3278 Catch That Cow(bfs+队列)

    Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...