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