Spring Cloud Config 遠程倉庫

遠程倉庫受網(wǎng)絡(luò)影響,可以從本地(配置中心)
注冊中心與配置中心合并在一起,可能導(dǎo)致頁面沒有樣式,所以分開來。

添加依賴

<?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">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>org.baozi</groupId>
    <artifactId>cloud-config</artifactId>
    <version>1.0</version>
    <packaging>pom</packaging>

    <modules>
        <module>config-registration</module>
        <module>config-server</module>
        <module>config-clients</module>
    </modules>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <!--
            config client 可以多項目通用倔丈,放在父依賴中
            config server 必須放在配置中心的依賴中,否則會導(dǎo)致有些屬性不能注入
         -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
<?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">
   <modelVersion>4.0.0</modelVersion>
   <parent>
      <groupId>org.baozi</groupId>
      <artifactId>cloud-config</artifactId>
      <version>1.0</version>
      <relativePath>..</relativePath>
   </parent>

   <artifactId>config-registration</artifactId>

</project>
<?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">
   <modelVersion>4.0.0</modelVersion>
   <parent>
      <groupId>org.baozi</groupId>
      <artifactId>cloud-config</artifactId>
      <version>1.0</version>
      <relativePath>..</relativePath>
   </parent>

   <artifactId>config-server</artifactId>

   <dependencies>
      <!-- 只能在配置中心添加config-server -->
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-config-server</artifactId>
      </dependency>
   </dependencies>

</project>
<?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">
   <modelVersion>4.0.0</modelVersion>
   <parent>
      <groupId>org.baozi</groupId>
      <artifactId>cloud-config</artifactId>
      <version>1.0</version>
      <relativePath>..</relativePath>
   </parent>

   <artifactId>config-clients</artifactId>

</project>

注冊中心

spring.application.name=config-registration
server.port=9000

eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class ConfigRegistrationApplication {

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

}

配置中心

spring.application.name=config-server
server.port=9500

eureka.client.service-url.defaultZone=http://localhost:9000/eureka
# 連接到遠程倉庫
spring.cloud.config.server.git.uri=https://gitee.com/baozi-baozi/cloud-config.git
1.png
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer  // 開啟配置中心
//@EnableDiscoveryClient // 這個可以省略
public class ConfigServerApplication {

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

}
訪問規(guī)則:

1.  application:上面文件中的config-clients
2.  profile:上面文件中的dev状蜗,如果沒有就是default
3.  labei:就是分支需五,不寫默認為master
4.  /application/profile[/label]
http://localhost:9500/config-clients/default
http://localhost:9500/config-clients/default/master
http://localhost:9500/config-clients/dev
http://localhost:9500/config-clients/dev/master
5.  /application-profile.[yml | properties | json ]
http://localhost:9500/config-clients-default.properties
http://localhost:9500/config-clients-default.yml
http://localhost:9500/config-clients-default.json
http://localhost:9500/config-clients-dev.properties
http://localhost:9500/config-clients-dev.yml
http://localhost:9500/config-clients-dev.json
6.  /label/application-profile.[yml | properties | json ]
在前一個基礎(chǔ)上加上分支。

配置客戶端

bootstrap.properties

# 配置文件對應(yīng)的application部分, 同時也是spring.application.name的名字
spring.application.name=config-clients
server.port=10000

# 可以寫在配置中心的配置文件中
eureka.client.service-url.defaultZone=http://localhost:9000/eureka

# config-server的url, 可以寫多個轧坎。
# spring.cloud.config.uri=http://localhost:9500/
# 可以用服務(wù)發(fā)現(xiàn)的方式代替寫多個uri
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=config-server

# profile部分
#spring.cloud.config.profile=default
spring.cloud.config.profile=dev

# label部分
spring.cloud.config.label=master
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class ConfigClientsApplication {

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

}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.MessageFormat;

@RestController
public class ClientsController {

    // ~方式一: 如果遠程倉庫延遲比較高宏邮,運行時會報錯,采用本地文件比較好
    // -------------------------------

    @Value("${user.name}")
    private String name;

    @Value("${user.age}")
    private String age;

    @GetMapping("/m1")
    public String m1() {
        return MessageFormat.format("m1  name: {0}, age: {1}", name, age);
    }

    // ~方式二
    // -------------------------------

    @Autowired
    private Environment environment;

    @GetMapping("/m2")
    public String m2() {
        String name = environment.getProperty("user.name", "undefined");
        String age = environment.getProperty("user.age", "-1");
        return MessageFormat.format("m2  name: {0}, age: {1}", name, age);
    }


}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末眶根,一起剝皮案震驚了整個濱河市蜀铲,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌属百,老刑警劉巖记劝,帶你破解...
    沈念sama閱讀 222,681評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異族扰,居然都是意外死亡厌丑,警方通過查閱死者的電腦和手機定欧,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,205評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來怒竿,“玉大人砍鸠,你說我怎么就攤上這事「郏” “怎么了爷辱?”我有些...
    開封第一講書人閱讀 169,421評論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長朦肘。 經(jīng)常有香客問我饭弓,道長,這世上最難降的妖魔是什么媒抠? 我笑而不...
    開封第一講書人閱讀 60,114評論 1 300
  • 正文 為了忘掉前任弟断,我火速辦了婚禮,結(jié)果婚禮上趴生,老公的妹妹穿的比我還像新娘阀趴。我一直安慰自己,他們只是感情好苍匆,可當(dāng)我...
    茶點故事閱讀 69,116評論 6 398
  • 文/花漫 我一把揭開白布刘急。 她就那樣靜靜地躺著,像睡著了一般锉桑。 火紅的嫁衣襯著肌膚如雪排霉。 梳的紋絲不亂的頭發(fā)上窍株,一...
    開封第一講書人閱讀 52,713評論 1 312
  • 那天民轴,我揣著相機與錄音,去河邊找鬼球订。 笑死后裸,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的冒滩。 我是一名探鬼主播微驶,決...
    沈念sama閱讀 41,170評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼开睡!你這毒婦竟也來了因苹?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,116評論 0 277
  • 序言:老撾萬榮一對情侶失蹤篇恒,失蹤者是張志新(化名)和其女友劉穎扶檐,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體胁艰,經(jīng)...
    沈念sama閱讀 46,651評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡款筑,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,714評論 3 342
  • 正文 我和宋清朗相戀三年智蝠,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片奈梳。...
    茶點故事閱讀 40,865評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡杈湾,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出攘须,到底是詐尸還是另有隱情漆撞,我是刑警寧澤,帶...
    沈念sama閱讀 36,527評論 5 351
  • 正文 年R本政府宣布于宙,位于F島的核電站叫挟,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏限煞。R本人自食惡果不足惜抹恳,卻給世界環(huán)境...
    茶點故事閱讀 42,211評論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望署驻。 院中可真熱鬧奋献,春花似錦、人聲如沸旺上。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,699評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽宣吱。三九已至窃这,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間征候,已是汗流浹背杭攻。 一陣腳步聲響...
    開封第一講書人閱讀 33,814評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留疤坝,地道東北人兆解。 一個月前我還...
    沈念sama閱讀 49,299評論 3 379
  • 正文 我出身青樓,卻偏偏與公主長得像跑揉,于是被迫代替她去往敵國和親锅睛。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,870評論 2 361

推薦閱讀更多精彩內(nèi)容