博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
dubbo学习--从demo开始:hello world(3)
阅读量:5826 次
发布时间:2019-06-18

本文共 7776 字,大约阅读时间需要 25 分钟。

hot3.png

dubbo学习--从demo开始:hello world(3)

标签(空格分隔): dubbo demo


[TOC]

0. 概要

在上一篇文章中,我们已经把dubbo项目转为gradle构建工具构建并且导入到eclipse中了。本篇我们从源码的demo开始,搭建一个可以工作的环境。然后在下回分析demo源码,开始剖析dubbo框架。

1. 定义服务接口

dubbo-demo-api:该模组定义了一个服务接口:DemoService 功能为:sayHello

DemoService.java

package com.alibaba.dubbo.demo;public interface DemoService {	String sayHello(String name);}

上述服务接口需要单独打包,在服务提供端和消费者端共享

2. 实现服务接口

2.1. 在服务提供方实现服务接口

DemoServiceImpl.java

package com.alibaba.dubbo.demo.provider;import java.text.SimpleDateFormat;import java.util.Date;import com.alibaba.dubbo.demo.DemoService;import com.alibaba.dubbo.rpc.RpcContext;public class DemoServiceImpl implements DemoService {    public String sayHello(String name) {        System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());        return "Hello " + name + ", response form provider: " + RpcContext.getContext().getLocalAddress();    }    }

2.2. 暴露服务

dubbo-demo-provider.xml

2.3. Demo服务端启动

使用dubbu的container工具类Main启动服务提供方:

DemoProvider.java

package com.alibaba.dubbo.demo.provider;public class DemoProvider {	public static void main(String[] args) {        com.alibaba.dubbo.container.Main.main(args);	}}

dubbo配置文件如下:

dubbo.properties

dubbo.container=log4j,springdubbo.application.name=demo-providerdubbo.application.owner=williamdubbo.registry.address=multicast://224.5.6.7:1234dubbo.protocol.name=dubbodubbo.protocol.port=20880dubbo.service.loadbalance=roundrobin

3. 消费者端使用服务

3.1. 编写服务消费者

DemoAction.java

package com.alibaba.dubbo.demo.consumer;import java.text.SimpleDateFormat;import java.util.Date;import com.alibaba.dubbo.demo.DemoService;public class DemoAction {        private DemoService demoService;    public void setDemoService(DemoService demoService) {        this.demoService = demoService;    }	public void start() throws Exception {	    // 循环调用sayHello服务        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.2. 引用远程服务

dubbo-demo-consumer.xml

3.3. Demo消费端启动

使用dubbu的container工具类Main启动服务提供方:

DemoConsumer.java

package com.alibaba.dubbo.demo.consumer;public class DemoConsumer {		public static void main(String[] args) {	    com.alibaba.dubbo.container.Main.main(args);	}}

dubbo配置文件如下:

dubbo.properties

dubbo.container=log4j,springdubbo.application.name=demo-consumerdubbo.application.owner=dubbo.registry.address=multicast://224.5.6.7:1234

4. 启动Demo

4.1. 先启动服务端,再启动消费端

  • 执行DemoProvider的main方法,弹出如下提示: 执行DemoProvider异常

  • 不管,确定执行,console日志如下:

Caused by: java.lang.UnsupportedClassVersionError: Bad version number in .class file	at java.lang.ClassLoader.defineClass1(Native Method)	at java.lang.ClassLoader.defineClass(ClassLoader.java:621)	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)	at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)	at java.net.URLClassLoader.access$100(URLClassLoader.java:56)	at java.net.URLClassLoader$1.run(URLClassLoader.java:195)	at java.security.AccessController.doPrivileged(Native Method)	at java.net.URLClassLoader.findClass(URLClassLoader.java:188)	at java.lang.ClassLoader.loadClass(ClassLoader.java:307)	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)	at java.lang.ClassLoader.loadClass(ClassLoader.java:252)	at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)	at java.lang.Class.forName0(Native Method)	at java.lang.Class.forName(Class.java:242)	at com.alibaba.dubbo.common.extension.ExtensionLoader.loadFile(ExtensionLoader.java:627)	... 32 more

纳尼~!,异常了!什么情况。再执行下消费端试下。

  • 执行DemoConsumer的main方法,弹出如下提示: 执行DemoConsumer异常
  • 不管,确定执行,console日志如下:
Caused by: java.lang.UnsupportedClassVersionError: Bad version number in .class file	at java.lang.ClassLoader.defineClass1(Native Method)	at java.lang.ClassLoader.defineClass(ClassLoader.java:621)	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)	at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)	at java.net.URLClassLoader.access$100(URLClassLoader.java:56)	at java.net.URLClassLoader$1.run(URLClassLoader.java:195)	at java.security.AccessController.doPrivileged(Native Method)	at java.net.URLClassLoader.findClass(URLClassLoader.java:188)	at java.lang.ClassLoader.loadClass(ClassLoader.java:307)	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)	at java.lang.ClassLoader.loadClass(ClassLoader.java:252)	at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)	at java.lang.Class.forName0(Native Method)	at java.lang.Class.forName(Class.java:242)	at com.alibaba.dubbo.common.extension.ExtensionLoader.loadFile(ExtensionLoader.java:627)	... 42 more

