版權聲明:僅供學習交流使用,如有誤,歡迎指正,轉載請說明來源~ https://blog.csdn.net/G_Youda/article/details/78075512
Dubbo入門
簡介:
架構:
以上內容摘自dubbo-user
入門案例:
安裝ZooKeeper,這裏將用ZooKeeper作爲註冊中心,統一管理服務。(安裝步驟略)
安裝dubbo-admin監控中心(非必須,注:可自行網上查找可用於JDK8的war包,要不然會因爲環境問題無法再Tomcat中啓動)
創建服務提供者工程Dubbo-P
添加依賴jar包(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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.guyouda</groupId>
<artifactId>Dubbo-P</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
</project>
創建服務接口:DemoService.java
package cn.guyouda.dubbo.example;
public interface DemoService {
String sayHello(String name);
}
服務提供者實現DemoService接口:DemoServiceImpl.java
package cn.guyouda.dubbo.example;
public class DemoServiceImpl implements DemoService {
public String sayHello(String name) {
return "Hello," + name;
}
}
創建配置文件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/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="hello-world-app" />
<!-- 使用multicast廣播註冊中心暴露服務地址 -->
<!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
<!-- -->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />
<!-- 用dubbo協議在20880端口暴露服務 -->
<dubbo:protocol name="dubbo" port="20881" />
<!-- 聲明需要暴露的服務接口 -->
<dubbo:service interface="cn.guyouda.dubbo.example.DemoService" ref="demoService" />
<!-- 和本地bean一樣實現服務 -->
<bean id="demoService" class="cn.guyouda.dubbo.example.DemoServiceImpl" />
</beans>
啓動服務:Provider.java
package cn.guyouda.dubbo.example;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Provider {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new
String[] {"classpath:provider.xml"});
context.start();
System.out.println("Provider started");
System.in.read(); // 按任意鍵退出
}
}
創建消費者工程Dubbo-C(結構如下)
引入依賴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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.guyouda</groupId>
<artifactId>SB</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
</dependencies>
</project>
創建服務接口DemoService.java(需和服務提供者接口保持一致)
package cn.guyouda.dubbo.example;
public interface DemoService {
String sayHello(String name);
}
<?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="consumer-of-helloworld-app" />
<!-- 使用multicast廣播註冊中心暴露發現服務地址 -->
<!--<dubbo:registry address="multicast://224.5.6.7:1234" /> -->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>
<!-- 生成遠程服務代理,可以和本地bean一樣使用demoService -->
<dubbo:reference id="demoService" interface="cn.guyouda.dubbo.example.DemoService"/>
</beans>
啓動Consumer.java
package cn.guyouda.dubbo.example;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Consumer {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new
String[] {"classpath:consumer.xml"});
context.start();
DemoService demoService = (DemoService)context.getBean("demoService"); // 獲取遠程服務代理
String hello = demoService.sayHello("world"); // 執行遠程方法
System.out.println( hello ); // 顯示調用結果
}
}
注意:
1、建議打開兩個Eclipse分別運行兩個工程,要不然運行Dubbo-C的時候Dubbo-P會停了,導致出錯
2、新手運行Dubbo-P時極易遇見無法綁定,Dubbo端口20880佔用問題:第一次啓動就會綁定20880,再次啓動就會出現無法綁定,可以關閉相應進程;建議重新啓動的時候在provider.xml換一個端口20881,就不會出現之前的問題了。
3、一定要先啓動Dubbo-P,確定服務提供者已經正常啓動以後再啓動Dubbo-C