4 Dubbo快速入門
Dubbo作為一個RPC框架,其最核心的功能就是要實現跨網絡的遠程調用。本小節就是要創建兩個應用,一個作為服務的提供方,一個作為服務的消費方。通過Dubbo來實現服務消費方遠程調用服務提供方的方法。
4.1 服務提供方開發
開發步驟:
(1)創建maven工程(打包方式為war)dubbodemo_provider,在pom.xml文件中導入如下坐標
war
UTF-8
1.8
1.8
5.0.5.RELEASE
org.springframework
spring-context
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-aspects
${spring.version}
org.springframework
spring-jms
${spring.version}
org.springframework
spring-context-support
${spring.version}
com.alibaba
dubbo
2.6.0
org.apache.zookeeper
zookeeper
3.4.7
com.github.sgroschupf
zkclient
0.1
javassist
javassist
3.12.1.GA
com.alibaba
fastjson
1.2.47
org.apache.maven.plugins
maven-compiler-plugin
2.3.2
1.8
1.8
org.apache.tomcat.maven
tomcat7-maven-plugin
8081
/
(2)配置web.xml文件
創建 webapp/WEB-INF/web.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
contextConfigLocation
classpath:applicationContext*.xml
org.springframework.web.context.ContextLoaderListener
(3)創建服務接口
package com.maweiqi.service;
/**
* HelloService
*
* @Author: 馬 偉 奇
* @CreateTime: 2019-07-18
* @Description:
*/
public interface HelloService {
public String sayHello(String name);
}
(4)創建服務實現類
package com.maweiqi.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.maweiqi.service.HelloService;
/**
* HelloServiceImpl
*
* @Author: 馬 偉 奇
* @CreateTime: 2019-07-18
* @Description:
*/
@Service
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return "hello " + name;
}
}
注意:服務實現類上使用的Service注解是Dubbo提供的,用于對外發布服務
(5)在src/main/resources下創建applicationContext-service.xml
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">
(6)啟動服務
啟動服務
4.2 服務消費方開發
開發步驟:
(1)創建maven工程(打包方式為war)dubbodemo_consumer,pom.xml配置和上面服務提供者相同,只需要將Tomcat插件的端口號改為8082即可
war
UTF-8
1.8
1.8
5.0.5.RELEASE
org.springframework
spring-context
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-aspects
${spring.version}
org.springframework
spring-jms
${spring.version}
org.springframework
spring-context-support
${spring.version}
com.alibaba
dubbo
2.6.0
org.apache.zookeeper
zookeeper
3.4.7
com.github.sgroschupf
zkclient
0.1
javassist
javassist
3.12.1.GA
com.alibaba
fastjson
1.2.47
org.apache.maven.plugins
maven-compiler-plugin
2.3.2
1.8
1.8
org.apache.tomcat.maven
tomcat7-maven-plugin
8082
/
(2)配置web.xml文件
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:applicationContext-web.xml
1
springmvc
/
(3)將服務提供者工程中的HelloService接口復制到當前工程
package com.maweiqi.service;
/**
* HelloService
*
* @Author: 馬偉奇
* @CreateTime: 2019-07-18
* @Description:
*/
public interface HelloService {
public String sayHello(String name);
}
(4)編寫Controller
package com.maweiqi.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.maweiqi.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* HelloController
*
* @Author: 馬偉奇
* @CreateTime: 2019-07-19
* @Description:
*/
@Controller
@RequestMapping("/demo")
public class HelloController {
@Reference
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下創建applicationContext-web.xml
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">
(6)運行測試
運行
在瀏覽器輸入http://localhost:8082/demo/hello?name=Jack,查看瀏覽器輸出結果
4.3 代碼重構
① 創建項目:dubbodemo_interface
創建項目
② 把 項目dubbodemo_consumer 和 項目dubbodemo_provider當中的 接口 HelloService 拷貝到dubbodemo_interface工程里面
image.png
③ 刪除工程dubbodemo_consumer 和 工程dubbodemo_provider當中的 接口 HelloService
image.png
dubbodemo_consumer 工程和dubbodemo_provider添加pom文件的依賴
com.maweiqi
dubbodemo_interface
1.0-SNAPSHOT
獲取數據
5. Dubbo管理控制臺
我們在開發時,需要知道Zookeeper注冊中心都注冊了哪些服務,有哪些消費者來消費這些服務。我們可以通過部署一個管理中心來實現。其實管理中心就是一個web應用,部署到tomcat即可。
5.1 安裝
安裝步驟:
(1)將資料中的dubbo-admin-2.6.0.war文件復制到tomcat的webapps目錄下
image.png
(2)啟動tomcat,此war文件會自動解壓
image.png
(3)修改WEB-INF下的dubbo.properties文件,注意dubbo.registry.address對應的值需要對應當前使用的Zookeeper的ip地址和端口號
dubbo.registry.address=zookeeper://192.168.134.129:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest
image.png
(4)重啟tomcat
5.2 使用
操作步驟:
image.png
(2)啟動服務提供者工程和服務消費者工程,可以在查看到對應的信息
image.png
image.png