还是异常···!! 什么情况!

4.2. Demo异常解决

看上面的异常是,编译后的class文件和jre不兼容 看官方文档说的依赖为:

dubbo必须依赖

查看具体的模组代码,发现有报错。

  • dubbo-common中PojoUtils.java有报错,找不到ConcurrentSkipListMap,该类在JDK5中不存在
  • dubbo-admin、dubbo-rpc-thift、dubbo-remoting-grizzly模组中都有类似错误
  • 修改dubbo根目录的构建脚本文件build.gradle中的内容:
subprojects {	apply plugin: 'java'	sourceCompatibility = 1.5	targetCompatibility = 1.5		tasks.withType(JavaCompile) { options.encoding = 'UTF-8' }	dependencies {		testCompile group: 'junit', name: 'junit', version:'4.10'		testCompile group: 'org.easymock', name: 'easymock', version:'3.0'		testCompile group: 'org.easymock', name: 'easymockclassextension', version:'3.0'		testCompile group: 'com.googlecode.jmockit', name: 'jmockit', version:'0.999.8'	}}
  • 改为如下:
subprojects {	apply plugin: 'java'	sourceCompatibility = 1.6  //修改此处	targetCompatibility = 1.6  //修改此处		tasks.withType(JavaCompile) { options.encoding = 'UTF-8' }	dependencies {		testCompile group: 'junit', name: 'junit', version:'4.10'		testCompile group: 'org.easymock', name: 'easymock', version:'3.0'		testCompile group: 'org.easymock', name: 'easymockclassextension', version:'3.0'		testCompile group: 'com.googlecode.jmockit', name: 'jmockit', version:'0.999.8'	}}
  • refresh gradle project等待执行结束,项目中就没有错误了。

4.3. 再次运行Demo

4.3.1. 先服务提供端后消费端

  • 执行DemoProvider的main方法后:
Dubbo service server started!
  • 执行DemoConsumer的main方法后,Consumer:
[21:19:18] Hello world0, response form provider: 192.168.56.1:20880[21:19:20] Hello world1, response form provider: 192.168.56.1:20880
  • 执行DemoConsumer的main方法后,Provider:
[21:19:18] Hello world0, response form provider: 192.168.56.1:20880[21:19:20] Hello world1, response form provider: 192.168.56.1:20880

4.3.2. 先消费端后服务端

  • 执行DemoConsumer的main方法后:
Caused by: java.lang.IllegalStateException: Failed to check the status of the service com.alibaba.dubbo.demo.DemoService. No provider available for the service com.alibaba.dubbo.demo.DemoService from the url multicast://224.5.6.7:1234/com.alibaba.dubbo.registry.RegistryService?anyhost=true&application=demo-consumer&check=false&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&loadbalance=roundrobin&methods=sayHello&owner=william&pid=7232&side=consumer&timestamp=1489411728629 to the consumer 192.168.56.1 use dubbo version 2.0.0	at com.alibaba.dubbo.config.ReferenceConfig.createProxy(ReferenceConfig.java:420)	at com.alibaba.dubbo.config.ReferenceConfig.init(ReferenceConfig.java:300)	at com.alibaba.dubbo.config.ReferenceConfig.get(ReferenceConfig.java:138)	at com.alibaba.dubbo.config.spring.ReferenceBean.getObject(ReferenceBean.java:65)	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:166)	... 22 more

所以multicast方式不能先启动消费者。

至此dubbo自带的multicast方式的demo就演示完毕了,接下来一章我们来看下demo源码中用到的一些东西。

转载于:https://my.oschina.net/intellitor/blog/857906

你可能感兴趣的文章
紫书 习题8-11 UVa 1615 (区间选点问题)
查看>>
asp.net mvc学习(Vs技巧与Httpcontext)
查看>>
float数据在内存中是怎么存储的
查看>>
dedecms 修改标题长度可以修改数据库
查看>>
Matplotlib学习---用matplotlib画直方图/密度图(histogram, density plot)
查看>>
MySQL案列之主从复制出错问题以及pt-slave-restart工具的使用
查看>>
linux 查看剩余内存数
查看>>
测试人员容易遗漏的隐藏缺陷
查看>>
maven+SpringMVC搭建RESTful后端服务框架
查看>>
一本书的摘录
查看>>
重排序(转载)
查看>>
python+selenium之字符串切割操作
查看>>
串结构练习——字符串匹配
查看>>
linux下输入密码不回显
查看>>
《构建之法》读书笔记
查看>>
拿下阿里、头条、滴滴的offer后谈谈面试经验---动身前看一看
查看>>
android开发(49) android 使用 CollapsingToolbarLayout ,可折叠的顶部导航栏
查看>>
【ERP】如何在多行数据块中实现仅能勾选唯一的主联系人
查看>>
Oracle 数据库优化的R方法(Method R)
查看>>
CentOS最小化安装系统开启网卡
查看>>