分散式服務框架Dubbo入門案例和專案原始碼
![](https://i.iter01.com/images/2a3f40eb1fc04620116455916aad202426f799a591fc7dba8d819e1bbd75cfbc.png)
Dubbo是一個分散式服務框架,致力於提供高效能和透明化的RPC遠端服務呼叫方案,
本專案程式碼,根據官方提供的dubbo-ws-demo-master例子,改造而來。
官網例子原始碼:https://github.com/dubbo/dubbo-ws-demo
官方的例子,都放在1個專案中,介面、實現類、Java應用測試例子。
自己給改造了下,方便在專案中直接使用。
雖說是HelloWorld,也還是要向實際情況靠攏。
3個專案
1.web-service介面專案, 定義介面和供呼叫放引入的dubbo配置。
package com.dubbo.demo;
public interface HelloService {
String hello(String name);
}
介面定義的dubbo配置
spring-dubbo.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:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
">
<dubbo:application name="ws-demo" />
<dubbo:registry address="N/A" />
<dubbo:reference id="helloService" interface="com.dubbo.demo.HelloService" version="1.0.0"
url="webservice://127.0.0.1:9000/com.dubbo.demo.HelloService"/>
</beans>
maven配置
pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.shop</groupId>
<version>1.0.0-SNAPSHOT</version>
<name>web-service</name>
<url>http://maven.apache.org</url>;
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.4.10</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-simple</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>2.6.1</version>
</dependency>
</dependencies>
<build>
<finalName>web-service</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
<artifactId>web-service</artifactId>
</project>
2.web-service-impl介面實現專案
![](https://i.iter01.com/images/011ea2c6ef2b01cc0e3c7539c96bbb74c25bdf7aeca9e1e71c06ed0b3a3be3f9.png)
介面實現類HelloServiceImpl
package com.dubbo.demo.impl;
import com.dubbo.demo.HelloService;
public class HelloServiceImpl implements HelloService {
@Override
public String hello(String name) {
return "Hello, " + name + "!";
}
}
介面實現dubbo配置
spring-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:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
">
<dubbo:application name="ws-demo" />
<dubbo:registry address="N/A" />
<dubbo:protocol name="webservice" port="9000" server="servlet" />
<bean id="helloService" class="com.dubbo.demo.impl.HelloServiceImpl"/>
<dubbo:service interface="com.dubbo.demo.HelloService" version="1.0.0"
protocol="webservice" ref="helloService"/>
</beans>
Maven配置
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.shop</groupId>
<artifactId>web-service-impl</artifactId>
<packaging>war</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>dubbo-ws Maven Webapp</name>
<url>http://maven.apache.org</url>;
<dependencies>
<dependency>
<groupId>com.shop</groupId>
<artifactId>web-service</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<finalName>web-service-impl</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
<?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_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>web-service</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:spring-context.xml
</param-value>
</context-param>
<servlet>
<servlet-name>dubbo</servlet-name>
<servlet-class>
com.alibaba.dubbo.remoting.http.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dubbo</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
3.介面測試(呼叫方)專案
import com.dubbo.demo.HelloService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ConsumerMain {
public static void main(String[] args) {
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("spring-dubbo.xml");
classPathXmlApplicationContext.start();
HelloService helloService = (HelloService) classPathXmlApplicationContext.getBean("helloService");
String world = helloService.hello("World");
System.out.println("=====================================");
System.out.println(world);
System.out.println("=====================================");
}
}
maven配置
類似上面的
Web程式訪問
ConsumerMain是通過應用程式的方式,訪問Dubbo包裝的服務。
而
@Controller
@RequestMapping("")
public class HelloWorldController {
@Autowired
private HelloService helloService;
@ResponseBody
@RequestMapping("hello")
public String hello(){
return helloService.hello("hi dubbo");
}
}
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_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>web-service-test</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:spring-context.xml
</param-value>
</context-param>
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
4.專案測試
a.啟動服務專案
web-service-imp
![](https://i.iter01.com/images/a66244c866643b460b92c9aea83a30e16986bbbb1d7234b94c7cd688d63bad59.png)
b.啟動Java應用程式ConsumerMain,列印結果。
![](https://i.iter01.com/images/26f79da8f2f75290e36cb17f4b6ae2e609733fe901d9832a053f1f0b437e8e51.png)
c.啟動Web應用程式web-service-test,訪問http://localhost:9080/hello
![](https://i.iter01.com/images/907d4dd5d26812cb031a956b49e4e0efb170f0ca3c304d4053f7216c4e5bd173.png)
![](https://i.iter01.com/images/2a3f40eb1fc04620116455916aad202426f799a591fc7dba8d819e1bbd75cfbc.png)
相關文章
- [分散式]--Dubbo分散式服務框架-服務治理分散式框架
- 分散式框架Dubbo入門分散式框架
- ZooKeeper分散式專題與Dubbo微服務入門分散式微服務
- Dubbo+zookeeper實現分散式服務框架分散式框架
- SpringBoot開發案例之整合Dubbo分散式服務Spring Boot分散式
- 探索分散式服務框架Dubbo3:為何選擇Dubbo分散式框架
- 阿里分散式服務框架Dubbo的架構總結阿里分散式框架架構
- 分散式服務Dubbo的前世今生分散式
- debezium官方分散式事務Saga案例原始碼分散式原始碼
- 分散式事務 TCC-Transaction 原始碼分析 —— Dubbo 支援分散式原始碼
- Dubbo原理和原始碼解析之服務引用原始碼
- 分散式服務框架 gRPC分散式框架RPC
- SpringCloud入門及建立分散式專案SpringGCCloud分散式
- Dubbo原始碼之服務引用原始碼
- dubbo服務者原始碼分期原始碼
- Dubbo服務暴露原始碼解析②原始碼
- 分散式服務治理框架Dubbo的前世今生及應用實戰分散式框架
- SpringCloud入門(二)服務間呼叫和案例SpringGCCloud
- Seata 分散式事務框架 TCC 模式原始碼分析分散式框架模式原始碼
- dubbo原始碼分析02:服務引用原始碼
- Dubbo原始碼分析之服務引用原始碼
- Dubbo原始碼分析之服務暴露原始碼
- Dubbo原始碼分析十一、服務路由原始碼路由
- Dubbo原始碼分析(五)Dubbo呼叫鏈-服務端原始碼服務端
- Dubbo原始碼分析(三)Dubbo的服務引用Refer原始碼
- [原始碼解析] PyTorch 分散式(17) --- 結合DDP和分散式 RPC 框架原始碼PyTorch分散式RPC框架
- springboot+dubbo+nacos入門專案Spring Boot
- Spring Cloud Alibaba系列之分散式服務元件DubboSpringCloud分散式元件
- 專案實戰!接入分散式定時任務框架分散式框架
- Dubbo原始碼分析(七)服務目錄原始碼
- Dubbo原始碼解析之服務叢集原始碼
- Dubbo服務呼叫過程原始碼解析④原始碼
- 分散式服務框架開發筆記分散式框架筆記
- Dubbo 整合 Pinpoint 做分散式服務請求跟蹤分散式
- JEESZ架構、分散式服務:Dubbo+Zookeeper+Proxy+Restful架構分散式REST
- Dubbo原始碼解析之服務引入過程原始碼
- Dubbo原始碼解析之服務呼叫過程原始碼
- Dubbo原始碼之服務端的釋出原始碼服務端
- Dubbo原始碼學習之-服務匯出原始碼