Spring Cloud - Spring Cloud 本地配置(本地配置概述、本地配置实现)
一、Spring Cloud Config 本地配置概述
- 本地文件系统:可以将微服务的相关配置文件存储到本地文件中,然后让微服务来读取本地配置文
二、本地配置
1、实现本地配置
(1)创建工程
- 创建子工程(Moudle),在 pom.xml 文件中配置相关依赖
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>SpringCloudTest</artifactId><groupId>com.my</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>NativeConfigServer</artifactId><dependencies><!-- Config Server --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency></dependencies></project>
(2)配置文件
- 在 recourses 目录下创建并配置 application.yaml 文件
server:port: 8762
spring:application:name: NativeConfigServer# 类型profiles:active: native# 配置中心cloud:config:server:native:search-locations: classpath:/shared
(3)本地配置文件
- 在 recourses 目录下创建 shared 目录,在该目录下创建并配置 NativeConfigClient-dev.yaml 本地配置文件
server:port: 8070
foo: foo version 1
(4)启动类
- 创建启动类
package com.my;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication// 本地配置中心
@EnableConfigServer
public class NativeConfigServerApplication {public static void main(String[] args) {SpringApplication.run(NativeConfigServerApplication.class, args);}
}
2、使用本地配置
(1)创建工程
- 创建子工程(Moudle),在 pom.xml 文件中配置相关依赖,读取配置中心的配置文件信息
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>SpringCloudTest</artifactId><groupId>com.my</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>NativeConfigClient</artifactId><dependencies><!-- Config --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency></dependencies></project>
(2)配置文件
-
在 recourses 目录下创建并配置 bootstrap.yaml
-
通过 uri 找到配置中心
-
通过 name 和 active 的拼接,找到配置中心的配置文件 NativeConfigClient-dev.yaml
-
spring:application:name: NativeConfigClientprofiles:active: devcloud:config:uri: http://localhost:8762fail-fast: true
(3)Controller
- 创建 NativeConfigClientHandler 类,读取读取配置中心的配置文件信息
package com.my.controller;import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/nativeConfigClient")
public class NativeConfigClientHandler {@Value("${server.port}")private String port;@Value("${foo}")private String foo;@GetMapping("/test")public String test() {return port + " - " + foo;}
}
(4)创建启动类
- 创建启动类
package com.my;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class NativeConfigClientApplication {public static void main(String[] args) {SpringApplication.run(NativeConfigClientApplication.class, args);}
}
3、测试
-
依次启动注册中心、本地配置中心、NativeConfigClient
-
使用 Postman 测试,请求地址(GET 请求):http://localhost:8070/nativeConfigClient/test
