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

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

最新文章

  1. ios - NSTimer中target的self是强引用问题
  2. sqlserver 锁与阻塞
  3. H5的一些小问题
  4. paip.自适应网页设计 跟 响应式 设计方法与工具补充(2)o54
  5. java服务器知识
  6. python的工作记录A
  7. How to configure CDB in Qt Creator(使用VC调试器)
  8. springMvc获取servletContext
  9. (NO.00003)iOS游戏简单的机器人投射游戏成形记(十八)
  10. 日志管理工具之logrotate
  11. OpenWRT平台搭建及简单应用[转帖]+华为HG255D编译实践(20190323更新)
  12. python 面向对象(五)约束 异常处理 MD5 日志处理
  13. 一个canonical标签解决site不在首页的问题
  14. Oracle触发器报错
  15. POJ-2081 Recaman's Sequence
  16. CentOS7安装OpenStack(Rocky版)-05.安装一个nova计算节点实例
  17. session与cookie详解
  18. 20155310 2016-2017-2 《Java程序设计》第2周学习总结
  19. 实用符号Alt+小键盘快输
  20. 关于for循环的一个小问题

热门文章

  1. iframe高度自适应实现方案
  2. ros pluginlib 段错误
  3. 使用yarn 安装 Vue-DevTools
  4. Linux硬件信息命令大全
  5. springboot 增加过滤器方法
  6. Win7 VS2013环境使用cuda_7.5.18
  7. 【转载】Impala和Hive的区别
  8. Oracle数据库使用mybatis的时候,实体类日期为Date类型,mybatis里面定义的是Date类型,插入的时候,时分秒全部是12:00:00问题
  9. SpringMVC处理模型数据
  10. poj 1094 Sorting It All Out 拓补排序