【dubbo】消费者Consumer搭建
生活随笔
收集整理的這篇文章主要介紹了
【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
總結
以上是生活随笔為你收集整理的【dubbo】消费者Consumer搭建的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python处理mysql数据结构_py
- 下一篇: bzoj3238 [Ahoi2013]差