本章課程學習目標:
(1)Spring Cloud微服務架構
(2)服務發現組件 Eureka與Feign實現服務間的調用框架
(3)SHOP商城微服務 Eureka與Feign組件的應用
1Spring Cloud簡介
1.1什麼是Spring Cloud
Spring Cloud是一系列框架的有序集合。它利用Spring Boot的開發便利性巧妙簡化了分佈式系統基礎設施的開發,如服務發現註冊、配置中心、消息總線、負載均衡、熔斷器、數據監控等,都可以用Spring Boot的開發風格做到一鍵啓動和部署。Spring並沒有重複製造輪子,它只是將目前各家公司開發的比較成熟、經得起實際考驗的服務框架組合起來,通過Spring Boot風格進行再封裝屏蔽掉了複雜配置和實現原理,最終給開發者留出了一套簡單易懂、易部署和易維護的分佈式系統開發工具包。
1.2Spring Cloud與spring boot的關係
Spring Boot 是 Spring 的一套快速配置腳手架,可以基於Spring Boot 快速開發單
個微服務,Spring Cloud是一個基於Spring Boot實現的雲應用開發工具;Spring Boot專注於快速、方便集成的單個微服務個體,Spring Cloud關注全局的服務治理框架;Spring Boot使用了默認大於配置的理念,很多集成方案已經幫你選擇好了,能不配置就不配置,Spring Cloud很大的一部分是基於Spring Boot來實現,可以不基於Spring Boot嗎?不可以。
Spring Boot可以離開Spring Cloud獨立使用開發項目,但是Spring Cloud離不開
Spring Boot,屬於依賴的關係。
1.3Spring Cloud主要框架
- 服務發現—— Netflix Eureka
- 服務調用—— Netflix Feign
- 熔斷器——Netflix Hystrix
- 服務網關—— Netflix Zuul
- 分佈式配置——Spring Cloud Config
- 消息總線 —— Spring Cloud Bus
1.4Spring Cloud與dubbo對比
或許很多人會說Spring Cloud和Dubbo的對比有點不公平,Dubbo只是實現了服務治理,而Spring Cloud下面有17個子項目(可能還會新增)分別覆蓋了微服務架構下的方方面面,服務治理只是其中的一個方面,一定程度來說,Dubbo只是Spring Cloud Netflix中的一個子集。
Dubbo | Spring cloud | |
---|---|---|
服務註冊中心 | Zookeeper | Spring Cloud Netflix Eureka |
服務調用方式 | RPC | REST API |
服務網關 | 無 | Spring Cloud Netflix Zuul |
熔斷器 | 無 | Spring Cloud Netflix Hystrix |
分佈式配置 | 無 | Spring Cloud Config |
服務跟蹤 | 無 | Spring Cloud Sleuth |
消息總線 | 無 | Spring Cloud Bus |
數據流 | 無 | Spring Cloud Stream |
批量任務 | 無 | Spring Cloud Task |
2 服務發現組件 Eureka
Spring Cloud Eureka是Spring Cloud Netflix 子項目的核心組件之一,主要用於微服務架構中的服務治理。
Eureka包含兩個組件:
Eureka Server和Eureka Client。
Eureka Server提供服務註冊服務,各個節點啓動後,會在Eureka Server中進行注
冊,這樣EurekaServer中的服務註冊表中將會存儲所有可用服務節點的信息,服務節點的信息可以在界面中直觀的看到。
Eureka Client是一個java客戶端,用於簡化與Eureka Server的交互,客戶端同時也就別一個內置的、使用輪詢(round-robin)負載算法的負載均衡器。在應用啓動後,將會向Eureka Server發送心跳,默認週期爲30秒,如果Eureka Server在多個心跳週期內沒有接收到某個節點的心跳,Eureka Server將會從服務註冊表中把這個服務節點移除(默認90秒)。
3Feign實現服務間的調用
3.1Feign簡介
Feign是簡化Java HTTP客戶端開發的工具(java-to-httpclient-binder),它的靈感來自於Retrofit、JAXRS-2.0WebSocket。Feign的初衷是降低統一綁定Denominator到HTTP API的複雜度,不區分是否爲restful。
4微服務代碼實戰
案例demo:搭建spring boot微服務項目,目前包含訂單微服務和商品微服務。以及base基礎功能微服務。
如何利用spring Cloud 服務發現組件和服務調用組件Feign去開發一個簡單的小型微服務。
4.1首先創建spring boot單體項目
創建如上圖meaven項目 shop_base基礎商城微服務
Shop_order訂單微服務
Shop_product商品微服務
4.2將每一個微服務的數據庫配置增刪改查配置一遍
數據庫表建設完成
4.2.1測試spring boot單體架構項目
- 查詢商品列表
- 查詢訂單列表
4.3搭建商城shop微服務註冊中心Eureka
4.3.1新建註冊中心模塊shop_eureka配置pom.xml
4.3.2添加application.yml
4.3.3配置啓動類開啓註冊中心服務
創建包com.springcloud.eureka ,包下建立類:
package com.springcloud.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
//開啓服務註冊中心服務
@EnableEurekaServer
public class EurekaServer {
public static void main(String[] args) {
SpringApplication.run(EurekaServer.class, args);
}
}
4.3.4啓動運行啓動類
瀏覽器輸入地址:http://localhost:9898/ 運行效果如下圖:
4.4將訂單微服務和商品微服務註冊到Eureka服務中心
如何將訂單微服務shop_order和shop_product注入到eureka服務中心呢?
4.4.1添加依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
4.4.2修改application.yml,添加註冊eureka服務的配置
eureka:
client:
service‐url:
defaultZone: http://localhost:9898/eureka
instance:
prefer‐ip‐address: true
4.4.3修改服務啓動類,添加註解
@EnableEurekaClient
4.4.4啓動測試
啓動測試:將訂單和商品微服務啓動起來,會發現eureka的註冊列表中可以看到這些微服務了。
4.5如何利用Feign進行商品微服務和訂單微服務的調用
創建訂單服務:
(調用商品微服務)
(1)調用 商品微服務進行商品列表查詢
4.5.1Order微服務調用接口查詢商品列表(Feign服務調用)
由於orader訂單屬於訂單微服務,商品屬於商品微服務,所以現在要使用訂單微服務去申請調用商品微服務接口的開發。
- 在shop_order模塊添加依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2 修改shop_order模塊的啓動類,添加註解
@EnableDiscoveryClient
@EnableFeignClients
3. 在shop_order模塊創建 com.springcloud.order.client包,包下創建接口
@FeignClient("shop-product")
public interface ProductClient {
/***
* 查詢商品列表
* 相當於就是商品微服務中的controller
*/
@RequestMapping(value="/search",method = RequestMethod.POST)
public Result findSearch(@RequestBody Map searchMap);
}
4. 修改shop_order模塊的OrderController
/***
* 訂單微服務調用商品微服務
*/
@Autowired
private ProductClient productClient;
@RequestMapping(value = "/search/product")
public Result findProducts(@RequestBody Map searchMap){
Result result = productClient.findSearch(searchMap);
return result;
}
5 測試
注意啓動的先後順序:先啓動product微服務
測試結果成功:看來Feign可以實現微服務之間的調用
4.5.2項目中業務邏輯處理微服務之間調用
上面只是測試一下訂單中使用商品微服務。
創建訂單接口: 首先查詢訂單的商品列表信息(需要調用商品微服務)
需要根據商品減少庫存(需要調用商品微服務)
用戶創建訂單。
代碼實現
我們現在就從controller編寫一個創建訂單的接口:
/**
* 創建訂單
* @param order
*/
@RequestMapping(method=RequestMethod.POST)
public Result add(@RequestBody Order order ){
orderService.add(order);
return new Result(true,StatusCode.OK,"增加成功");
}
Service層中加入創建訂單步驟
/**
* 增加
* @param order
*/
public void add(Order order) {
/*****
* (1)調用商品微服務進行查詢調用,查詢列表
*/
Result result = productClient.findSearch(new HashMap());
Object obj=result.getData();
System.out.println("訂單微服務Feign遠程調用查詢商品微服務列表信息:");
/****
* (2)調用商品微服務進行庫存減少
*/
order.setId( idWorker.nextId()+"" );
orderDao.save(order);
}
測試結果:
5個人說明
經驗是由一點一點積累的,思維也是由一天一天訓練出來的。
謝謝大家的閱讀,原創不易,如果你認爲文章對你有所幫助,就點個贊感謝大家支持,你的點贊是我持續寫作的動力。