1. 程式人生 > >基於 Spring Cloud 的微服務架構實踐指南(下)

基於 Spring Cloud 的微服務架構實踐指南(下)

show me the code and talk to me,做的出來更要說的明白
本文原始碼,請點選 learnSpringCloud
我是布林bl,你的支援是我分享的動力!

一、引入

上回 基於 Spring Cloud 的微服務架構實踐指南(上) 介紹了 Spring Cloud 的常見元件,我們接著繼續進入 Spring Cloud 的實戰教程,擼起袖子,真槍實彈幹一場。在實戰演練中感受一下 Spring Cloud 的魅力所在。在教程中,我會繼續將 Spring Cloud 常見元件進行整合。整個過程就像搭積木一樣,一點一點地完成一個微服務工程的搭建。實戰演練是比較繁瑣的,但是隻要我們真正地去做了,就會收穫很多。

二、hystrix 元件( 服務熔斷 )

hystrix 元件主要作用是服務熔斷以及服務降級。可以在我們犯錯的時候,再給我們一次機會。他就像家裡的短路開關,對程式起到保護作用。另一方面其實覺得和 java的異常機制相似。當專案發生未知異常, hystrix 元件就會挺身而出,作為專案的貼身保鏢,為專案保駕護航。當然,你認我的程式碼沒有bug,那麼你可以把他放在一邊。另外hystrix 元件提供了一個監控功能,但是沒有圖形化,我們可以使用相關依賴引入影象化介面。

2.1 pom 檔案

我們引入 hystrix的依賴。

<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>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-actuator</artifactId>
</dependency>

2.2 yml 檔案

引入必要模組後,我們就要去配置 yml 檔案了。

server:
  port: 8010 # 埠
  
spring:
  application:
    name: microservicloud-hystrix # 給模組起一個名字
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka  # 註冊中心地址
  instance:
    instance-id: microservicloud-hystrix-8010
    prefer-ip-address: true

2.3 啟動類

@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker  //啟動斷路器
@EnableDiscoveryClient // 啟動影象化監控
@RestController
public class AppApllcation8005 {
    public static void main( String[] args ) {
        SpringApplication.run(AppApllcation8005.class, args);
    }
    @GetMapping("/hellohystrix")
    @HystrixCommand(fallbackMethod = "fallback") // 發生異常執行響應方法
    public String hystrix() {
        int i = 1 / 0;
        return "hellohystrix";
    }

    public String fallback() {
        return "出錯了";
    }

}

2.4 啟動效果

啟動註冊中以及 hystrix 元件專案。

訪問 http://localhost:8010/hellohystrix 介面:

訪問 http://localhost:8010/hystrix 介面:

三、zuul元件(服務閘道器)

zuul 元件主要是提供路由與過濾器功能。一般作為專案的大門守衛,對所有進入專案的介面進行檢查。就像地鐵的安保人員一樣,會對每一個進入地鐵的人員進行一一 的檢查,發現不符合地鐵管理條例的人員不予進入。這就是過濾功能,同時當你迷路的時候,你可以詢問安保人,他會為你指導方向,這就是路由功能。

加入服務閘道器的專案架構

3.1 pom 檔案

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>

3.2 yml 檔案

server:
  port: 8005 # 埠

spring:
  application:
    name: microservicloud-zuul-gateway # 專案名稱
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka # 註冊中心

3.3 啟動類

@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy // 啟動zuul 元件
public class AppApllcation8005 {
    public static void main( String[] args ) {
         SpringApplication.run(AppApllcation8005.class, args);
    }
}

3.4 啟動效果

訪問 http://localhost:8005/microservicloud-dept/list 介面:

可以看到我們通過閘道器地址就訪問到了服務端介面。這就是服務閘道器的路由功能。

四、config元件(配置中心)

當專案越來越多,伴隨的配置檔案也越來越多。我們是否可以將這次雜亂無章的檔案統一進行管理呢,答案是可以的。這就是 config 元件的作用。config 元件主要是利用 git 作為配置服務站,實現檔案統一管理。當配置檔案更新的時候,我們就可以拉去最新的檔案。

五、github

https://github.com/buerbl/learnSpringCloud

六、關注微信公眾號,隨時移動端閱讀