欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

spring-cloud入門之spring-cloud-config(配置中心)

 更新時間:2018年01月18日 16:41:04   作者:大黃蜂coder  
這篇文章主要介紹了spring-cloud入門之spring-cloud-config(配置中心),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

前言

在分布式系統(tǒng)中,由于服務(wù)數(shù)量巨多,為了方便服務(wù)配置文件統(tǒng)一管理,實時更新,所以需要分布式配置中心組件:spring-cloud-config ,它支持配置服務(wù)放在配置服務(wù)的內(nèi)存中(即本地),也支持放在遠程Git倉庫中。

本節(jié)主要演示怎么用Git倉庫作為配置源。

開源地址:https://github.com/bigbeef

創(chuàng)建配置項目

在github中創(chuàng)建一個項目,專門用來保存我們所有項目的配置文件,項目是我的項目結(jié)構(gòu)

配置項目地址:https://github.com/bigbeef/cppba-config

eureka-server.properties

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
spring.application.name=eureka-server
server.port=18761
eureka.instance.hostname=peer1
eureka.client.serviceUrl.defaultZone=http://peer1:18761/eureka/

創(chuàng)建spring-cloud-config-server項目

項目結(jié)構(gòu)如圖:


pom.xml核心代碼

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

SpringCloudConfigServerApplication.java

package com.cppba;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class SpringCloudConfigServerApplication {

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

application.properties

這個根據(jù)自己實際的git項目修改配置

server.port=8888

spring.application.name=config-server

spring.cloud.config.server.git.uri=https://github.com/bigbeef/cppba-config
spring.cloud.config.label=master
# spring.cloud.config.server.git.username=
# spring.cloud.config.server.git.password=

spring.cloud.config.server.git.searchPaths=\
 cppba-spring-cloud/*,\
 cppba-spring-cloud/eureka-client/*

spring.cloud.config.server.git.uri:配置git倉庫地址
spring.cloud.config.server.git.searchPaths:配置倉庫路徑,以逗號隔開
spring.cloud.config.label:配置倉庫的分支
spring.cloud.config.server.git.username:訪問git倉庫的用戶名
spring.cloud.config.server.git.password:訪問git倉庫的用戶密碼

啟動項目

訪問地址:http://127.0.0.1:8888

http請求地址和資源文件映射如下:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

根據(jù)我們自己的配置,我們可以這樣訪問:http://127.0.0.1:8888/eureka-server/default/master

application -> eureka-server (應(yīng)用名)
profile -> default (啟用的配置,通常是后綴,下面解釋)
label -> master (分支)

訪問到的結(jié)果就是:

profile比較重要,可以理解成讀取哪些配置文件,假如我不止一個配置文件,可能會有:

eureka-server.properties(這個是通用配置文件,默認(rèn)都會加載),
eureka-server-mysql.properties,
eureka-server-oracle.properties,
eureka-server-jpa.properties,
eureka-server-mysql.properties......

我們可能會選擇性的加載其中的部分properties配置文件,那我們可以這樣寫:http://127.0.0.1:8888/eureka-server/default,mysql,jpa/master

到此,我們的spring-cloud-config-server就簡單搭起來,后面的章節(jié)我會教大家怎么在項目中讀取配置

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論