01 Spring Cloud Config 實現配置中心

来源:https://www.cnblogs.com/sxpujs/archive/2020/02/29/12386077.html
-Advertisement-
Play Games

Spring Cloud官網: 本篇主要講 "Spring Cloud Config" ,參考內容如下: "Spring Cloud Config 2.2.1.RELEASE參考文檔" "Spring Cloud Config 實現配置中心,看這一篇就夠了" 實現簡單的配置中心 配置文件就在Spri ...


Spring Cloud官網: https://spring.io/projects/spring-cloud

本篇主要講Spring Cloud Config,參考內容如下:

實現簡單的配置中心

配置文件就在Spring官方提供的配置倉庫:https://github.com/spring-cloud-samples/config-repo

1 創建配置中心服務端

完整代碼參考:https://github.com/sxpujs/spring-cloud-examples/tree/master/config/config-server

1 新建Spring Boot項目,引入config-server

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

2 配置config相關的配置項

bootstrap.yml

spring:
  application:
    name: foo # 應用名
  profiles:
    active: dev,mysql

application.yml

server:
  port: 8888

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo
          basedir: target/config

3 Application啟動類,增加相關註解:

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

4 啟動服務,進行相關測試

curl localhost:8888/foo/development

{
   "name" : "foo",
   "profiles" : ["development"],
   "propertySources" : [
      {"name":"https://github.com/spring-cloud-samples/config-repo/foo-development.properties",
       "source":{"bar":"spam",
                 "foo":"from foo development"}},
      {"name":"https://github.com/spring-cloud-samples/config-repo/foo.properties", 
       "source":{"foo":"from foo props",
                 "democonfigclient.message":"hello spring io"}},
      {"name":"https://github.com/spring-cloud-samples/config-repo/application.yml (document #0)", 
       "source":{"info.url" : "https://github.com/spring-cloud-samples",
                 "info.description":"Spring Cloud Samples",
                 "foo":"baz",
                 "eureka.client.serviceUrl.defaultZone":"http://localhost:8761/eureka/"}}
   ]
}


# 訪問不存在的profile(mysql)
curl http://localhost:8888/foo/mysql
{
  "name":"foo",
  "profiles":["mysql"],
  "propertySources":[
    {"name":"https://github.com/spring-cloud-samples/config-repo/foo.properties",
     "source":{"foo":"from foo props",
               "democonfigclient.message":"hello spring io"}},
    {"name":"https://github.com/spring-cloud-samples/config-repo/application.yml (document #0)",
     "source":{"info.description":"Spring Cloud Samples",
               "info.url":"https://github.com/spring-cloud-samples",
               "eureka.client.serviceUrl.defaultZone":"http://localhost:8761/eureka/",
               "foo":"baz"}}
  ]
}


curl http://localhost:8888/foo-dev.yml  # 從結果來看,包含了

bar: spam
democonfigclient:
  message: hello from dev profile
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
foo: from foo development
info:
  description: Spring Cloud Samples
  url: https://github.com/spring-cloud-samples
my:
  prop: from application-dev.yml

2 創建配置中心客戶端,使用配置

完整代碼參考:https://github.com/sxpujs/spring-cloud-examples/tree/master/config/config-client

1 引用相關的maven依賴。

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2 配置文件

bootstrap.yml

spring:
  profiles:
    active: dev
  cloud:
    config:
      uri: http://localhost:8888
  application:
    name: myapp

application.yml

management:
  endpoint:
    shutdown:
      enabled: false
  endpoints:
    web:
      exposure:
        include: "*"   # * 在yaml 文件屬於關鍵字,所以需要加引號

3 Application啟動類

@SpringBootApplication
@RestController
public class ConfigClientApplication {

    @Value("${foo}")
    private String foo;

    @Value("${my.prop}")
    private String myProp;

    @RequestMapping("/")
    public String home() {
        return "Hello World!" + myProp + "," + foo;
    }

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

4 驗證數據

訪問env埠,註意:從Spring Boot 2.x開始,不能直接訪問 http://localhost:8080/env,需要添加actuator。

curl localhost:8080    # 可以看出my.prop這個屬於是來自application-dev.yml,foo來自application.yml

Hello World!from application-dev.yml,baz


curl localhost:8080/actuator/env|json_pp

{
   "activeProfiles" : [],
   "propertySources" : [
      {"name":"server.ports","properties":{"local.server.port":{"value":8080}}},
      {
         "name" : "bootstrapProperties-https://github.com/spring-cloud-samples/config-repo/application.yml (document #0)",
         "properties" : {
            "info.url" : {"value" : "https://github.com/spring-cloud-samples"},
            "eureka.client.serviceUrl.defaultZone" : {"value" : "http://localhost:8761/eureka/"},
            "foo" : {"value" : "baz"},
            "info.description" : {"value" : "Spring Cloud Samples"}
         }
      }
   ]
}

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 基於JSP+Servlet開發旅游(景點賓館)系統(前臺+後臺): 開發環境: Windows操作系統開發工具: MyEclipse+Jdk+Tomcat+MYSQL資料庫運行效果圖 源碼及原文鏈接:https://javadao.xyz/forum.php?mod=viewthread&tid=6 ...
  • 匿名(lambda)函數: 作用:創始一個匿名函數對象,同 def 類似,但不提供函數名,只是一個表達式,lambda比函數簡單且可以隨時創建和銷毀,有利於減少程式的偶合度。lambda的主體是一個表達式,而不是一個代碼塊。僅僅能在lambda表達式中封裝有限的邏輯進去。lambda 函數擁有自己的 ...
  • 線程的五種狀態 線程從創建到銷毀一般分為五種狀態,如下圖: 1) 新建 當用new關鍵字創建一個線程時,就是新建狀態。 2) 就緒 調用了 start 方法之後,線程就進入了就緒階段。此時,線程不會立即執行run方法,需要等待獲取CPU資源。 3) 運行 當線程獲得CPU時間片後,就會進入運行狀態, ...
  • 題目1 求平均分 輸入:兩行 第一行是分數個數n 第二行是這n個分數,以空格隔開 輸出: 去掉最高分與最低分後的平均成績 #include<stdio.h> float buf[1001]; void fun(int n){ for(int i=0;i<n;i++){ for(int j=0;j<n ...
  • #include <iostream> //#include(預處理指令) iostream(所嵌入的頭文件(cout,<<等操作的有關信息就是在該文件中聲明的)) using namespace std; //針對命名空間的指令 int main(){ //int(返回值類型) main(主函數名 ...
  • 有監督學習 常用分類演算法 KNN:K近鄰分類器。通過計算待分類數據點,與已知數據中所有點的距離,取距離最小的前K個點,根據"少數服從多數"的原則,將這個數據點劃分為出現次數最多的那個類別。 在sklearn中,使用sklearn.neighbors.KNeighborsClassifier創建K鄰近 ...
  • 1、格式 符號為大括弧 集合沒有順序,也不支持下標操作 集合沒有重覆的數據 2、定義的類型 #有數據 s1 = {1, 3, 4} # {1,3,4} s2 = set('asdadsdas') # {'a','s','d'} 集合沒有重覆數據,也沒有順序 # 空集合 s3 = {} # dict ...
  • 1、格式 符號為大括弧 數據為鍵值對形式出現(字典數據與數據順序沒有關係,即字典不支持下標) 各個鍵值對之間逗號隔開 2、定義的類型 # 有數據 dict1 = {'name': '小明', 'sex': '男'} # 空字典 dict2 = {} dict3 =dict() # 函數定義 3、常用 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...