Spring Cloud進(jìn)行Eureka ribbon hystrix集成

Eurek進(jìn)行服務(wù)的注冊(cè)與發(fā)現(xiàn)(請(qǐng)看之前的筆記[Spring Cloud Eureka搭建注冊(cè)中心])
ribbon進(jìn)行RestTemplate負(fù)載均衡策略(下期寫(xiě)ribbon實(shí)現(xiàn)負(fù)載均衡以及手寫(xiě)負(fù)責(zé)均衡)
hystrix 實(shí)現(xiàn)熔斷機(jī)制以及通過(guò)dashboard查看熔斷信息(有時(shí)間寫(xiě)hystrix dashboard詳解)

項(xiàng)目結(jié)構(gòu)如下(不包含Eureka服務(wù)注冊(cè)與發(fā)現(xiàn)),另外部署

image.png

spring-cloud-study-provider 作為服務(wù)提供者將服務(wù)注冊(cè)到Eureka集群
spring-cloud-study-api 作為項(xiàng)目api提供基礎(chǔ)類(lèi)庫(kù)支持
spring-cloud-study-consumer 作為服務(wù)消費(fèi)者從Eureka集群獲取提供者信息,并進(jìn)行消費(fèi),集成了Eureka,ribbon, hystrix, hystrix dashboard

Eureka主要實(shí)現(xiàn)服務(wù)的注冊(cè)與發(fā)現(xiàn)(請(qǐng)看之前的筆記[Spring Cloud Eureka搭建注冊(cè)中心]),這里不在重復(fù)

消費(fèi)端eureka配置
eureka:
  client:
    register-with-eureka: false
    fetch-registry: true
    service-url:
      defaultZone: http://eureka-server.com:7001/eureka/,http://eureka-client1.com:7002/eureka/,http://eureka-client2.com:7003/eureka/

服務(wù)提供方eureka配置

eureka:
  client:
    service-url:
      defaultZone: http://eureka-server.com:7001/eureka/,http://eureka-client1.com:7002/eureka/,http://eureka-client2.com:7003/eureka/
    register-with-eureka: true
    fetch-registry: false
  instance:
    instance-id: spring-cloud-study-provider # 調(diào)用服務(wù)時(shí)需要此名稱(全部大寫(xiě))
    prefer-ip-address: true

ribbon實(shí)現(xiàn)負(fù)載均衡,默認(rèn)采用:輪詢。

引用jar
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
            <version>1.3.1.RELEASE</version>
        </dependency>

在RestTemplat加入LoanBalance注釋即可

@Configuration
public class RestConfigBean {

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate()
    {
        return new RestTemplate();
    }
}

hystrix 實(shí)現(xiàn)熔斷機(jī)制以及通過(guò)dashboard進(jìn)行監(jiān)控

引入jar依賴
              <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
            <version>1.3.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
            <version>1.3.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.netflix.hystrix</groupId>
            <artifactId>hystrix-metrics-event-stream</artifactId>
            <version>1.5.12</version>
        </dependency>
        <dependency>
            <groupId>com.netflix.hystrix</groupId>
            <artifactId>hystrix-javanica</artifactId>
            <version>1.5.12</version>
        </dependency>

啟動(dòng)hystrix有及hystrix dashboard

@SpringBootApplication
@EnableDiscoveryClient #啟用eureak服務(wù)發(fā)現(xiàn)
@EnableHystrix # 啟用hystrix熔斷
@EnableHystrixDashboard # 啟用hystrix dashboard服務(wù)監(jiān)控
public class ConsumerApplication {

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

重要步驟

先啟動(dòng)eureka服務(wù)器,這邊啟動(dòng)三臺(tái),模擬集群, 訪問(wèn)

http://eureka-server.com:7001/
http://eureka-client1.com:7002/
http://eureka-client2.com:7003/

如果訪問(wèn)地址出現(xiàn)下圖,表示eureka啟動(dòng)成功
image.png
啟動(dòng)服務(wù)提供者,將服務(wù)注冊(cè)到eureka服務(wù)器
[http://eureka-server.com:7001/](http://eureka-server.com:7001/)
[http://eureka-client1.com:7002/](http://eureka-client1.com:7002/)
[http://eureka-client2.com:7003/](http://eureka-client2.com:7003/)
訪問(wèn)以上地址,出現(xiàn)下圖,表示服務(wù)提供者注冊(cè)服務(wù)到eureka集群成功
image.png
啟動(dòng)服務(wù)提供者,從eureka集群獲取服務(wù)提供者信息,并進(jìn)行服務(wù)消費(fèi),啟動(dòng)成功后,進(jìn)行測(cè)試
image.png

image.png

http://localhost:9001/dept/get/2 訪問(wèn)這個(gè)地址時(shí),出現(xiàn)RuntimeException異常,將進(jìn)行熔斷,將返回getIdError方法的內(nèi)容

image.png

查看熔斷信息(訪問(wèn)地址:http://localhost:9001/hystrix)

image.png

地址欄輸入:localhost:9001/hystrix.stream
title:隨便輸入
點(diǎn)擊 按鈕提交
訪問(wèn):http://localhost:9001/dept/get/2, 服務(wù)提供者控制臺(tái)將出現(xiàn)異常
查詢hystrix dashboard頁(yè)面,刷新

image.png
image.png

------------------------ 完畢 -------------------------

代碼已提交到碼云:https://gitee.com/liwuyin/SpringCloudStudy/tree/master/spring-cloud-study

如有疑問(wèn),請(qǐng)?zhí)砑観Q:2646409029, 備注:簡(jiǎn)書(shū),謝謝!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容