Spring整合Dubbo框架過程及原理解析
這篇文章主要介紹了Spring整合Dubbo框架過程及原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
Dubbo作為一個(gè)RPC框架,其最核心的功能就是要實(shí)現(xiàn)跨網(wǎng)絡(luò)的遠(yuǎn)程調(diào)用。演示過程創(chuàng)建兩個(gè)小工程,一個(gè)作為服務(wù)的提供者,一個(gè)作為服務(wù)的消費(fèi)者。通過Dubbo來實(shí)現(xiàn)服務(wù)消費(fèi)者遠(yuǎn)程調(diào)用服務(wù)提供者的方法。
dubbo 的使用需要一個(gè)注冊中心,這里以Zookeeper為例來演示
1.Dubbo架構(gòu)
Dubbo架構(gòu)圖(Dubbo官方提供)如下:


節(jié)點(diǎn)角色說明:
| 節(jié)點(diǎn) | 角色名稱 |
|---|---|
| Provider | 暴露服務(wù)的服務(wù)提供方 |
| Consumer | 調(diào)用遠(yuǎn)程服務(wù)的服務(wù)消費(fèi)方 |
| Registry | 服務(wù)注冊與發(fā)現(xiàn)的注冊中心 |
| Monitor | 統(tǒng)計(jì)服務(wù)的調(diào)用次數(shù)和調(diào)用時(shí)間的監(jiān)控中心 |
| Container | 服務(wù)運(yùn)行容器 |
調(diào)用關(guān)系說明:
服務(wù)容器負(fù)責(zé)啟動(dòng),加載,運(yùn)行服務(wù)提供者。
服務(wù)提供者在啟動(dòng)時(shí),向注冊中心注冊自己提供的服務(wù)。
服務(wù)消費(fèi)者在啟動(dòng)時(shí),向注冊中心訂閱自己所需的服務(wù)。
注冊中心返回服務(wù)提供者地址列表給消費(fèi)者,如果有變更,注冊中心將基于長連接推送變更數(shù)據(jù)給消費(fèi)者。
服務(wù)消費(fèi)者,從提供者地址列表中,基于軟負(fù)載均衡算法,選一臺提供者進(jìn)行調(diào)用,如果調(diào)用失敗,再選另一臺調(diào)用。
服務(wù)消費(fèi)者和提供者,在內(nèi)存中累計(jì)調(diào)用次數(shù)和調(diào)用時(shí)間,定時(shí)每分鐘發(fā)送一次統(tǒng)計(jì)數(shù)據(jù)到監(jiān)控中心。
2.服務(wù)提供者開發(fā)
(1) 創(chuàng)建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相關(guān) -->
<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">
<!-- 監(jiān)聽器監(jiān)聽其他的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) 創(chuàng)建服務(wù)接口
package com.test.dubbo.api;
public interface HelloService {
public String sayHello(String name);
}
(4) 創(chuàng)建服務(wù)實(shí)現(xiàn)類
package com.test.service.impl;
import com.test.dubbo.api.HelloService;
//這個(gè)service并不是spring提供的,是dubbo的service代替的
import com.alibaba.dubbo.config.annotation.Service;
@Service //把此服務(wù)注冊到zookeeper
public class HelloServiceImpl implements HelloService {
public String sayHello(String name) {
return "hello " + name;
}
}
==注意==:服務(wù)實(shí)現(xiàn)類上使用的Service注解是Dubbo提供的,用于對外發(fā)布服務(wù)
(5) 在src/main/resources下創(chuàng)建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">
<!-- 當(dāng)前應(yīng)用名稱,用于注冊中心計(jì)算應(yīng)用間依賴關(guān)系,注意:消費(fèi)者和提供者應(yīng)用名不要一樣 -->
<dubbo:application name="dubbodemo_provider" />
<!-- 連接服務(wù)注冊中心zookeeper ip為zookeeper所在服務(wù)器的ip地址-->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- 注冊 協(xié)議和port 端口默認(rèn)是20880 -->
<dubbo:protocol name="dubbo" port="20881"></dubbo:protocol>
<!-- 掃描指定包,加入@Service注解的類會被發(fā)布為服務(wù) -->
<dubbo:annotation package="com.test.service.impl" />
</beans>
6)啟動(dòng)服務(wù) 也就是把spring容器啟動(dòng)即可
可以用tomcat啟動(dòng)項(xiàng)目
也可以用main方法加載spring配置文件,也就是啟動(dòng)了spring容器
在com.itheima包下創(chuàng)建一個(gè)DemoProvider類來啟動(dòng)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 {
// 加載配置文件,啟動(dòng)容器
ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-provider.xml");
app.start();
System.in.read(); //等待控制臺回車。如果不回車就一直卡這兒不繼續(xù)
}
}
3.服務(wù)消費(fèi)者開發(fā)
開發(fā)步驟:
(1) 創(chuàng)建maven工程(打包方式為war)dubbodemo_consumer,pom.xml配置和上面服務(wù)提供者相同
(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>
<!-- 指定加載的配置文件 ,通過參數(shù)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) 將服務(wù)提供者工程中的HelloService接口復(fù)制到當(dāng)前工程
(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來注入服務(wù)
private HelloService helloService;
@RequestMapping("/hello")
@ResponseBody
public String getName(String name){
//遠(yuǎn)程調(diào)用
String result = helloService.sayHello(name);
System.out.println(result);
return result;
}
}
==注意==:Controller中注入HelloService使用的是Dubbo提供的@Reference注解
(5) 在src/main/resources下創(chuàng)建spring文件夾,再創(chuàng)建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 當(dāng)前項(xiàng)目是哪個(gè)-->
<dubbo:application name="dubbodemo_consumer"/>
<!--鏈接注冊中心-->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!--注解掃描 掃描的是dubbo的 @Reference注解-->
<dubbo:annotation package="cn.test"/>
<!-- timeout:每次請求都會等待3秒 retries失敗后重試次數(shù)-->
<dubbo:consumer timeout="3000" retries="0"/>
</beans>
dubbo的使用小demo已經(jīng)完成。大家可以嘗試一下
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot使用@RefreshScope注解實(shí)現(xiàn)配置文件的動(dòng)態(tài)加載
本文主要介紹了Springboot使用@RefreshScope注解實(shí)現(xiàn)配置文件的動(dòng)態(tài)加載,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
Spring?Boot小型項(xiàng)目如何使用異步任務(wù)管理器實(shí)現(xiàn)不同業(yè)務(wù)間的解耦
這篇文章主要介紹了Spring?Boot小型項(xiàng)目如何使用異步任務(wù)管理器實(shí)現(xiàn)不同業(yè)務(wù)間的解耦,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08
Spring MVC利用Swagger2如何構(gòu)建動(dòng)態(tài)RESTful API詳解
這篇文章主要給大家介紹了關(guān)于在Spring MVC中利用Swagger2如何構(gòu)建動(dòng)態(tài)RESTful API的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10
Idea如何使用System.getenv()獲取環(huán)境變量的值
文章介紹了如何在Java中使用`System.getenv()`方法讀取環(huán)境變量的值,并提供了兩種配置環(huán)境變量的方法:啟動(dòng)項(xiàng)配置和系統(tǒng)環(huán)境變量配置,對于系統(tǒng)環(huán)境變量,文章特別指出需要重啟電腦或程序才能使其生效2024-11-11
springboot如何使用yml文件方式配置shardingsphere
這篇文章主要介紹了springboot如何使用yml文件方式配置shardingsphere問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09

