目錄
- 第一章、概念介紹
- 1.1)什么是RPC框架
- 1.2)什么是分布式系統
- 1.3)Dubbo概述
- 1.3)Dubbo基本架構
- 第二章、服務提供者
- 2.1)目錄結構和依賴
- 2.2)model層
- 2.3)service層
- 2.4)resources配置文件
- 第三章、服務消費者
- 3.1)目錄結構和依賴
- 3.2)service層
- 3.3)resources配置文件
友情提醒
先看文章目錄,大致了解文章知識點結構,點擊文章目錄可直接跳轉到文章指定位置。 |
第一章、概念介紹
1.1)什么是RPC框架
RPC 【Remote Procedure Call】是指遠程過程調用,是一種進程間通信方式,是一種技術思想,而不是規范。它允許程序調用另一個地址空間(網絡的另一臺機器上)的過程或函數,而不用開發人員顯式編碼這個調用的細節。調用本地方法和調用遠程方法一樣。
1.2)什么是分布式系統
分布式系統是若干獨立計算機(服務器)的集合,這些計算機對于用戶來說就像單個相關系統,分布式系統(distributed system)是建立在網絡之上的服務器端一種結構。
部署在獨立服務器上的各個子系統(項目),相互之間可以調用,形成一個大型分布式系統
獨立部署的服務器沒有額外要求,只需要滿足子系統需求即可。
分布式系統中的計算機可以使用不同的操作系統,可以運行不同應用程序提供服務,將服務分散部署到多個計算機服務器上。
1.3)Dubbo概述
Dubbo官網網址:官網鏈接
①Apache Dubbo 是一款高性能、輕量級的開源Java RPC框架,它提供了三大核心能力:面向接口的遠程方法調用,智能容錯和負載均衡,以及服務自動注冊和發現。可以和Spring框架無縫集成。
②Dubbo是一個分布式服務框架,致力于提供高性能和透明化的RPC遠程服務調用方案、服務治理方案。
③面向接口代理:調用接口的方法,在A服務器調用B服務器的方法,由dubbo實現對B的調用,無需關心實現的細節,就像MyBatis訪問Dao的接口,可以操作數據庫一樣。不用關心Dao接口方法的實現。這樣開發是方便,舒服的。
④支持多種協議:dubbo , hessian , rmi , http, webservice , thrift , memcached , redis。dubbo官方推薦使用dubbo協議。dubbo協議默認端口20880
1.3)Dubbo基本架構
①服務提供者(Provider):暴露服務的服務提供方,服務提供者在啟動時向注冊中心注冊自己提供的服務。服務容器spring負責啟動,加載,運行服務提供者。
②服務消費者(Consumer): 調用遠程服務的服務消費方,服務消費者在啟動時,向注冊中心訂閱自己所需的服務,服務消費者從提供者地址列表中,基于軟負載均衡算法,選一臺提供者進行調用,如果調用失敗,再選另一臺調用。
③注冊中心(Registry):注冊中心返回服務提供者地址列表給消費者,如果有變更,注冊中心將基于長連接推送變更數據給消費者 –如果信息有變,注冊中心提供新的信息給消費者
④監控中心(Monitor):服務消費者和提供者,在內存中累計調用次數和調用時間,定時每分鐘發送一次統計數據到監控中心 –監控服務提供者、消費者狀態,與開發沒有直接關系
第二章、服務提供者
2.1)目錄結構和依賴
pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.bjpowernode.dubbo</groupId><artifactId>001-link-orderservice-provider</artifactId><version>1.0.0</version><dependencies><!--Spring --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.3.16.RELEASE</version></dependency><!--Dubbo依賴--><dependency><groupId>com.alibaba</groupId><artifactId>dubbo</artifactId><version>2.6.2</version></dependency></dependencies><build><plugins><!--JDK1.8編譯插件--><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.1</version><configuration><source>1.8</source><target>1.8</target></configuration></plugin></plugins></build></project>
2.2)model層
order
package com.bjpowernode.dubbo.model;import java.io.Serializable;public class Order implements Serializable {private String id;private String goodsName;private Double price;private Integer amount;public Order() {}public Order(String id, String goodsName, Double price, Integer amount) {this.id = id;this.goodsName = goodsName;this.price = price;this.amount = amount;}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getGoodsName() {return goodsName;}public void setGoodsName(String goodsName) {this.goodsName = goodsName;}public Double getPrice() {return price;}public void setPrice(Double price) {this.price = price;}public Integer getAmount() {return amount;}public void setAmount(Integer amount) {this.amount = amount;}@Overridepublic String toString() {return "Order{" +"id='" + id + '\'' +", goodsName='" + goodsName + '\'' +", price=" + price +", amount=" + amount +'}';}
}
2.3)service層
OrderService
package com.bjpowernode.dubbo.service;import com.bjpowernode.dubbo.model.Order;public interface OrderService {public Order addOrder(Integer userId,String goodsName, Double price,Integer amount);
}
OrderServiceImpl
package com.bjpowernode.dubbo.service;import com.bjpowernode.dubbo.model.Order;public class OrderServiceImpl implements OrderService{public Order addOrder(Integer userId, String goodsName, Double price, Integer amount) {return new Order("110",goodsName,price,amount);}
}
OrderApplication
package com.bjpowernode.dubbo;import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;import java.io.IOException;public class OrderApplication {public static void main(String[] args) throws IOException {/*** 啟動spring容器:閱讀配置文件* 1、 new ClassPathXmlApplicationContext("orderservce-provider.xml");* 2、 new FileSystemXmlApplicationContext("D:/orderservce-provider.xml");* 3、tomcat啟動*/new ClassPathXmlApplicationContext("orderservce-provider.xml");//標準鍵盤輸入:線程會阻塞System.in.read();}
}
2.4)resources配置文件
orderservce-provider.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!--服務項目名稱:唯一 ,它的名稱是dubbo內部使用的唯一標識 飯店名稱--><dubbo:application name="001-link-order-service-provider"></dubbo:application><!--定義協議:告訴消費者如何訪問 怎么訪問--><dubbo:protocol name="dubbo" port="20880"></dubbo:protocol><!--dubbo:service:提供(暴露)服務 菜單interface:區分不同的服務ref:關聯真正提供服務的bean對象registry="N/A":直連--><dubbo:serviceinterface="com.bjpowernode.dubbo.service.OrderService"ref="orderServiceImpl" registry="N/A"/><!--真正提供服務的bean對象 廚師--><bean id="orderServiceImpl" class="com.bjpowernode.dubbo.service.OrderServiceImpl"></bean>
</beans>
第三章、服務消費者
3.1)目錄結構和依賴
pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.bjpowernode.dubbo</groupId><artifactId>002-link-main-web</artifactId><version>1.0.0</version><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.3.16.RELEASE</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>dubbo</artifactId><version>2.6.2</version></dependency><dependency><groupId>com.bjpowernode.dubbo</groupId><artifactId>001-link-orderservice-provider</artifactId><version>1.0.0</version></dependency></dependencies></project>
3.2)service層
ShopService
package com.bjpowernode.dubbo.service;import com.bjpowernode.dubbo.model.Order;public interface ShopService {public Order buyGoods(Integer userId, String goodsName, Double price, Integer amount);}
ShopServiceImpl
package com.bjpowernode.dubbo.service;import com.bjpowernode.dubbo.model.Order;public class ShopServiceImpl implements ShopService {OrderService orderService;public void setOrderService(OrderService orderService) {this.orderService = orderService;}public Order buyGoods(Integer userId, String goodsName, Double price, Integer amount) {// new OrderServiceImpl().addOrder()return orderService.addOrder(userId, goodsName, price, amount);}
}
ShopApplication
package com.bjpowernode.dubbo;import com.bjpowernode.dubbo.model.Order;
import com.bjpowernode.dubbo.service.ShopService;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class ShopApplication {public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("shop-consume.xml");ShopService shopServiceImpl = (ShopService)context.getBean("shopServiceImpl");Order order = shopServiceImpl.buyGoods(1111, "apple", 10d, 2);System.out.println(order);}
}
3.3)resources配置文件
shop-consume.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!--服務項目名稱:唯一--><dubbo:application name="002-link-main-web"></dubbo:application><!--dubbo:reference:生成一個 代表遠程服務的 bean對象id="remoteOrderService":bean對象名稱url:dubbo服務地址interface:區分不同的服務registry="N/A":直連--><dubbo:referenceid="remoteOrderService"url="dubbo://localhost:20880"interface="com.bjpowernode.dubbo.service.OrderService"registry="N/A"/><bean id="shopServiceImpl" class="com.bjpowernode.dubbo.service.ShopServiceImpl"><property name="orderService" ref="remoteOrderService"></property></bean></beans>