Dubbo+zookeeper入門示例搭建
-安裝zookeeper
1.在官網上下載zookeeper安裝檔案,解壓,重新命名zookeeper-3.4.5\conf目錄下的zoo_sample.cfg為zoo.cfg
2.在zookeeper-3.4.5\bin目錄下,雙擊zkServer.cmd開啟zookeeper伺服器
-安裝dubbo
1新建maven專案,在pom檔案中新增如下依賴:引入如下三個主要的包就可以了,主要是spring,dubbo以及zkclient
<dependency>
<groupId>org.springframework</groupId >
<artifactId>spring-context</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.4.10</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions >
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.3</version>
</dependency>
新建一個介面:
public interface DemoService {
String sayHello(String name);
public List<User> getUsers();
}
實現介面:
public class DemoServiceImpl implements DemoService {
public String sayHello(String name) {
System.out.println("----------");
return "Hello " + name;
}
public List<User> getUsers() {
List<User> list = new ArrayList<User>();
User u1 = new User();
u1.setName("jack");
u1.setAge(20);
u1.setSex("男");
User u2 = new User();
u2.setName("tom");
u2.setAge(21);
u2.setSex("女");
User u3 = new User();
u3.setName("rose");
u3.setAge(19);
u3.setSex("女");
list.add(u1);
list.add(u2);
list.add(u3);
return list;
}
}
配置producter的Spring配置檔案:
<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
">
<!-- 具體的實現bean -->
<bean id="demoService" class="com.unj.dubbotest.provider.DemoServiceImpl" />
<!-- 提供方應用資訊,用於計算依賴關係 -->
<dubbo:application name="xixi_provider" />
<!-- 使用multicast廣播註冊中心暴露服務地址
<dubbo:registry address="multicast://127.0.0.1:1234" />-->
<!-- 使用zookeeper註冊中心暴露服務地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 用dubbo協議在20880埠暴露服務 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 宣告需要暴露的服務介面 -->
<dubbo:service interface="com.unj.dubbotest.provider.DemoService" ref="demoService" />
</beans>
開啟服務:
public class Producter {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationContext-producter.xml" });
context.start();
System.out.println("start");
System.in.read(); // 為保證服務一直開著,利用輸入流的阻塞來模擬
}
}
配置consumer的spring配置檔案
<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="hehe_consumer" />
<!-- 使用zookeeper註冊中心暴露服務地址 -->
<!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 生成遠端服務代理,可以像使用本地bean一樣使用demoService -->
<dubbo:reference id="demoService"
interface="com.unj.dubbotest.provider.DemoService" />
</beans>
consumer來使用服務
public class Consumer {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationContext-consumer.xml" });
context.start();
DemoService demoService = (DemoService) context.getBean("demoService"); //
String hello = demoService.sayHello("tom"); // ִ
System.out.println(hello); //
//
List<User> list = demoService.getUsers();
if (list != null && list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
// System.out.println(demoService.hehe());
System.in.read();
}
}
使用的user類如下:
public class User implements Serializable{
private String name;
private String sex;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
先啟動producter,再啟動consumer執行結果如下:
遠端呼叫失敗問題:
Caused by: com.alibaba.dubbo.rpc.RpcException: Failed to invoke the method subscribe in the service com.alibab
a.dubbo.registry.RegistryService. Tried 3 times of the providers[172.168.1.167:2181] (1/1) from the registry
172.168.1.167:2181 on the consumer 169.254.249.102 using the dubbo version2.4.9. Last error is: Invoke remote
method timeout.
解決方法
這個是由於dubbo介面中的的傳輸物件沒有被序列化而導致的,只需要要檢查dubbo介面中引數中的實體類實現序列化(implements Serializable)就可以解決這一個異常.