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

show me the code and talk to me,做的出來更要說的明白
GitHub 項目learnSpringCloud同步收錄
我是布爾bl,你的支持是我分享的動力!

一、 引入

上回 Spring Cloud 理論篇 介紹了 Spring Cloud 的常見組件,讓讀者對 Spring Cloud 有了一個宏觀認識,這是從理論層面出發的。接下來我們就進入 Spring Cloud 的實戰教程,擼起袖子,真槍實彈幹一場。在實戰演練中感受一下 Spring Cloud 的魅力所在。在教程中,我會將 Spring Cloud 常見組件進行整合。整個過程就像搭積木一樣,一點一點地完成一個微服務工程的搭建。實戰演練是比較繁瑣的,但是隻要我們真正地去做了,就會收穫很多。

二、Eureka 組件(註冊中心)

作爲 Spring Cloud 的註冊中心,我們第一步需要把 Eureka 組件搭起來。因爲接下來的消費者以及生產者都是以 Eureka 組件爲基礎展開的。

2.1 pom 文件

我們引入 Eureka 的依賴。

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

正如 Spring Cloud 理論篇 提到 Spring Cloud 是基於 SpringBoot 開發的。那麼讀者是否有疑惑爲什麼不需要
web模塊呢?當然是需要的,只不過是 Eureka 的依賴的依賴中已經包含了必要的模塊。

2.2 yml 文件

引入必要模塊後,我們就要去配置 yml 文件了。

server:
  port: 7001

eureka:
  instance:
    hostname: localhost # eureka 服務端的實例名稱
  client:
    register-with-eureka: false # 表示不需要向註冊中心註冊自己
    fetch-registry: false # false 表示自己端就是註冊中心,我的職責就是維護服務實例,並不需要去檢索服務
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka # 設置與 Eureka Server 交互的地址查詢服務和註冊服務

至於配置相關的文件意義,註釋已經說的很清楚了。詳情請看註釋。

2.3 啓動類

最後寫好啓動類,就算完成的Eureka 組件的搭建了。

@SpringBootApplication
@EnableEurekaServer
public class AppApplication7001 {
    public static void main( String[] args ) {
        SpringApplication.run(AppApplication7001.class, args);
    }
}

可以看到我們的啓動類的註解除了萬年不變的@SpringBootApplication,還增加了 @EnableEurekaServer,該註解的作用是表明該工程作爲 Eureka 組件存在的。就好像我們怎麼證明自己的身份呢,拿出自己的身份證即可。

2.4 啓動效果

Eureka 組件啓動如圖所示。

三、生產者(服務提供者)

3.1 pom 文件

我們再新建一個工程,該工程其實本質也是一個 Eureka 組件,不過我們可以通過配置文件使其變成生產者。可能讀者有點迷糊,我打個比方。社會上有警察、醫生、碼畜等等,有很多的身份類型,但本質我們都是人。我們都是有感情的生物。回到代碼層面,首先我們需要引入相關依賴。

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

3.2 yml 文件

server:
  port: 8002  # 服務的端口
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

3.3 啓動類

@SpringBootApplication
@EnableEurekaClient
public class AppApllcation8002{
    public static void main( String[] args ) {
         SpringApplication.run(AppApllcation8002.class, args);
    }
}

這裏我們可以看到註冊中心的啓動類和服務提供者的註解是不一樣的。

註冊中心 服務提供者
@EnableEurekaServer @EnableEurekaClient

3.4 啓動效果

目前我們有了兩個工程,啓動的時候是有順序的。首先啓動註冊中心然後再啓動服務提供者。這就好比我們先要有房子了,纔可以入住一樣。

對比上圖我們可以發現網頁多出來一個服務提供者。說明服務提供者項目搭建完成。

四、消費者(服務消費者)

4.1 pom 文件

有個服務提供者,那麼是不是應該有服務消費者呢?此時我們需要新建另外一個工程作爲服務消費者。那麼問題來了,作爲服務消費者,我們怎樣去調用服務提供者呢?此時,肯定是不能像以前直接在 controller 層調用 service 層一樣了,因爲服務消費者和服務提供者是兩個工程了,並且分別運行在兩個 tomcat 裏面。這裏我們就需要進行網絡調用了。在 Spring Cloud 裏面我們可以使用 Feign 進行不同服務的調用。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-feign</artifactId>
</dependency>

4.2 yml 文件

接下來我們需要配置yml 文件。

server:
  port: 80

eureka:
  client:
    register-with-eureka: false  # 不向註冊中心註冊了
    service-url:
      defaultZone: http://localhost:7001/eureka

因爲工程是作爲服務消費者存在的。所以我們不需要往註冊中心註冊服務。這樣註冊中心就只管理好服務提供者即可。

4.3 啓動類以及feign類接口

@FeignClient(value = "microservicloud-dept") // 服務提供者的名字
public interface IFeignService {
    @GetMapping("/provide")
    String feign();
}

@SpringBootApplication
@RestController
@EnableEurekaClient
@EnableFeignClients(basePackages = {"com.example"})
public class AppApplication80 {
    public static void main( String[] args ) {
        SpringApplication.run(AppApplication80.class, args);
    }

    @Autowired
    private IFeignService feignService;
    @GetMapping("/controller/feign")
    public String feign(){
        return feignService.feign();
    }

}

五、總結

至此我們就完成了一個簡單的 Spring Cloud 的微服務架構多模塊項目。根據項目搭建,我們可以簡單畫出架構圖。具體源碼已經在開頭引用給出,可以直接克隆運行。

關注微信公衆號,隨時移動端閱讀

公衆號.jpg

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章