【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

 

转载于:https://www.cnblogs.com/grape1211/p/6259270.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值