• 【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

    
    
  • 相关阅读:
    开源控件PullToRefreshGridView的使用(三)
    JS中的运算符 以及变量和输入输出
    HTML中表格
    JS中 事件冒泡与事件捕获
    JS中的循环结构
    UML类图详解
    一步一步生成图片水印
    快速生成缩略图
    ==和Equal()的区别
    屌丝程序人生(下)
  • 原文地址:https://www.cnblogs.com/grape1211/p/6259270.html
Copyright © 2020-2023  润新知