dubbo

来源:互联网 发布:淘宝开店保证金30元 编辑:程序博客网 时间:2024/06/06 00:29

新项目中遇到dubbo,然后就想着学一下。

1、打开官网,把代码拷贝下来。

//Provider 类:发布服务

import org.springframework.context.support.ClassPathXmlApplicationContext;public class Provider {    public static void main(String[] args) throws Exception {        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(                new String[] {"META-INF/spring/dubbo-demo-provider.xml"});        context.start();        System.in.read(); // press any key to exit    }}
import com.alibaba.dubbo.demo.DemoService;import org.springframework.context.support.ClassPathXmlApplicationContext;//Consumer调用服务 public class Consumer {    public static void main(String[] args) throws Exception {        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(                new String[]{"META-INF/spring/dubbo-demo-consumer.xml"});        context.start();        DemoService demoService = (DemoService) context.getBean("demoService"); // obtain proxy object for remote invocation        String hello = demoService.sayHello("world"); // execute remote invocation        System.out.println(hello); // show the result    }}
//接口
package com.alibaba.dubbo.demo;public interface DemoService {String sayHello(String name);}
//实现类
package com.alibaba.dubbo.demo.provider;import com.alibaba.dubbo.demo.DemoService;public class DemoServiceImpl implements DemoService {public String sayHello(String name) {        return "Hello " + name;}}
配置文件1
<?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="demo-consumer"/>    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>    <dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService"/></beans>
配置文件2
<?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="demo-provider"/>    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>    <dubbo:protocol name="dubbo" port="20880"/>    <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"/>    <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/></beans>
先运行服务,后运行客户端就可以了。
注意:我开始写的<dubbo:registry address="multicast://234.4.5.6:1234" />报错了,
multicast是个什么东西,我也不清楚,然后看着别人的写了zookeeper。zookeeper我懂啊。
1,下载了一个zookeeper,解压,运行bin里的zkServer.cmd,报错了。
2、将conf/zoo_c****.cfg修改为zoo.cfg.
将dataDir=E:\stu\zookeeper\zookeeper-3.4.6\conf/dataDir
3、重新启动zkServer.cmd,provider,Consumer 。
好了。
另外,pom.xml里为
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>dubboio</groupId>  <artifactId>dubboio</artifactId>  <version>0.0.1-SNAPSHOT</version>  <packaging>jar</packaging>  <name>dubboio</name>  <url>http://maven.apache.org</url>  <properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>    <spring.version>3.0.0.RELEASE</spring.version>    <slf4j.version>1.7.5</slf4j.version>  </properties>  <dependencies>    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>3.8.1</version>      <scope>test</scope>    </dependency>    <dependency>    <groupId>com.alibaba</groupId>    <artifactId>dubbo</artifactId>    <version>2.5.5</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-expression</artifactId><version>${spring.version}</version></dependency><!-- log --><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.16</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>${slf4j.version}</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>${slf4j.version}</version></dependency><dependency>  <groupId>com.github.sgroschupf</groupId>  <artifactId>zkclient</artifactId>  <version>0.1</version></dependency><!--  zookeeper --><dependency>  <groupId>org.apache.zookeeper</groupId>  <artifactId>zookeeper</artifactId>  <version>3.4.6</version></dependency>  </dependencies></project>

备注:开始的时侯报:找不到META-INF/spring/dubbo-demo-provider.xml

后来放到和Provider相同的目录下就好了。



原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 孕34周隐血1十是怎么办 窦性心动过缓伴不齐怎么办 09年买的万科b怎么办 苹果5s手机打不开机怎么办 剖腹产后一年半后意外怀孕怎么办 考驾照挂了5次了怎么办 怀孕咳嗽一个月了好不了怎么办 孕妇餐后2小时血糖高怎么办 我想开网店但我不懂该怎么办 新开的淘宝店没生意怎么办 做肝胆b超喝了水怎么办 红米1s刷机失败怎么办 红米3x手机太卡怎么办 红米2a线刷失败怎么办 红米2用不了4g怎么办 小米红米3s卡顿怎么办 魅蓝4g网速很慢怎么办 lol装到c盘了会怎么办 急用钱又借不到怎么办小额信贷 花呗分期买手机额度不够怎么办 2个月婴儿脸皴了怎么办 掉头发怎么办怎样能让头发变多 11个月的宝宝大便干燥怎么办 1岁宝宝又拉又吐怎么办 怀孕八个月了不想要了怎么办 奶水不够宝宝又不喝奶粉怎么办 手机恢复出厂设置密码忘了怎么办 5s锁屏密码忘了怎么办 深圳房子卖了户口没地方迁怎么办 宝马1系130i烧机怎么办 小孩流清鼻涕怎么办最简单方法 孕3个月胎盘低置怎么办 孩子判逆不听家长的话该怎么办 香港购物超5000被海关扣怎么办 浅色衣服被深色衣服染色了怎么办 金立手机微信不能发语音怎么办 吃鸡买的账号密码邮箱忘记了怎么办 氩弧焊枪管带里进水了怎么办 绝地求生穿头盔的时候连衣帽怎么办 开车不小心把光缆线给挂断了怎么办 脚刺到了生锈钢钉没打针怎么办