dubbo学习之Hello world


        现在企业中使用dubbo的越来越多,今天就简单的学习一下dubbo,写了一个hello world,教程仅供入门,如要深入学习请上官网

 

 

服务提供方:

       首先将提供方和消费方都引入jar包,如果使用的是maven管理项目,可以直接加入dubbo的配置

—dubbo start -->

<dependency> 
    <groupId>com.alibabagroupId> 
    <artifactId>dubboartifactId> 
    <version>2.5.3version> 
    <exclusions> 
        <exclusion> 
            <groupId>org.springframeworkgroupId> 
            <artifactId>springartifactId> 
        exclusion> 
    exclusions> 
dependency>

—dubbo end  -->

 
<dependency> 
    <groupId>org.apache.zookeepergroupId> 
    <artifactId>zookeeperartifactId> 
    <version>version> 
dependency> 
<dependency> 
    <groupId>com.101tecgroupId> 
    <artifactId>zkclientartifactId> 
    <version>${zkclient_version}version> 
dependency> 
 
    <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" /> 
     
    <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.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://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> 

 

服务消费方入口:

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就完成了,欢迎大神拍砖~