zoukankan      html  css  js  c++  java
  • dubbo学习之Hello world

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

     

     

    服务提供方:

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

    <!—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 –>

    声明接口:

    public interface UserInfoService { 
         
        String sayHello(String name);
    
    }

    实现接口:

    public class UserInfoServiceImpl implements UserInfoService{
    
        public String sayHello(String name) { 
            return name + " Hello !"; 
        }
    
    }

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

  • 相关阅读:
    一些好用的小工具
    App随机测试之Monkey和Maxim
    Appium如何自动判断浏览器驱动
    最简单的一个Appium测试Android Web APP的代码demo
    pytest使用allure生成测试报告的2种命令
    使用order by in()将快到期的数据排到最上方。
    关于jQuery click()方法重复提交的问题
    关于List removeAll失效的问题
    根据年和月计算对应的天数
    jquery通过监听输入框实现值的自动计算
  • 原文地址:https://www.cnblogs.com/tangkai/p/4755752.html
Copyright © 2011-2022 走看看