Config Server
使用 IntelliJ Idea 創(chuàng)建一個(gè)Config Server項(xiàng)目
config也是一個(gè)微服務(wù),所以也需要注冊(cè)
在Dependencies選擇時(shí)選擇:
- Cloud Discovery - Eureka Discovery ;
- Cloud Config - Config Server;
都是老套路了磕谅,啟動(dòng)類上增加注解
package com.calligraphy.config;
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
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
你現(xiàn)在啟動(dòng)就大錯(cuò)特錯(cuò)了,還沒配置呢雾棺,先將git安裝好膊夹,安裝教程就不說了,開始寫我們的配置文件
我們需要用到遠(yuǎn)程倉庫捌浩,我使用的是碼云(不是馬爸爸)放刨,注冊(cè)完成后創(chuàng)建一個(gè)倉庫,這是我的倉庫
主頁.png
打開倉庫創(chuàng)建一個(gè)文件
新建文件.png
文件.png
將你的公共配置給放上去
配置.png
注意格式尸饺,我使用的是.yml文件
遠(yuǎn)程倉庫的設(shè)置就到此為止了...
接下來回到我們的項(xiàng)目
在yml上填寫你的遠(yuǎn)程倉庫的用戶名和密碼
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: config
cloud:
config:
server:
git:
uri: https://gitee.com/hystyle/calligraphy-config
username: xxxx
password: xxxx
basedir: D:\calligraphy\config\basedir //下載到此文件夾进统,不要下載到自己的項(xiàng)目目錄
以上完成后,啟動(dòng)項(xiàng)目侵佃,打開http://localhost:8090/order-a.yml麻昼,至于為什么加 -a ,自己去了解一下吧馋辈,總之你不加就報(bào)404抚芦,成功之后,Config Server就完成了
接下來看看如何使用?
Config Client
首先添加依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
修改yml叉抡,名字也要修改 bootstrap.yml
spring:
application:
name: order
cloud:
config:
discovery:
enabled: true
service-id: CONFIG
profile: dev //這里怎么描述..我有個(gè)文件名是:order-dev.yml 尔崔,
//order就對(duì)應(yīng)了本項(xiàng)目的name,所以這里不用寫褥民,只用寫后面的dev...大概是這樣
啟動(dòng)項(xiàng)目季春,正常啟動(dòng)...Spring Cloud Bus ,暫時(shí)有坑消返,我暫時(shí)不用载弄,所以不寫...以后補(bǔ)
(自動(dòng)更新需要用到)
簡(jiǎn)書著作權(quán)歸作者所有,任何形式的轉(zhuǎn)載都請(qǐng)聯(lián)系作者獲得授權(quán)并注明出處撵颊。