搭建一个简单的本地的dubbo-demo案例
一、创建一个Maven工程,然后创建三个module模块
二、dubbo-api(maven模块)
创建一个api类,命名为ApiService.java
package com.example.service; public interface ApiService { public String say(String hello); }
三、dubbo-provider(springboot模块)
1.配置pom.xml,添加依赖
<!-- dubbo-api模块打成jar包,然后导入依赖 --> <dependency> <groupId>com.example</groupId> <artifactId>dubbo-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!-- 导入阿里的依赖 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.8.4</version> </dependency> <!-- 导入zookeeper的依赖 --> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency>
2.创建一个ProviderServiceImpl.java
package com.example.service.imp; import com.example.service.ApiService; import org.springframework.stereotype.Service; @Service public class ProviderServiceImpl implements ApiService { @Override public String say(String hello) { System.err.println("provider调用成功"); return hello; } }
3.在src/main/java/resources下创建一个provider.xml并配置dubbo的端口
<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!--定义了提供方应用信息,用于计算依赖关系;在 dubbo-admin 或 dubbo-monitor 会显示这个名字,方便辨识--> <dubbo:application name="demotest-provider" owner="programmer" organization="dubbox" /> <!--使用 zookeeper 注册中心暴露服务,注意要先开启 zookeeper,这里配置的adress是集群模式,个人用修改为本地:localhost:2181--> <dubbo:registry port="8086" protocol="zookeeper" address="172.30.0.177:2181,172.30.0.178:2181,172.30.0.180:2181"/> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20878" /> <!--使用 dubbo 协议实现定义好的 api.PermissionService 接口,黑色字体部分修改为创建类的包目录 -->
<dubbo:service interface="com.example.service.ApiService" ref="apiService" protocol="dubbo" />
<!--具体实现该接口的 bean-->
<bean id="apiService" class="com.example.service.imp.ProviderServiceImpl"/> </beans>
4.在DubboProvider1Application内导入配置的xml文件,然后在运行
5.如果注册成功可以在DUBBO注册中心查看 http://172.30.0.82:4000/ ,账号:root 密码:root
四、dubbo-consumer(spring boot模块)
1.配置pom.xml
<!-- dubbo-api模块打成jar包,然后导入依赖 --> <dependency> <groupId>com.example</groupId> <artifactId>dubbo-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!-- 导入阿里的依赖 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.8.4</version> </dependency> <!-- 导入zookeeper的依赖 --> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency>
2.创建一个控制类ConsumerController.java
package com.example.controller; import com.example.service.ApiService; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController public class ConsumerController { @Resource ApiService apiService; @GetMapping("/consumer") public String consumer() { return apiService.say("hello"); } }
3.创建Consumer.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/beans http://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="demotest-consumer"/> <!--向 zookeeper 订阅 provider 的地址,由 zookeeper 定时推送--> <dubbo:registry protocol="zookeeper" address="172.30.0.177:2181,172.30.0.178:2181,172.30.0.180:2181"/> <!--使用 dubbo 协议调用定义好的 api.PermissionService 接口--> <dubbo:reference id="apiService" interface="com.example.service.ApiService"/> </beans>
4.配置application.properties文件
#配置访问端口 server.port=8085
5.运行DubboConsumer1Application
五、打开浏览器,输入 localhost:8085/consumer
访问成功,做完后,最好将api,provider做成一个项目,consumer做成另一个项目,consumer通过RPC的方式调用provider,这样感知dubbo更明显