七: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;
}
}

最新文章

  1. MMORPG大型游戏设计与开发(攻击区域 扇形)
  2. [HTML/HTML5]9 使用表单
  3. git下载自己项目到本地
  4. html中相似的标签、属性的区别
  5. 模式串匹配,kmp
  6. 剑指offer
  7. 哈夫曼(Huffman)编码
  8. jmeter 响应结果分析一
  9. S5PV210开发板刷机(SD卡uboot、串口+USB-OTG刷机方法)
  10. Math 对象 识记
  11. leetcode刷题笔记342 4的幂
  12. C++设计模式——模板方法模式
  13. ADO SQL手写分页
  14. FTP:500 OOPS: failed to open vsftpd log file:/var/log/vsftpd.log
  15. 学习windows编程 day2 之滚动条使用
  16. MYSQL的服务不见了
  17. HK Openstack Summit 归来有感
  18. UVA-208 Firetruck (回溯)
  19. uedit富文本编辑器及图片上传控件
  20. 再说rocketmq消息存储

热门文章

  1. 第三方模块的下载与使用、requests模块、爬取链家二手房数据、openpyxl模块、hashlib加密模块
  2. git回滚操作系列
  3. Java对象拷贝原理剖析及最佳实践
  4. Java开发学习(四十六)----MyBatisPlus新增语句之id生成策略控制及其简化配置
  5. 彻底理解Python中的闭包和装饰器(下)
  6. 使用docker中的MySQL
  7. 从稍微懂一点开始的C++学习之路1: 智能指针
  8. Jmeter 之提取的值为null时,if控制器中的判断表达式
  9. opencv-python学习之旅
  10. vlc qt player 播放器开发实例