Spring Cloud Config 本地配置
2024-09-08 16:59:58
七:Spring Cloud Config 本地配置
本地文件系统
我们可以将微服务的相关配置文件存储到本地文件中,然后让微服务来读取本地文件。
创建本地文件 Config Server
1.创建模块,配置环境
<?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>springCloud01</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>nativeConfigServer</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
</project>
2.配置application.yml
server:
port: 8762
spring:
application:
name: nativeconfigserver
profiles:
active: native
cloud:
config:
server:
native:
search-location: classpath:/shared
3.创建文件夹shared、文件configclient-dev.yml
server:
port: 8070
foo: foo version 1
4.编写启动类
package com.southwind;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class nativeServerApplication {
public static void main(String[] args) {
SpringApplication.run(nativeServerApplication.class,args);
}
}
配置中心已经完成
创建客户端
1.创建模块,配置环境
<?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>springCloud01</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>nativeconfigclient</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
</project>
2.创建文件
bootstrap.yml
spring:
application:
name: configclient
profiles:
active: dev
cloud:
config:
uri: http://localhost:8762
fail-fast: true
3启动类:
package com.southwind;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class nativeConfigCliehtApplication {
public static void main(String[] args) {
SpringApplication.run(nativeConfigCliehtApplication.class,args);
}
}
4.handler:
package com.southwind.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("/native")
public class NativeConfigHandler {
@Value("${server.port}")
private String port;
@Value("${foo}")
private String foo;
@GetMapping("/index")
public String index(){
return this.port+"---"+this.foo;
}
}
最新文章
- MMORPG大型游戏设计与开发(攻击区域 扇形)
- [HTML/HTML5]9 使用表单
- git下载自己项目到本地
- html中相似的标签、属性的区别
- 模式串匹配,kmp
- 剑指offer
- 哈夫曼(Huffman)编码
- jmeter 响应结果分析一
- S5PV210开发板刷机(SD卡uboot、串口+USB-OTG刷机方法)
- Math 对象 识记
- leetcode刷题笔记342 4的幂
- C++设计模式——模板方法模式
- ADO SQL手写分页
- FTP:500 OOPS: failed to open vsftpd log file:/var/log/vsftpd.log
- 学习windows编程 day2 之滚动条使用
- MYSQL的服务不见了
- HK Openstack Summit 归来有感
- UVA-208 Firetruck (回溯)
- uedit富文本编辑器及图片上传控件
- 再说rocketmq消息存储