一、創建一個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更明顯
