1. 程式人生 > >創建Spring Config工程

創建Spring Config工程

etc sta pom registry 中心 cover 拉取 com one

1、初始化工程

https://start.spring.io/

選config server Eureka Discovery

2、導入IDE改改

入口類上加@EnableConfigServer @EnableDiscoveryClient

application.yml

server:

port: 1027

spring:

application:

name: vishnu-config

cloud:

config:

server:

git:

uri: https://gitee.com/frankawp/vishnu-config

eureka:

instance:

prefer-ip-address: true

lease-renewal-interval-in-seconds: 5

lease-expiration-duration-in-seconds: 20

client:

serviceUrl:

defaultZone: http://localhost:1026/eureka

registry-fetch-interval-seconds: 10

把配置文件傳到這個git上。 vishnu-userinfo-dev.yml

啟動後在 localhost:1027/vishnu-userinfo/dev 上可以看到這個配置文件就對了

先啟動eureka,再啟動config項目,能看到註冊的config服務

3、使用config配置

回到vishnu-userinfo工程pom.xml加依賴

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-config</artifactId>

</dependency>

拉取config的配置不能放在application.yml裏面 將application.yml改為bootstrap.yml配置修改為

server:

port: 1028

spring:

application:

name: vishnu-userinfo

cloud:

config:

fail-fast: true

discovery:

service-id: vishnu-config

enabled: true

profile: dev

label: master

eureka:

instance:

prefer-ip-address: true

lease-renewal-interval-in-seconds: 5

lease-expiration-duration-in-seconds: 20

client:

serviceUrl:

defaultZone: http://localhost:1026/eureka

registry-fetch-interval-seconds: 10

自動從註冊中心拉config服務

創建Spring Config工程