温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring Cloud Config如何使用本地配置文件

发布时间:2021-07-19 00:39:40 来源:亿速云 阅读:280 作者:chen 栏目:开发技术

本篇内容主要讲解“Spring Cloud Config如何使用本地配置文件”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Spring Cloud Config如何使用本地配置文件”吧!

一、简介

在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。

在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。

在spring cloud config 组件中,分两个角色,一是config server,二是config client。

二、配置

2.1 Spring Cloud Config Server项目

1 pom.xml中导入Config Server需要的包

<dependency>             <groupId>org.springframework.cloud</groupId>             <artifactId>spring-cloud-config-server</artifactId>         </dependency>

2 在Application类中添加@EnableConfigServer注解

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

3 修改配置文件application.yml,指定本地客户端配置文件的路径

spring:   profiles:     active: native   cloud:     config:       server:         native:           searchLocations: F:/conf

4 准备客户端配置文件

Spring Cloud Config如何使用本地配置文件

client-dev.yml文件的内容:

server:   #设置成0,表示任意未被占用的端口   port: 8081 nickName: world

2.2 Spring Cloud Config Client项目

1 pom.xml中导入Config Client需要的包(注意,此处跟Config Server的配置不同)

<dependency>             <groupId>org.springframework.cloud</groupId>             <artifactId>spring-cloud-starter-config</artifactId>         </dependency>

2 在src/main/resources中,新建bootstrap.yml文件

bootstrap文件会在application文件之前加载,一般是不会变的。

spring:   application:     name: client   cloud:     config:       uri: http://127.0.0.1:8888       profile: dev       label: master

资源文件映射如下:

/{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties

3 新建HelloController用来显示读取到的配置

package com.sunbufu.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController {     @Value("${nickName}")     private String nickName;     @RequestMapping("/hello")     public String hello() {         return "hello " + nickName;     } }

Spring Cloud Config如何使用本地配置文件

三、总结

源码地址 :https://github.com/sunbufu/sunbufu-cloud

总觉的使用svn或者git不如直接修改配置文件方便,特此记录下来。

spring cloud config本地读取配置文件

1、创建maven项目,引入spring boot 起步依赖

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">     <modelVersion>4.0.0</modelVersion>     <parent>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-parent</artifactId>         <version>2.1.7.RELEASE</version>         <relativePath/> <!-- lookup parent from repository -->     </parent>     <groupId>com.wxz</groupId>     <artifactId>cloud-config-demo3</artifactId>     <version>0.0.1-SNAPSHOT</version>     <name>cloud-config-demo3</name>     <description>Demo project for Spring Boot</description>     <properties>         <java.version>1.8</java.version>     </properties>     <dependencies>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter</artifactId>         </dependency>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-test</artifactId>             <scope>test</scope>         </dependency>     </dependencies>     <dependencyManagement>         <dependencies>             <dependency>                 <groupId>org.springframework.cloud</groupId>                 <artifactId>spring-cloud-dependencies</artifactId>                 <version>${spring-cloud.version}</version>                 <type>pom</type>                 <scope>import</scope>             </dependency>         </dependencies>     </dependencyManagement>     <build>         <plugins>             <plugin>                 <groupId>org.springframework.boot</groupId>                 <artifactId>spring-boot-maven-plugin</artifactId>             </plugin>         </plugins>     </build> </project>

新建config-server 模块,引入依赖

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">     <modelVersion>4.0.0</modelVersion>     <parent>         <groupId>com.wxz</groupId>         <artifactId>cloud-config-demo3</artifactId>         <version>0.0.1-SNAPSHOT</version>         <relativePath/> <!-- lookup parent from repository -->     </parent>     <groupId>com.wxz</groupId>     <artifactId>config-server</artifactId>     <version>0.0.1-SNAPSHOT</version>     <name>config-server</name>     <description>Demo project for Spring Boot</description>     <properties>         <java.version>1.8</java.version>     </properties>     <dependencies>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter</artifactId>         </dependency>         <dependency>             <groupId>org.springframework.cloud</groupId>             <artifactId>spring-cloud-config-server</artifactId>             <version>2.1.2.RELEASE</version>         </dependency>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-test</artifactId>             <scope>test</scope>         </dependency>     </dependencies>     <build>         <plugins>             <plugin>                 <groupId>org.springframework.boot</groupId>                 <artifactId>spring-boot-maven-plugin</artifactId>             </plugin>         </plugins>     </build> </project>

2、配置文件:

spring:   cloud:     config:       server:         native:           search-locations: classpath:/shared   profiles:     active: native   application:     name: config-server server:   port: 8769

在resources下新建目录shared,里面新建文件config-client-dev

server:   port: 8762 foo: foo version 1

在启动类添加

@EnableConfigServer

新建config-client模块

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">     <modelVersion>4.0.0</modelVersion>     <parent>         <groupId>com.wxz</groupId>         <artifactId>cloud-config-demo3</artifactId>         <version>0.0.1-SNAPSHOT</version>         <relativePath/> <!-- lookup parent from repository -->     </parent>     <groupId>com.wxz</groupId>     <artifactId>config-client</artifactId>     <version>0.0.1-SNAPSHOT</version>     <name>config-client</name>     <description>Demo project for Spring Boot</description>     <properties>         <java.version>1.8</java.version>     </properties>     <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>             <version>2.1.2.RELEASE</version>         </dependency>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-test</artifactId>             <scope>test</scope>         </dependency>     </dependencies>     <build>         <plugins>             <plugin>                 <groupId>org.springframework.boot</groupId>                 <artifactId>spring-boot-maven-plugin</artifactId>             </plugin>         </plugins>     </build> </project>

新建配置文件bootstrap.yml(bootstrap比application具有优先的读取顺序)

spring:   cloud:     config:       uri: http://localhost:8769       fail-fast: true   application:     name: config-client   profiles:     active: dev

新建一个controller进行测试:

package com.wxz.configclient.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /**  * @author Wangxingze  * @date 2019-08-26 12:58  */ @RestController public class Test {     @Value("${foo}")     public String  foo;     @GetMapping("/t")     public String t(){         return foo;     } }

依次启动server client,启动时可以看到读取了配置文件和启动黛安克,访问:

Spring Cloud Config如何使用本地配置文件

注意:spring boot 和cloud的版本以及config依赖的版本

<groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-parent</artifactId>         <version>2.1.7.RELEASE</version>         <relativePath/> <!-- lookup parent from repository -->

我这里好像没有指定spring cloud 的版本呀

config的相关依赖使用:<version>2.1.2.RELEASE</version>

到此,相信大家对“Spring Cloud Config如何使用本地配置文件”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI