spring boot配置ip_3、配置中心

1、当一个系统中的配置文件发生改变的时候,经常的做法是重新启动该服务,才能使得新的配置文件生效,spring cloud config可以实现微服务中的所有系统的配置文件的统一管理,而且还可以实现当配置文件发生变化的时候,系统会自动更新获取新的配置。

f1738fd95106c83fff8afe965abbd5c6.png

将配置文件放入git或者svn等服务中,通过一个Config Server服务来获取git或者svn中的配置数据,二其他服务需要配置数据时在通过Config Client从Config Server获取。

2、 在git仓库新建如下图目录

63e8637f5f059bd7230df86372bb98c1.png

具体内容查看:https://gitee.com/hjj520/spring-cloud-2.x/tree/master/config-repos

3、 新建maven项目sc-config-server,对应pom.xml

"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">

4.0.0

spring-cloud

sc-config-server

0.0.1-SNAPSHOT

jar

sc-config-server

http://maven.apache.org

org.springframework.boot

spring-boot-starter-parent

2.0.4.RELEASE

org.springframework.cloud

spring-cloud-dependencies

Finchley.RELEASE

pom

UTF-8

1.8

1.8

org.springframework.cloud

spring-cloud-config-server

2.0.1.RELEASE

org.springframework.cloud

spring-cloud-starter-netflix-eureka-server

2.0.1.RELEASE

4、新建类ConfigServerApplication.java

package sc.config.server;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.config.server.EnableConfigServer;

import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication

@EnableConfigServer

@EnableEurekaClient

public class ConfigServerApplication {

public static void main(String[] args) {

SpringApplication.run(ConfigServerApplication.class, args);

}

}

5、创建bootstrap.yml文件

#服务端口

server:

port: 8100

#服务注册中心

eureka:

client:

registerWithEureka: true #是否将自己注册到Eureka服务中,默认为true

fetchRegistry: true #是否从Eureka中获取注册信息,默认为true

serviceUrl:

defaultZone: http://localhost:5001/eureka/

instance:

prefer-ip-address: true #将自己的ip地址注册到Eureka服务中

ipAddress: 127.0.0.1

spring:

application:

name: sc-config-server #服务名称

cloud:

config:

label: master #配置文件所在的分支

server:

git:

uri: https://gitee.com/hjj520/spring-cloud-2.x.git #服务的git仓库地址

#git仓库的用户名

#username: huangjinjin

#git仓库的密码

#password: ********

search-paths: /config-repos/sc-consumer-config #配置文件所在的目录

备注:search-paths可以使用占位符{application},不过需要注意的必须使用这样的方式:’{application}’ (单引号引起来),不然可能出现https://blog.csdn.net/weixin_35022258/article/details/79019033帖子说的问题,具体这个占位符以后会说到。

6、启动注册中心Eureka,然后在启动sc-config-server项目

274527f51492dcc0cee286e0e8ab8a5d.png

http请求地址和资源文件映射如下:

/{application}/{profile}[/{label}]

/{application}-{profile}.yml

/{label}/{application}-{profile}.yml

/{application}-{profile}.properties

/{label}/{application}-{profile}.properties

具体可以使用哪种http请求地址和资源文件映射可以在config server的日志可以看到

a2a6a2c5189d2aebcb52edd40ef5c899.png

7、验证获取仓库中的配置数据

http://127.0.0.1:8100/application/dev

3597ec906d7af4a3c9066096fd2acbe6.png

http://127.0.0.1:8100/application/prd

3dc64f54610c42f7c25ac85da3ed0e54.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值