Spring整合Dubbo框架過程及原理解析
這篇文章主要介紹了Spring整合Dubbo框架過程及原理解析,文中通過示例程式碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
Dubbo作為一個RPC框架,其最核心的功能就是要實現跨網路的遠端呼叫。演示過程建立兩個小工程,一個作為服務的提供者,一個作為服務的消費者。通過Dubbo來實現服務消費者遠端呼叫服務提供者的方法。
dubbo 的使用需要一個註冊中心,這裡以Zookeeper為例來演示
1.Dubbo架構
Dubbo架構圖(Dubbo官方提供)如下:
節點角色說明:
節點 | 角色名稱 |
---|---|
Provider | 暴露服務的服務提供方 |
Consumer | 呼叫遠端服務的服務消費方 |
Registry | 服務註冊與發現的註冊中心 |
Monitor | 統計服務的呼叫次數和呼叫時間的監控中心 |
Container | 服務執行容器 |
呼叫關係說明:
服務容器負責啟動,載入,執行服務提供者。
服務提供者在啟動時,向註冊中心註冊自己提供的服務。
服務消費者在啟動時,向註冊中心訂閱自己所需的服務。
註冊中心返回服務提供者地址列表給消費者,如果有變更,註冊中心將基於長連線推送變更資料給消費者。
服務消費者,從提供者地址列表中,基於軟負載均衡演算法,選一臺提供者進行呼叫,如果呼叫失敗,再選另一臺呼叫。
服務消費者和提供者,在記憶體中累計呼叫次數和呼叫時間,定時每分鐘傳送一次統計資料到監控中心。
2.服務提供者開發
(1) 建立maven工程(打包方式為war)dubbodemo_provider,新增依賴pom.xml程式碼如下
<?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> <packaging>war</packaging> <artifactId>dubbodemo_provider</artifactId> <dependencys> <!-- dubbo相關 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.6</version> </dependency> <!-- dubbo2.6.X以上的版本開始 2.8.4不需要 --> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.32.Final</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>4.0.0</version> <exclusions> <exclusion> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.7</version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>3.12.1.GA</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency> </dependencys> </project>
(2) 配置web.xml檔案
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <!-- 監聽器監聽其他的spring配置檔案 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext-provider.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
(3) 建立服務介面
package com.test.dubbo.api; public interface HelloService { public String sayHello(String name); }
(4) 建立服務實現類
package com.test.service.impl; import com.test.dubbo.api.HelloService; //這個service並不是spring提供的,是dubbo的service代替的 import com.alibaba.dubbo.config.annotation.Service; @Service //把此服務註冊到zookeeper public class HelloServiceImpl implements HelloService { public String sayHello(String name) { return "hello " + name; } }
==注意==:服務實現類上使用的Service註解是Dubbo提供的,用於對外發布服務
(5) 在src/main/resources下建立applicationContext-provider.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 當前應用名稱,用於註冊中心計算應用間依賴關係,注意:消費者和提供者應用名不要一樣 --> <dubbo:application name="dubbodemo_provider" /> <!-- 連線服務註冊中心zookeeper ip為zookeeper所在伺服器的ip地址--> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <!-- 註冊 協議和port 埠預設是20880 --> <dubbo:protocol name="dubbo" port="20881"></dubbo:protocol> <!-- 掃描指定包,加入@Service註解的類會被髮布為服務 --> <dubbo:annotation package="com.test.service.impl" /> </beans>
6)啟動服務 也就是把spring容器啟動即可
可以用tomcat啟動專案
也可以用main方法載入spring配置檔案,也就是啟動了spring容器
在com.itheima包下建立一個DemoProvider類來啟動spring容器,程式碼如下
package com.test; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException; public class DemoProvider { public static void main(String[] args) throws IOException { // 載入配置檔案,啟動容器 ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-provider.xml"); app.start(); System.in.read(); //等待控制檯回車。如果不回車就一直卡這兒不繼續 } }
3.服務消費者開發
開發步驟:
(1) 建立maven工程(打包方式為war)dubbodemo_consumer,pom.xml配置和上面服務提供者相同
(2) 配置web.xml檔案
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 指定載入的配置檔案 ,通過引數contextConfigLocation載入--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
(3) 將服務提供者工程中的HelloService介面複製到當前工程
(4) 編寫Controller
package com.test.controller; import com.alibaba.dubbo.config.annotation.Reference; import com.itheima.dubbo.api.HelloService; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("/demo") public class HelloController { @Reference//這裡使用dubbo提供的,用來代替@Autowired來注入服務 private HelloService helloService; @RequestMapping("/hello") @ResponseBody public String getName(String name){ //遠端呼叫 String result = helloService.sayHello(name); System.out.println(result); return result; } }
==注意==:Controller中注入HelloService使用的是Dubbo提供的@Reference註解
(5) 在src/main/resources下建立spring資料夾,再建立springmvc.xml檔案
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--spring的掃描包,掃描的是spring的註解--> <context:component-scan base-package="cn.test"/> <!--告訴zookeeper 當前專案是哪個--> <dubbo:application name="dubbodemo_consumer"/> <!--連結註冊中心--> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <!--註解掃描 掃描的是dubbo的 @Reference註解--> <dubbo:annotation package="cn.test"/> <!-- timeout:每次請求都會等待3秒 retries失敗後重試次數--> <dubbo:consumer timeout="3000" retries="0"/> </beans>
dubbo的使用小demo已經完成。大家可以嘗試一下
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。