SpringCloud config native 配置

  • 2021 年 11 月 12 日
  • 筆記

1.概述

最近項目使用springCloud 框架,使用config搭建git作為配置中心。

在私有化部署中,出現很多比較麻煩的和雞肋的設計。

  • 每次部署都需要安裝gitlab
  • 有些環境安裝完gitlab,外面不能訪問,不給開端口
  • 實時同步比較麻煩

基於上述問題,決定將配置中心依springCloud config本地文件的方式進行改造

缺點就是每個服務器上都可以放配置文件

2、springCloud config配置方式

config配置方式有三種,本文主要介紹本地文件方式

  • git方式
  • svn方式
  • 本地文件方式

3、部署架構

springcloud config的流程架構

image-20211112104205223

4、環境搭建

springcloud config分為eureka服務,config服務器端和config客戶端

1、eureka服務端搭建

1、pom.xml

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Eureka-Server 依賴 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
 <dependencyManagement>
        <dependencies>
            <!-- SpringCloud 版本控制依賴 springboot版本需要匹配cloud版本 
            這裡用的boot版本2.3.2.RELEASE -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR12</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

2、aplication.yml

server:
  port: 8761

eureka:
  instance:
    hostname: 127.0.0.1 #eureka服務端的實例名稱2
  client:
    register-with-eureka: false     #false表示不向註冊中心註冊自己。
    fetch-registry: false     #false表示自己端就是註冊中心,我的職責就是維護服務實例,並不需要去檢索服務
    service-url:
      defaultZone: //127.0.0.1:8761/eureka/

3、啟動類

package com.feng;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class SpringCloud13EurekaApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringCloud13EurekaApplication.class, args);
    }
}

4、啟動測試

訪問地址://127.0.0.1:8761/

image-20211111181219443

2、config服務端搭建

1、pom.xml

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- Eureka client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <!--Spring Boot Actuator,感應服務端變化-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <!-- SpringCloud 版本控制依賴 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

2、application

server:
  port: 8888
spring:
  profiles:
    active: native #設置為本地config
  application:
    name: springcloud-config
  cloud:
    config:
      server:
        native:
          search-locations: d:/config-repo #本地配置的路徑
#        git:
#          uri: //222.175.101.224:8090/liuyusong/springcloud-config.git
#          search-paths: /config-repo
eureka:
  client:
    service-url.defaultZone: //127.0.0.1:8761/eureka/
  instance:
    prefer-ip-address: true

3、啟動類

package com.feng;


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;

@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class SpringCloud14ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringCloud14ConfigServerApplication.class, args);
    }

}

4、測試服務器端

瀏覽器訪問 //localhost:8888/springcloud-config/config-base-local.yml

瀏覽器訪問個 server端口/應用名字/文件名-環境名.yml

3、config客戶端搭建並測試

1、pom.xml

  <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- Eureka client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
        </dependency>
        <!--Spring Boot Actuator,感應服務端變化-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <!-- SpringCloud 版本控制依賴 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

2、application

server:
  port: 8889
spring:
  application:
    name: springcloud-15-config-client

3、bootstrap

spring:
  cloud:
    config:
      discovery:
        enabled: true
        service-id: springcloud-13-config-server #eureka的service
      name: config-test
#      name: config-test,config-test1 可以配置多個配置
eureka:
  client:
    service-url.defaultZone: //127.0.0.1:8761/eureka/
  instance:
    prefer-ip-address: true

4、啟動類

package com.feng;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class SpringCloud15ConfigClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringCloud15ConfigClientApplication.class, args);
    }
}

5、測試類

package com.feng.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @Value("${myconfig.name}")
    String name;
    @Value("${myconfig.version}")
    String version;
    @GetMapping("/hello")
    public  String hello()
    {
        return "ok1"+name+version;
    }

}


6、測試客戶端

測試地址://localhost:8889/hello

image-20211112103151952

5、總結

springcloud config 本地化配置的優點是不需要另外搭建gitlab或者svn,部署相對簡單

缺點是 如果有多個server端需要手動同步文件,每一個服務器都需要有文件

6、源碼地址