dubbo學習之Hello world

        如今企業中使用dubbo的愈來愈多,今天就簡單的學習一下dubbo,寫了一個hello world,教程僅供入門,如要深刻學習請上官網spring

 

 

服務提供方:

       首先將提供方和消費方都引入jar包,若是使用的是maven管理項目,能夠直接加入dubbo的配置apache

<!—dubbo start -->

<dependency> 
    <groupId>com.alibaba</groupId> 
    <artifactId>dubbo</artifactId> 
    <version>2.5.3</version> 
    <exclusions> 
        <exclusion> 
            <groupId>org.springframework</groupId> 
            <artifactId>spring</artifactId> 
        </exclusion> 
    </exclusions> 
</dependency>

<!—dubbo end  -->

<!-- zookeeper start --> 
<dependency> 
    <groupId>org.apache.zookeeper</groupId> 
    <artifactId>zookeeper</artifactId> 
    <version></version> 
</dependency> 
<dependency> 
    <groupId>com.101tec</groupId> 
    <artifactId>zkclient</artifactId> 
    <version>${zkclient_version}</version> 
</dependency> 
<!-- zookeeper end –>

 

聲明接口:app

public interface UserInfoService { 
     
    String sayHello(String name);

}

 

 

實現接口:maven

public class UserInfoServiceImpl implements UserInfoService{

    public String sayHello(String name) { 
        return name + " Hello !"; 
    }

}

 

 

配置applicationContext.xml文件:ide

<?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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd 
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <dubbo:application name="hello-world-app"/> 
    <!-- 註冊地址 --> 
    <dubbo:registry address="zookeeper://192.168.0.123:2181" ></dubbo:registry> 
    
    <dubbo:protocol name="dubbo" port="20880"></dubbo:protocol> 
    
    <dubbo:service ref="userInfoService" interface="com.zhiyi.service.UserInfoService" /> 
    <!-- designate implementation --> 
    <bean id="userInfoService" class="com.zhiyi.service.impl.UserInfoServiceImpl" />

</beans> 

 

 

程序啓動入口:學習

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.google.common.util.concurrent.AbstractIdleService;

public class BootStart extends AbstractIdleService{

    ClassPathXmlApplicationContext context = null; 
    
    public static void main(String[] args) { 
        BootStart bootStart = new BootStart(); 
        bootStart.startAsync(); 
        try { 
            Object lock = new Object(); 
            synchronized (lock) { 
                while(true){ 
                    lock.wait(); 
                } 
            } 
        } catch (Exception e) { 
            e.printStackTrace(); 
        }

    } 
    
    @Override 
    protected void shutDown() throws Exception { 
        if( context != null){ 
            context.stop(); 
        } 
    }

    @Override 
    protected void startUp() throws Exception { 
        String configure = "applicationContext.xml"; 
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(configure); 
        
        String[] beans = context.getBeanDefinitionNames(); 
        
        for( String bean : beans){ 
            System.out.println("beanName:"+bean); 
        } 
        context.start(); 
        context.registerShutdownHook(); 
        System.out.println("provider is start!"); 
        
    } 
}

 

 

服務消費方:

配置applicationContext.xmlgoogle

注意:在消費方須要引入接口spa

<?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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd 
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <dubbo:application name="consumer-of-hello-world"/>

    <dubbo:registry address="zookeeper://192.168.0.123:2181"></dubbo:registry>

    <dubbo:reference id="userInfoService" interface="com.zhiyi.service.UserInfoService"></dubbo:reference> 
   

</beans> 

 

 

服務消費方入口:code

import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zhiyi.service.UserInfoService;

public class BootStart {

    public static void main(String[] args) { 
        BootStart bootStart = new BootStart(); 
        bootStart.start(); 
    } 
    
    public void start(){ 
        String applicationConfig = "applicationContext.xml"; 
        ApplicationContext context = new ClassPathXmlApplicationContext(applicationConfig); 
        String[] beans = context.getBeanDefinitionNames(); 
        for(String bean : beans){ 
            System.out.println("beanName:"+bean); 
        } 
        
        UserInfoService userInfoService = (UserInfoService) context.getBean("userInfoService"); 
        System.out.println(userInfoService.sayHello("zhangsan")); 
    } 
}

 

好了,就是這麼簡單,dubbo的hello world就完成了,歡迎大神拍磚~xml

相關文章
相關標籤/搜索