1

构建模块

新建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:
          - gkconfig
      #### 读取分支环境
 label: master

gitee --> 新建仓库-->新建文件夹 gkconfigimage.png

  • 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。
image.png

码云gitee创建配置文件

命名规范

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

创建配置文件

gkconfig ,目录下面创建两个配置文件:

  1. test-configClient-prd.properties
babainfo=pro.baba.znkj.com

2.test-configClient-sit.properties

babainfo=sit.baba.znkj.com

image.png

再次访问Eureka上的服务,
image.png
image.png
成功读取到gitee上面的配置文件信息!

config-client模块

构建springcloud-config-client

在这之前我们在gitee上新建两个项目需要用到的配置文件:
image.png

  • 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);
 }
}

image.png

启动config-client

image.png
浏览器访问:http://localhost:8883/getBabainfo
image.png
成功读取到gitee上面的配置文件信息!


isWulongbo
228 声望26 粉丝

在人生的头三十年,你培养习惯,后三十年,习惯铸就你