zoukankan      html  css  js  c++  java
  • 【dubbo】消费者Consumer搭建

    一.consumer搭建(可以web/jar)

    1.新建Maven项目,groupId:com.dubbo.consumer.demo artifactId:demo projectName:dubboo-consumer-demo

    2.新建class :com.dubbo.consumer.demo.DemoAction

    package com.dubbo.consumer.demo;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import com.dubbo.api.demo.*;
    /**
     * Created by Administrator on 17-1-7.
     */
    public class DemoAction {
        private static DemoService demoService;
        public void setDemoService(DemoService demoService) {
            this.demoService = demoService;
        }
    
        public void start() throws Exception {
            for (int i = 0; i < Integer.MAX_VALUE; i ++) {
                try {
                    String hello = demoService.sayHello("world" + i);
                    System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] " + hello);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Thread.sleep(2000);
            }
        }
    }

    3.添加配置文件spring-dubbo-consumer.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://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="dubbo-consumer-demo" ></dubbo:application>
        <dubbo:registry address="zookeeper://127.0.0.1:2181" protocol="zookeeper"></dubbo:registry>
        <dubbo:reference id="demoService" interface="com.dubbo.api.demo.DemoService" ></dubbo:reference>
    
    </beans>

    4.修改maven配置文件pom.xml,添加spring、dubbo、dubbo-api

    <dependencies>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>2.4.10</version>
            </dependency>
            <dependency>
                <groupId>com.github.sgroschupf</groupId>
                <artifactId>zkclient</artifactId>
                <version>0.1</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>3.2.16.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>3.2.16.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>3.2.16.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>com.dubbo.api.demo</groupId>
                <artifactId>dubbo-interface</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
        </dependencies>

     5.添加消费者Class DemoConsumer

    package Consumer;/*
     * Copyright 1999-2011 Alibaba Group.
     *  
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *  
     *      http://www.apache.org/licenses/LICENSE-2.0
     *  
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    
    import com.dubbo.api.demo.*;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class DemoConsumer {
        
        public static void main(String[] args) {
    
            ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("META-INFO/spring-dubbo-consumer.xml");
    
            ctx.start();
    
            DemoService demoService = (DemoService) ctx.getBean("demoService");
    
            String hello = demoService.sayHello("world" + 1);
            System.out.println("[" + hello);
    
            ctx.close();
        }
        }

     二、总结

    1.web或jar包形式发布消费者都可以

    2.工程中涉及主要配置

      A.consumer.xml 中引入API(interface)

      B.pom.xml中引入API(Interface)jar包

      C.接口实现在provider中完成,本地引入接口定义jar即可

      D.java中import api package

    
    
  • 相关阅读:
    2020毕业设计选用4412开发板,实战教程,小成果不放过
    学习嵌入式有决心4412再送免费教程
    iTOP4412开发板can测试工具使用文档
    iTOP4412Ubuntu系统源码ubuntu没有声音的解决办法
    修身养性,为人处事100条
    用X++实现有规律分割的长字符串,分别提取(如逗号隔开)
    自定义Dialog中对数组的取值
    Common实现任何Form定位查询转到当前Form种过滤符合条件的数据区域
    用X++得到当前用户是否有某Security Key的权限级别,完整Job演示
    获取table中的系统字段信息
  • 原文地址:https://www.cnblogs.com/grape1211/p/6259270.html
Copyright © 2011-2022 走看看