dubbo使用例子

來源:https://my.oschina.net/penngo/blog/495613php

 

dubbo2.4.11源碼編譯 http://my.oschina.net/penngo/blog/494838java

一樣以兩個簡單的登陸和註冊服務爲例,以前已經用thrift實現過,thrift版本的實現請參考Thrift的java和phphttp://my.oschina.net/penngo/blog/489311spring

客戶端和服務器端共用接口類服務器

登陸接口類app

package com.penngo.dubbo.service;

public interface LoginService {
	public User login(String name, String psw);

}

註冊接口類ide

package com.penngo.dubbo.service;

public interface RegisterService {
	 public User createUser(String name, String psw);
}

 

服務器端.net

登陸接口實現代理

package com.penngo.dubbo.serviceimpl;
import com.penngo.dubbo.service.LoginService;
import com.penngo.dubbo.service.User;

public class LoginServiceImpl implements LoginService{
	public User login(String name, String psw){
        User user = null;
        if(name.equals("penngo") && psw.equals("123")){
            user = new User();
            user.setId(1);
            user.setName("penngo");
        }
        return user;
    }
}

註冊接口實現code

package com.penngo.dubbo.serviceimpl;
import com.penngo.dubbo.service.RegisterService;
import com.penngo.dubbo.service.User;

public class RegisterServiceImpl implements RegisterService{
	 public User createUser(String name, String psw){
	        User user = new User();
	        user.setId(2);
	        user.setName(name);
	        user.setPassword(psw);
	        return user;
	    }
}

服務器端利用spring發佈服務dubbo-penngo-provider.xmlserver

<?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-2.5.xsd
	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
	
    
    <!-- 提供方應用信息,用於計算依賴關係 -->
    <dubbo:application name="provider-penngo-app"  />
 
    <!-- 使用zookeeper廣播註冊中心暴露服務地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />
    <!--  <dubbo:registry address="multicast://224.5.6.7:1234" />   -->
 
    <!-- 用dubbo協議在20880端口暴露服務 -->
    <dubbo:protocol name="dubbo" port="20880" />

    <!-- 和本地bean同樣實現服務 -->
	<bean id="loginService" class="com.penngo.dubbo.serviceimpl.LoginServiceImpl" />
	<bean id="registerService" class="com.penngo.dubbo.serviceimpl.RegisterServiceImpl" />
	
	<!-- 聲明須要暴露的服務接口 -->
	<dubbo:service interface="com.penngo.dubbo.service.LoginService" ref="loginService" />
	<dubbo:service interface="com.penngo.dubbo.service.RegisterService" ref="registerService" />
	
</beans>

服務啓動類,發佈服務

package com.penngo.dubbo.server;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Server {
	public static void main(String[] args) throws Exception{
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"dubbo-penngo-provider.xml"});
        context.start();
        System.in.read(); // 按任意鍵退出
	}
}

 

客戶端調用服務很簡單,只需一個xml文件和一個java調用類

客戶端利用spring配置可用遠程服務器

<?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-2.5.xsd
	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:application name="consumer-penngo-app"  />

    <dubbo:registry address="zookeeper://127.0.0.1:2181" />
    <!-- 使用multicast廣播註冊中心暴露發現服務地址 -->
    <!--   <dubbo:registry address="multicast://224.5.6.7:1234" />  -->
    
    <!-- 生成遠程服務代理, -->
	<dubbo:reference id="loginService" interface="com.penngo.dubbo.service.LoginService" />
	<dubbo:reference id="registerService" interface="com.penngo.dubbo.service.RegisterService" />
</beans>

客戶端調用

package com.penngo.dubbo.client;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.penngo.dubbo.service.LoginService;
import com.penngo.dubbo.service.RegisterService;
import com.penngo.dubbo.service.User;

public class Client {
	 public static void main(String[] args) throws Exception {
	        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"dubbo-penngo-consumer.xml"});
	        context.start();
	 
	        LoginService loginService = (LoginService)context.getBean("loginService"); // 獲取遠程服務代理
	        User user = loginService.login("penngo", "123");
	 
	        System.out.println( user.getId() + " " + user.getName()); // 顯示調用結果
	        
	        RegisterService registerService = (RegisterService)context.getBean("registerService");
	        User user2 = registerService.createUser("penngo", "123");
	        System.out.println( user2.getId() + " " + user2.getName()); // 顯示調用結果
	    }
}

 

源碼下載

相關文章
相關標籤/搜索