Spring Cloud Config Server

 2023-09-05 阅读 407 评论 0

摘要:spring cloud config server 官方文档 本地保存配置文件 先创建一个空项目创建 config-server 作为Model创建再创建 config-client 作为model 创建 以下是config-server的 build.gradle 文件 buildscript {ext {springBootVersion = '1.5.7.RELEASE'}repositorie

spring cloud config server 官方文档

本地保存配置文件

先创建一个空项目
image
image
创建 config-server 作为Model创建
image
image
image
image
image
image
再创建 config-client 作为model 创建
image
image
image
image
image
image

以下是config-server的 build.gradle 文件

buildscript {ext {springBootVersion = '1.5.7.RELEASE'}repositories {mavenCentral()}dependencies {classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")}
}apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8repositories {mavenCentral()
}ext {springCloudVersion = 'Dalston.SR3'
}dependencies {compile('org.springframework.cloud:spring-cloud-config-server')testCompile('org.springframework.boot:spring-boot-starter-test')
}dependencyManagement {imports {mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"}
}

config-server 目录结构
image

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

# 配置中心存储配置文件的方式
spring.profiles.active: native
# 应用名称
spring.application.name: config-server
# 端口
server.port: 8888
#本地配置文件存放路径 (${spring.profiles.active} 为native 是才有效)
spring.cloud.config.server.native.searchLocations: classpath:/config

启动config-server

image
image

启动成功

image
image

以下是configclient配置

buildscript {ext {springBootVersion = '1.5.7.RELEASE'}repositories {mavenCentral()}dependencies {classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")}
}apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8repositories {mavenCentral()
}ext {springCloudVersion = 'Dalston.SR3'
}dependencies {compile('org.springframework.cloud:spring-cloud-starter-config')compile('org.springframework.boot:spring-boot-starter-web')testCompile('org.springframework.boot:spring-boot-starter-test')
}dependencyManagement {imports {mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"}
}
package com.cn.nur;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class ConfigClientApplication {public static void main(String[] args) {SpringApplication.run(ConfigClientApplication.class, args);}
}

bootstrap读取顺序先于application.yml文件,所以bootstrap中指定配置中心地址
bootstrap.properties

# Flag to say that remote configuration is enabled. Default true;
spring.cloud.config.enabled=true
# Name of application used to fetch remote properties.
# 远程配置文件名称  如 service0-{profile}.properties
# @Value("${spring.application.name:application}")
#
spring.cloud.config.name=service0
# The URI of the remote server (default http://localhost:8888). 配置服务器
spring.cloud.config.uri=http://localhost:8888
# 要启用的环境(test,pro,dev 之类的) 配置服务器得有 ${spring.cloud.config.name}-{spring.cloud.config.profile}.properties文件
spring.cloud.config.profile=test
#spring.cloud.config.label=config-label-test

项目目录结构
image

启动config-client
image
说明配置文件已经从配置中心服务器获取
image

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/441.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息