搭建config client服务,从gitlab上拉取配置文件
在上一篇中iot创建一个maven modules,pom文件都一样
我取的项目名是iot-daemon-service项目
注意图中红色的部分,因为要通过iot-config服务去拉取gitlab上的配置信息
首先创建一个bootstrap.yml文件
spring:
application:
name: iot-daemon-service #服务名
profiles:
active: dev
cloud:
config:
fail-fast: false
discovery:
service-id: iot-config-server
enabled: true
profile: ${spring.profiles.active} #iot-daemon-service-dev
label: ${spring.profiles.active} #dev分支上拉取配置名为iot-daemon-service-dev.yml
---
spring:
profiles: dev
eureka:
instance:
prefer-ip-address: true
lease-renewal-interval-in-seconds: 5
lease-expiration-duration-in-seconds: 20
client:
serviceUrl:
defaultZone: http://root:root@127.0.0.1:1025/eureka
registry-fetch-interval-seconds: 10
1.加载顺序
这里主要是说明application和bootstrap的加载顺序。
bootstrap.yml(bootstrap.properties)先加载
application.yml(application.properties)后加载。
bootstrap.yml 用于应用程序上下文的引导阶段。
bootstrap.yml 由父Spring ApplicationContext加载。
父ApplicationContext 被加载到使用 application.yml 的之前。
启动类
package com.xhs.iot.daemon;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class IotDaemonServiceApplication {
public static void main(String[] args) {
SpringApplication.run(IotDaemonServiceApplication.class, args);
}
}
要创建一个配置文件,我把配置文件放到了自己搭建的gitlab上
配置文件名称最好是和服务名称一样
内容就是定义了一个端口好
启动项目
我的端口号变为刚刚gitlab上定义的端口4060,说明拉取到了。
也可以在config service上查看是否拉取到了配置信息,浏览器输入config service端口号在加上在gitlab上定义的文件名
127.0.0.1:4001/iot-daemon-service-dev/dev/dev iot-daemon-service-dev/dev 文件名称 后面的dev是该文件在哪个分支上dev分支