构建模块
新建springcloud-config-server模块
- 引入pom文件:
springcloud-parents
com.baba.wlb
1.0-SNAPSHOT
4.0.0
springcloud-config-server
org.springframework.cloud
spring-cloud-dependencies
Finchley.M7
pom
import
org.springframework.cloud
spring-cloud-config-server
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
spring-milestones
Spring Milestones
https://repo.spring.io/libs-milestone
false
- application.yml配置文件:
##服务端口号
server:
port: 8888
eureka:
client:
service-url:
##当前服务注册到Eureka服务地址
defaultZone: http://localhost:8100/eureka,http://localhost:9100/eureka
register-with-eureka: true
## 需要检索服务信息
fetch-registry: true
## 服务注册名称
spring:
application:
name: config-server
cloud:
config:
server:
git:
#### config-server读取git环境地址
uri: https://gitee.com/svavo_admin/config.git
search-paths:
64. gkconfig
#### 读取分支环境
label: master
gitee --> 新建仓库-->新建文件夹 gkconfig
- AppConfigServer 启动类:
package com.baba.wlb;
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;
/**
78. @Author wulongbo
79. @Date 2021/1/27 14:57
80. @Version 1.0
*/@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class AppConfigServer {
public static void main(String[] args) {
SpringApplication.run(AppConfigServer.class,args);
}
}
启动项目
分别启动 Eureka Server,和 config-server。
码云gitee创建配置文件
命名规范
服务名称-环境.properties或者服务名称-环境.yml
创建配置文件
在gkconfig ,目录下面创建两个配置文件:
- test-configClient-prd.properties
babainfo=pro.baba.znkj.com
2.test-configClient-sit.properties
babainfo=sit.baba.znkj.com
再次访问Eureka上的服务,
成功读取到gitee上面的配置文件信息!
config-client模块
构建springcloud-config-client
在这之前我们在gitee上新建两个项目需要用到的配置文件:
- pom依赖:
springcloud-parents
com.baba.wlb
1.0-SNAPSHOT
4.0.0
springcloud-config-client
org.springframework.cloud
spring-cloud-dependencies
Finchley.M7
pom
import
org.springframework.cloud
spring-cloud-config-client
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
spring-milestones
Spring Milestones
https://repo.spring.io/libs-milestone
false
- bootstrap.yml配置文件:
##服务端口号
server:
port: 8883
eureka:
client:
service-url:
##当前服务注册到Eureka服务地址
defaultZone: http://localhost:8100/eureka,http://localhost:9100/eureka
register-with-eureka: true
## 需要检索服务信息
fetch-registry: true
## 服务注册名称
spring:
application:
#### 服务名称要和gitee上的配置文件服务名称一致
name: config-client
cloud:
config:
#### 读取版本环境
profile: sit
discovery:
#### config server 服务注册别名
service-id: config-server
#### 开启读取权限
enabled: true
- PropertyController控制页面:读取gitee配置信息
package com.baba.wlb.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author wulongbo
* @Date 2021/1/27 16:06
* @Version 1.0
*/
@RestController
public class PropertyController {
@Value("${babainfo}")
private String babainfo;
@RequestMapping("/getBabainfo")
public String getBabainfo(){
return babainfo;
}
}
- 启动类AppConfigClient:
package com.baba.wlb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @Author wulongbo
* @Date 2021/1/27 16:09
* @Version 1.0
*/@SpringBootApplication
@EnableEurekaClient
public class AppConfigClient {
public static void main(String[] args) {
SpringApplication.run(AppConfigClient.class,args);
}
}
启动config-client
浏览器访问:http://localhost:8883/getBabainfo
成功读取到gitee上面的配置文件信息!