共计 5262 个字符,预计需要花费 14 分钟才能阅读完成。
构建模块
新建 springcloud-config-server 模块
- 引入 pom 文件:
<?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>springcloud-parents</artifactId>
<groupId>com.baba.wlb</groupId>
<version>1.0-SNAPSHOT</version>
</parent> <modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-config-server</artifactId>
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M7</version>
<type>pom</type>
<scope>import</scope>
</dependency> </dependencies> </dependencyManagement>
<dependencies>
<!--SpringCloud 整合 config-server--> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!--SpringCloud Eureka 客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency> </dependencies>
<!-- 留神:这里必须增加,否则各种依赖有问题 -->
<repositories>
<repository> <id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots> <enabled>false</enabled>
</snapshots> </repository> </repositories>
</project>
- 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 依赖:
<?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>springcloud-parents</artifactId>
<groupId>com.baba.wlb</groupId>
<version>1.0-SNAPSHOT</version>
</parent> <modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-config-client</artifactId>
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M7</version>
<type>pom</type>
<scope>import</scope>
</dependency> </dependencies> </dependencyManagement>
<dependencies>
<!--SpringCloud 整合 config-client--> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<!--SpringCloud Eureka 客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency> </dependencies>
<!-- 留神:这里必须增加,否则各种依赖有问题 -->
<repositories>
<repository> <id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots> <enabled>false</enabled>
</snapshots> </repository> </repositories></project>
- 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 下面的配置文件信息!
正文完