天天看點

SpringCloud學習筆記(八、配置伺服器)

為什麼配置伺服器

有時候,微服務要做叢集,這就意味着,會有多個微服務執行個體。 在業務上有時候需要修改一些配置資訊,比如說 版本資訊吧~ 倘若沒有配置服務, 那麼就需要挨個修改微服務,挨個重新部署微服務,這樣就比較麻煩。

為了減少工作量,可以把 這些配置資訊就會放在一個公共的地方,比如git, 然後通過配置伺服器把它擷取下來,然後微服務再從配置伺服器上取下來。

這樣隻要修改git上的資訊,那麼同一個叢集裡的所有微服務都立即擷取相應資訊了,這樣就大大節約了開發,上線和重新部署的時間了。

如圖所示,我們先在 git 裡儲存 version 資訊, 然後通過 ConfigServer 去擷取 version 資訊, 接着不同的視圖微服務執行個體再去 ConfigServer 裡擷取 version.

SpringCloud學習筆記(八、配置伺服器)

在git上建立配置檔案

本來想在github上面建立的,結果網卡了一點,是以就在碼雲上建立了一個。碼雲可以直接用github的賬号登入。

1、建立倉庫

選擇公開吧,免得還得輸密碼什麼的。

SpringCloud學習筆記(八、配置伺服器)

2、建立檔案夾

建立一個名為respo的檔案夾

SpringCloud學習筆記(八、配置伺服器)

3、建立配置檔案

建立配置檔案product-view-service-dev.properties

裡面就一行東西:

version = LaughterYoung springcloud version 1.0      

建立子項目

建立子項目cofig-server

pom.xml

添加spring-cloud-config-serve依賴

<?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</artifactId>
        <groupId>edu.hpu.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>config-server</artifactId>


    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

    </dependencies>


</project>      

配置

application.yml:

spring:
  application:
    name: config-server
  cloud:
    config:
      label: master                         #表示分支
      server:
        git:
          uri: https://gitee.com/LaughterYoung/springCloudConfig/   #表示git位址
          searchPaths: respo                       #表示目錄
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/      

啟動類

ConfigServerApplication:

package edu.hpu.springcloud;

import cn.hutool.core.util.NetUtil;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableConfigServer                 //表示這是一個配置伺服器
@EnableDiscoveryClient
@EnableEurekaClient
public class ConfigServerApplication {
    public static void main(String[] args) {
        int port = 8030;
        if(!NetUtil.isUsableLocalPort(port)) {
            System.err.printf("端口%d被占用了,無法啟動%n", port );
            System.exit(1);
        }
        new SpringApplicationBuilder(ConfigServerApplication.class).properties("server.port=" + port).run(args);

    }
}      

啟動并通路

運作啟動類,通路位址:

http://localhost:8030/version/dev
SpringCloud學習筆記(八、配置伺服器)

參考:

【1】、

http://how2j.cn/k/springcloud/springcloud-config-server/2047.html#nowhere

【2】、

https://blog.csdn.net/yjw123456/article/details/84330523#Git_117