一、Eureka(服務注冊與發現)
1、Eureka采用了C/S的設計架構。Eureka Server作為服務注冊工程的伺服器,它是服務注冊中心。而系統中的其他微服務,使用Eureka的用戶端連接配接到Eureka Server并維持心跳連結。這樣系統的維護人員就可以通過Eureka Server來監控系統中各個微服務是否正常運作。SpringCloud的一些其他子產品(比如Zuul)就可以通過Eureka Server來發現系統中的其他微服務,并執行相關的邏輯。
Eureka包含兩個元件:Eureka Server和Eureka Client
Eureka Server提供服務注冊服務
各個節點啟動後,會在Eureka Server中進行注冊,這樣Eureka Server中的服務系統資料庫中将會存儲所有可用服務節點的資訊,服務節點的資訊可以在界面中看到。
Eureka Client是一個Java用戶端,用于簡化Eureka Server的互動,用戶端同時也具備一個内置的、使用輪詢(round-robin)負載算法的負載均衡器。在應用啟動後,将會向Eureka Server發送心跳(預設周期為30秒)。如果Eureka Server在多個心跳周期内沒有接收到某個節點的心跳,Eurkea Server将會從服務系統資料庫中把這個服務節點移除(預設90秒)。
二、Ribbon(負載均衡)
1、SpringCloud Ribbon是基于Netflix Ribbon實作的一套用戶端負載均衡的工具。
2、Ribbon是Netflix釋出的開源項目,主要功能是提供用戶端的軟體負載均衡算法,将Netflix的中間層服務連接配接在一起。Ribbon用戶端元件提供一系列完善的配置項,如:連接配接逾時、重試等。簡單的說,就是在配置檔案中列出Load Balancer(簡稱LB)後面所有的機器,Ribbon會自動的幫助你基于某種規則(如簡單輪詢,随機連接配接等)去連接配接這些機器。我們很容易使用Ribbon實作自定義的負載均衡算法。SpringCloud的負載均衡算法可以自定義
3、LB:即負載均衡,分為集中式LB和程序内LB
(1)集中式LB:即在服務的消費方和提供方之間使用獨立的LB設施(可以是硬體,如F5(超級貴),也可以是軟體,如nginx),由改設施負責把通路請求通過某種政策轉發至服務的提供方,偏向于硬體
(2)程序内LB:将LB邏輯內建到消費方,消費從服務注冊中心獲知有哪些位址可用,然後自己再從這些位址中選擇出一個合适的伺服器,偏向于軟體。
Ribbon就屬于程序内LB,它隻是一個類庫,內建于消費方程序,消費方通過他來擷取到服務提供方的位址。
項目示範
1、首先導入pom檔案(用戶端的子工程)
<!-- Ribbon相關三個 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
2、修改application配置檔案
eureka:
client:
register-with-eureka: false //自己不注冊
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
3、在ConfigBean添加新注解@LoadBalanced,獲得Rest時加入Ribbon的配置
@Configuration
public class ConfigBean //boot -->spring applicationContext.xml --- @Configuration配置 ConfigBean = applicationContext.xml
{
@Bean
@LoadBalanced//Spring Cloud Ribbon是基于Netflix Ribbon實作的一套用戶端 負載均衡的工具。
public RestTemplate getRestTemplate()
{
return new RestTemplate();
}
@Bean
public IRule myRule()
{
//return new RoundRobinRule();
//return new RandomRule();//達到的目的,用我們重新選擇的随機算法替代預設的輪詢。
return new RetryRule();
}
}
4、在啟動類加上@EnableEurekaClient
5、修改用戶端類的微服務名字
private static final String REST_URL_PREFIX = "http://MICROSERVICECLOUD-DEPT";
負載均衡示範(Ribbon,預設輪詢算法)
1、建立餘下的子工程
2、修改配置檔案,以8001為例,這裡需要改端口、資料庫名、Eureka别名
server:
port: 8001
mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置檔案所在路徑
type-aliases-package: com.atguigu.springcloud.entities # 所有Entity别名類所在包
mapper-locations:
- classpath:mybatis/mapper/**/*.xml # mapper映射檔案
spring:
application:
name: microservicecloud-dept
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 目前資料源操作類型
driver-class-name: org.gjt.mm.mysql.Driver # mysql驅動包
url: jdbc:mysql://localhost:3306/cloudDB01 # 資料庫名稱
username: root
password: root
dbcp2:
min-idle: 5 # 資料庫連接配接池的最小維持連接配接數
initial-size: 5 # 初始化連接配接數
max-total: 5 # 最大連接配接數
max-wait-millis: 200 # 等待連接配接擷取的最大逾時時間
eureka:
client: #用戶端注冊進eureka服務清單内
service-url:
#defaultZone: http://localhost:7001/eureka
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
#Eureka頁面微服務的名字(别名)
instance:
instance-id: microservicecloud-dept8001
prefer-ip-address: true #通路路徑可以顯示IP位址(内網壞境的ip位址)
#關于微服務的描述
info:
app.name: atguigu-microservicecloud
company.name: www.atguigu.com
#這裡不寫死,靈活調用
build.artifactId: $project.artifactId$
build.version: $project.version$
負載均衡算法(IRule),七個。
RetryRule():如果多個服務在運作中,突然有一個服務故障,那麼它将繼續執行,直到發現該服務确實不能通路了,那麼才跳過該服務,重新輪詢。
//其他算法,不定義該方法,預設輪詢算法
@Bean
public IRule myRule()
{
//return new RoundRobinRule(); //輪詢算法
//return new RandomRule();//達到的目的,用我們重新選擇的随機算法替代預設的輪詢。
return new RetryRule();
}
自定義IRule,這裡寫一個讓每個微服務都通路五次,在跳到下一個微服務的算法
1、在啟動類加上注解,注意:這個自定義配置類不能放在@ComponenScan所掃描的目前包下以及子包下,否則自定義的這個配置類就會被所有的Ribbon用戶端所共享,也就是說達不到我們想要的目的
@RibbonClient(name="MICROSERVICECLOUD-DEPT",configuration=MySelfRule.class)
2、建立自定義IRule配置類
@Configuration
public class MySelfRule
{
@Bean
public IRule myRule() {
//return new RandomRule();// Ribbon預設是輪詢,我自定義為随機
//return new RoundRobinRule();// Ribbon預設是輪詢,我自定義為随機
return new RandomRule_ZY();// 我自定義為每台機器5次,這裡傳回我們自定義的IRule類
}
}
public class RandomRule_ZY extends AbstractLoadBalancerRule
{
// total = 0 // 當total==5以後,我們指針才能往下走,
// index = 0 // 目前對外提供服務的伺服器位址,
// total需要重新置為零,但是已經達到過一個5次,我們的index = 1
// 分析:我們5次,但是微服務隻有8001 8002 8003 三台,OK?
//
private int total = 0; // 總共被調用的次數,目前要求每台被調用5次
private int currentIndex = 0; // 目前提供服務的機器号
public Server choose(ILoadBalancer lb, Object key) {
if (lb == null) {
return null;
}
Server server = null;
while (server == null) {
if (Thread.interrupted()) {
return null;
}
List<Server> upList = lb.getReachableServers();
List<Server> allList = lb.getAllServers();
int serverCount = allList.size();
if (serverCount == 0) {
/*
* No servers. End regardless of pass, because subsequent passes only get more
* restrictive.
*/
return null;
}
// int index = rand.nextInt(serverCount);// java.util.Random().nextInt(3);
// server = upList.get(index);
// private int total = 0; // 總共被調用的次數,目前要求每台被調用5次
// private int currentIndex = 0; // 目前提供服務的機器号
if(total < 5) {
server = upList.get(currentIndex);
total++;
}else {
total = 0;
currentIndex++;
if(currentIndex >= upList.size()) {
currentIndex = 0;
}
}
if (server == null) {
/*
* The only time this should happen is if the server list were somehow trimmed.
* This is a transient condition. Retry after yielding.
*/
Thread.yield();
continue;
}
if (server.isAlive()) {
return (server);
}
// Shouldn't actually happen.. but must be transient or a bug.
server = null;
Thread.yield();
}
return server;
}
@Override
public Server choose(Object key)
{
return choose(getLoadBalancer(), key);
}
@Override
public void initWithNiwsConfig(IClientConfig clientConfig)
{
// TODO Auto-generated method stub
}
}
三、Feign(負載均衡)
1、Feign是一個聲明式WebService用戶端。使用Feign能讓編寫Web Service用戶端更加簡單,它的使用方法是定義一個接口,然後在上面添加注解,同時也支援JAX-RS标準的注解。Feign也支援可拔插式的編碼器和解碼器。SpringCloud對Feign進行了封裝,使其支援SpringMVC标準注解和HttpMessageConverters。Feign可以與Eureka和Ribbon組護使用以支援負載均衡。
Feign是一個聲明式的Web服務用戶端,使得編寫Web服務用戶端變得非常容易,隻需要建立一個接口,然後在上面添加注解即可。
2、Feign內建了Ribbon,利用Ribbon維護了MicroServiceCloud-Dept的服務清單資訊,并且通過輪詢實作了用戶端的負載均衡。而與Ribbon不同的是,通過Feign隻需要定義服務綁定接口且以聲明式的方法,就可以實作服務調用
項目示範
1、建立子工程,添加Feign相關依賴,這裡的子工程是複制上一個80的子工程的,所有有些檔案和類都是相同的
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
2、修改api公共工程,pom檔案添加Feign依賴,建立一個service接口并新增注解@FeignClient
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
@FeignClient(value = "MICROSERVICECLOUD-DEPT")
public interface DeptClientService {
@RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
public Dept get(@PathVariable("id") long id);
@RequestMapping(value = "/dept/list", method = RequestMethod.GET)
public List<Dept> list();
@RequestMapping(value = "/dept/add", method = RequestMethod.POST)
public boolean add(Dept dept);
}
3、再修改feign工程的controller,并将剛剛在api工程編寫的接口注入到controller中
@RestController
public class DeptController_Consumer {
@Autowired
private DeptClientService service;
@RequestMapping(value = "/consumer/dept/get/{id}")
public Dept get(@PathVariable("id") Long id)
{
return this.service.get(id);
}
@RequestMapping(value = "/consumer/dept/list")
public List<Dept> list() {
return this.service.list();
}
@RequestMapping(value = "/consumer/dept/add")
public Object add(Dept dept) {
return this.service.add(dept);
}
}
4、修改feign啟動類
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages= {"com.atguigu.springcloud"})
@ComponentScan("com.atguigu.springcloud")
public class DeptConsumer80_Feign_App{
public static void main(String[] args) {
SpringApplication.run(DeptConsumer80_Feign_App.class, args);
}
}
Feign和Ribbon都可以實作負載均衡
Feign通過接口的方法調用Rest服務(之前是Ribbon+RestTemplate),通過Feign直接找到服務接口,由于在進行服務調用的時候融合了Ribbon技術,是以也支援負載均衡。
四、Hystrix(熔斷器)
服務雪崩
1、多個微服務之間調用的時候,假設微服務A調用微服務B和微服務C,微服務B和微服務C又調用其他的微服務,這就是所謂的“扇出”。如果扇出的鍊路上某個微服務的調用響應時間過長或者不可用,對微服務A的調用就會占用越來越多的系統資源,進而引起系統崩潰,就是所謂的“雪崩效應”。
2、Hystrix是一個處理分布式系統的延遲和容錯的開源庫,在分布式系統裡,許多依賴不可避免的會調用失敗,比如逾時、異常等,Hystrix能保證在一個依賴出問題的情況下,不會導緻整體服務失敗,避免級聯故障,以提高分布式系統的彈性。
“斷路器”本身是一種開關裝置,當某個微服務單元發生故障之後,通過斷路器的故障監控(類似于熔斷保險絲),向調用方傳回一個符合預期的響應,而不是長時間的等待或抛出無法處理的異常,這樣就保證了服務調用方線程不會被長時間、不必要的占用,進而避免了故障在分布式系統中的蔓延,乃至雪崩。
服務熔斷
一般是某個服務故障或者異常引起,類似于現實世界中的“保險絲”,當某個異常條件被觸發,直接熔斷整個服務,而不是一直等到此服務逾時。
熔斷機制是應對雪崩效應的一種微服務鍊路保護機制,注解:@HystrixCommand
項目示範(參考8001微服務)
1、建立子工程
2、修改pom檔案,引入Hystrix依賴,修改application配置檔案的自定義id
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
3、修改controller,@HystrixCommand注解的意思就是如果微服務不能調用get方法,那就就調用下面的processHystrix_Get()方法,如果能調用get()方法,就傳回dept。說白了就是能成功調用,那麼就傳回一個異常資料,不能調用,也要傳回一個不能調用的資料。
@RestController
public class DeptController
{
@Autowired
private DeptService service = null;
@RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
//一旦調用服務方法失敗并抛出了錯誤資訊後,會自動調用@HystrixCommand标注好的fallbackMethod調用類中的指定方法
@HystrixCommand(fallbackMethod = "processHystrix_Get")
public Dept get(@PathVariable("id") Long id) {
Dept dept = this.service.get(id);
if (null == dept) {
throw new RuntimeException("該ID:" + id + "沒有沒有對應的資訊");
}
return dept;
}
public Dept processHystrix_Get(@PathVariable("id") Long id) {
return new Dept().setDeptno(id).setDname("該ID:" + id + "沒有沒有對應的資訊,[email protected]")
.setDb_source("no this database in MySQL");
}
}
4、在啟動類加上新注解,@EnableCircuitBreaker
@SpringBootApplication
@EnableEurekaClient //本服務啟動後會自動注冊進eureka服務中
@EnableDiscoveryClient //服務發現
@EnableCircuitBreaker//對hystrixR熔斷機制的支援
public class DeptProvider8001_Hystrix_App {
public static void main(String[] args) {
SpringApplication.run(DeptProvider8001_Hystrix_App.class, args);
}
}
服務降級
整體資源快不夠了,忍痛将某些服務先關掉,待度過難關,在開啟回來。
服務降級處理實在用戶端實作完成的,與服務端沒有關系
先前那樣寫,會導緻,有一個業務方法,那麼就要寫一個對應處理的fallback方法,是以,下面對這個問題進行改善。
項目示範
1、修改microservicecloud-api工程,根據已經有的DeptClientService接口,建立一個實作了FallbackFactory接口的類DeptClientServiceFallbackFactory,該類上面要添加@Component
@Component // 不要忘記添加,不要忘記添加
public class DeptClientServiceFallbackFactory implements FallbackFactory<DeptClientService> {
@Override
public DeptClientService create(Throwable throwable)
{
return new DeptClientService() {
@Override
public Dept get(long id)
{
return new Dept().setDeptno(id).setDname("該ID:" + id + "沒有沒有對應的資訊,Consumer用戶端提供的降級資訊,此刻服務Provider已經關閉")
.setDb_source("no this database in MySQL");
}
@Override
public List<Dept> list()
{
return null;
}
@Override
public boolean add(Dept dept)
{
return false;
}
};
}
}
2、在api公共工程的接口中添加fallbackFactory屬性
@FeignClient(value = "MICROSERVICECLOUD-DEPT",fallbackFactory=DeptClientServiceFallbackFactory.class)
3、在feign工程的application配置檔案添加相應的配置,因為這種處理是在@FeignClient中的,是以需要在feign工程的配置檔案中配置
feign:
hystrix:
enabled: true
實作這種方式是為了,主業務方法、異常處理、熔斷處理的解耦,避免方法膨脹
服務監控HystrixDashboard
項目示範
1、建立一個HystrixDashboard工程,修改pom和application配置檔案
pom:
<!-- hystrix和 hystrix-dashboard相關 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
yml:
server:
port: 9001
2、啟動類添加新注解
@SpringBootApplication
@EnableHystrixDashboard//監控注解
public class DeptConsumer_DashBoard_App
{
public static void main(String[] args)
{
SpringApplication.run(DeptConsumer_DashBoard_App.class, args);
}
}
3、所有的服務中都要有(8001、8002、8003)
<!-- actuator監控資訊完善 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
4、通路服務監控頁面出現下圖所示,則為監控成功
(1)Delay:該參數用來控制伺服器上輪詢監控資訊的延遲時間,預設為2000毫秒,可以通過配置該屬性來降低用戶端的網絡和CPU消耗。
(2)Title:該參數對應了頭部标題Hystrix Stream之後的内容,預設會使用具體監控執行個體的URL,可以通過配置該資訊來展示更合适的标題。
5、資料展示:通路某個服務監控的字尾名為hystrix.stream,例如:http://8001/hystrix.stream
6、圖形化展示,在文本框填寫需要監控服務的位址
7、怎麼看?注意:需要多次重新整理監控的微服務
7色、一圈、一線
一圈:實心圓,他通過顔色的變化代表了執行個體的健康程度,從綠色<黃色<橙色<紅色遞減。該實心圓除了顔色的變化之外,它的大小也會根據執行個體的請求流量發生變化,流量越大實心圓越大。是以通過實心圓的展示,就可以在大量的執行個體中快速的發現故障執行個體的高壓力執行個體。
五、Zuul(路由網關)
1、Zuul包含了對請求的路由和過濾兩個最主要的功能:
(1)其中路由功能負責将外部請求轉發到具體的微服務執行個體上,是實作外部通路統一入口的基礎,而過濾器功能則負責對請求的處理過程進行幹預,是實作請求校驗、服務聚合等功能的基礎。
(2)Zuul和Eureka進行整合,将Zuul自身注冊為Eureka服務治理下的應用,同時從Eureka中獲得其他微服務的消息,也即以後的通路微服務都是通過Zuul跳轉後獲得。
注意:Zuul服務最終還是會注冊Erueka
提供=代理+路由+過濾
項目示範(基本路由配置)
1、建立工程,導入Zuul相關依賴
<!-- zuul路由網關 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
2、修改application配置檔案,修改端口、微服務名稱、别名ip(instance-id)
server:
port: 9527
spring:
application:
name: microservicecloud-zuul-gateway
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
instance:
instance-id: gateway-9527.com
prefer-ip-address: true
info:
app.name: atguigu-microcloud
company.name: www.atguigu.com
build.artifactId: $project.artifactId$
build.version: $project.version$
3、域名映射,hosts檔案修改,這裡我都注釋了
4、啟動類添加新注解@EnableZuulProxy
@SpringBootApplication
@EnableZuulProxy
public class Zuul_9527_StartSpringCloudApp {
public static void main(String[] args)
{
SpringApplication.run(Zuul_9527_StartSpringCloudApp.class, args);
}
}
5、啟動成功的Eureka頁面,會多出一個微服務
6、如何去進行業務通路(通路規則),myzuul.com是域名映射的名稱
例如:http://myzuul.com:9527/microservicecloud-dept/dept/get/2
7、設定通路映射規則
zuul:
#ignored-services: microservicecloud-dept
prefix: /atguigu #設定公共字首
ignored-services: "*" #過濾所有的真實路徑,隻能使用自己固定的路徑(mydept)
routes:
mydept.serviceId: microservicecloud-dept
mydept.path: /mydept/**
通路路徑為:myzuul.com:9527/atguigu/mydept/dept/get/3
六、SpringCloud Config(分布式配置中心)
1、SpringCloud Config為微服務架構中的微服務提供集中化的外部配置支援,配置伺服器為各個不同微服務應用的所有環境提供了一個中心化的外部配置。
2、SpringCloud Config分為服務端和用戶端兩部分
(1)服務端也稱為分布式配置中心,它是一個獨立的微服務應用,用來連接配接配置伺服器并為用戶端提供擷取配置資訊,加密/解密資訊等通路接口。
(2)用戶端則是通過指定的配置中心來管理應用資源,以及與業務相關的配置内容,并在啟動的時候從配置中心擷取和加載配置資訊,配置伺服器預設采用git來存儲配置資訊,這樣就有助于對環境配置進行版本管理,并且可以通過git用戶端工具來友善的管理和通路配置内容。
SpringCloud Config服務端配置
1、用自己的GitHub賬号建立一個Config檔案,點選creat,複制SSH協定連結
2、在本地建立一個Config檔案夾,然後執行Git複制指令,裡面建立一個application.yml檔案(必須UTF-8形式儲存),另存為編碼格式就行
3、Git指令進行送出
4、建立maven子工程,config工程,添加配置依賴
<!-- springCloud Config -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- 避免Config的Git插件報錯:org/eclipse/jgit/api/TransportConfigCallback -->
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>4.10.0.201712302008-r</version>
</dependency>
5、修改application配置檔案,填寫git位址
server:
port: 3344
spring:
application:
name: microservicecloud-config
cloud:
config:
server:
git:
uri: [email protected]:zzyybs/microservicecloud-config.git #GitHub上面的git倉庫名字
6、啟動類添加新注解@EnableConfigServer
@SpringBootApplication
@EnableConfigServer
public class Config_3344_StartSpringCloudApp
{
public static void main(String[] args)
{
SpringApplication.run(Config_3344_StartSpringCloudApp.class, args);
}
}
7、windows下修改hosts檔案
增加映射:127.0.0.1 config-3344.com
8、通路規則,application後面加個“-”就行:config-3344.com:3344/application-dev.yml
SpringCloud Config用戶端配置
1、在原來的本地config檔案夾中,建立一個新的config-client.yml配置檔案,裡面寫
2、通過git指令送出
3、建立maven子工程,修改pom檔案
<!-- SpringCloud Config用戶端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
4、resources包下建立bootstrap.yml和application.yml檔案,注意看代碼注釋
application.yml是使用者級的資源配置項
bootstrap.yml是系統級的,優先級更高,負責從外部加載配置資訊
bootstrap.yml檔案内容
spring:
cloud:
config:
name: microservicecloud-config-client #需要從github上讀取的資源名稱,注意沒有yml字尾名
profile: test #本次通路的配置項
label: master
uri: http://config-3344.com:3344 #本微服務啟動後先去找3344号服務,通過SpringCloudConfig擷取GitHub的服務位址
application.yml檔案内容
spring:
application:
name: microservicecloud-config-client
5、做域名通路映射(hosts檔案)
6、建立rest包和ConfigClientRest類
@RestController
public class ConfigClientRest
{
@Value("${spring.application.name}")
private String applicationName;
@Value("${eureka.client.service-url.defaultZone}")
private String eurekaServers;
@Value("${server.port}")
private String port;
@RequestMapping("/config")
public String getConfig()
{
String str = "applicationName: " + applicationName + "\t eurekaServers:" + eurekaServers + "\t port: " + port;
System.out.println("******str: " + str);
return "applicationName: " + applicationName + "\t eurekaServers:" + eurekaServers + "\t port: " + port;
}
}
7、建立啟動類
@SpringBootApplication
public class ConfigClient_3355_StartSpringCloudApp
{
public static void main(String[] args)
{
SpringApplication.run(ConfigClient_3355_StartSpringCloudApp.class, args);
}
}
8、啟動服務,先啟動3344在啟動3355
SpringCloud Config配置實戰
1、本地config檔案夾,建立eureka-client.yml檔案和dept-client.yml檔案
eureka-client.yml内容
dept-client.yml内容
2、git指令送出檔案