构建模块

新建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: 8888eureka:  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@EnableConfigServerpublic class AppConfigServer {    public static void main(String[] args) {        SpringApplication.run(AppConfigServer.class,args); }}

启动我的项目

别离启动 Eureka Server,和 config-server。

码云gitee创立配置文件

命名标准

服务名称-环境.properties或者服务名称-环境.yml

创立配置文件

gkconfig ,目录上面创立两个配置文件:

  1. 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: 8883eureka:  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 */@RestControllerpublic 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@EnableEurekaClientpublic class AppConfigClient {    public static void main(String[] args) {        SpringApplication.run(AppConfigClient.class,args); }}

启动config-client


浏览器拜访:http://localhost:8883/getBabainfo

胜利读取到gitee下面的配置文件信息!