dubbo入门官方案例学习

来源:互联网 发布:无线虚能矩阵 编辑:程序博客网 时间:2024/05/22 00:26
概述:

凡是先入门,而然后破门而出,不深究,所为何?难矣难矣,简单来说就是从入门到放弃。

dubbo官网

1、画一画dubbo架构粗略图


这个框架,让我想起,好像类似QQ添加特别关心功能。只要特别关心的人有最新动态你都会第一时间通知到。所有信息首先会在一个地方报个到,然后进行转发通知特定对象。

还是来看看dubbo过程(个人理解)

  • 0、启动服务,做好向外提供服务的准备。
  • 1、启动服务之后,会异步注册到注册中心(也就是向外公开,有这么个服务,需要的可以订阅)
  • 2、消费者它需要订阅服务(它也会去注册中心找是否存在我所有需要的服务)
  • 3、刚刚好存在这个服务,它会得到注册中心通知,说有这样服务。
  • 4、消费者就开始调用服务接口。
  • 5、这个调用行为会异步到监控中心报道。

2、开始实现HelloWorld案例

2.1、先决条件

JDK:1.6及以上 (笔者1.8)

Maven:版本3及以上 (笔者3.5)

采用Spring进行玩耍

2.2、工程结构


3个类1个接口,两个配置文件

DemoService.java 服务接口

DemoServiceImpl.java : 服务接口实现

Provider.java:启动服务类

Comsumer.java : 消费调用类

spring.xml : 服务配置文件

springConsumer.xml: 消费配置文件


2.3、引入dubbo 依赖和Spring相关依赖

<dependency>    <groupId>com.alibaba</groupId>    <artifactId>dubbo</artifactId>    <version>2.5.5</version></dependency>

2.4、先整服务相关

DemoService.java 接口

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

DemoServiceImpl.java 实现类

package com.jack.dubbo.demo.provider;import com.jack.dubbo.demo.DemoService;public class DemoServiceImpl implements DemoService{public String sayHello(String name) {return "Hello   " + name;}}
Provider.java

package com.jack.dubbo.demo.provider;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Provider {public static void main(String[] args) throws Exception {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"spring.xml"});context.start();System.in.read();}}
spring.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:context="http://www.springframework.org/schema/context"xmlns:c="http://www.springframework.org/schema/c" xmlns:p="http://www.springframework.org/schema/p"xmlns:util="http://www.springframework.org/schema/util"xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util.xsdhttp://code.alibabatech.com/schema/dubbo    http://code.alibabatech.com/schema/dubbo/dubbo.xsd"><dubbo:application name="demo-provider"/><dubbo:registry address="multicast://224.5.6.7:1234"/><dubbo:protocol name="dubbo" port="20880"/><dubbo:service interface="com.jack.dubbo.demo.DemoService" ref="demoService"/><bean id="demoService" class="com.jack.dubbo.demo.provider.DemoServiceImpl"/></beans>

总结:

1、引入dubbo命名空间 

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
2、表示应用的名称为demo-provider
<dubbo:application name="demo-provider"/>
3、表示注册中心地址为224.5.6.7:1234 (multicast 多广播)
 <dubbo:registry address="multicast://224.5.6.7:1234"/>
4、协议名称为dubbo, 占用服务端口为20880
<dubbo:protocol name="dubbo" port="20880"/>
5、表示具体提供的服务,这样引用的是接口,ref是对应实现类的一个实例
<dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"/>
6、让Spring实例一个bean

<bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/>


运行Provider.java的main方法启动服务(截取部分日志):



2.5、整一下消费者

springConsumer.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:context="http://www.springframework.org/schema/context"xmlns:c="http://www.springframework.org/schema/c" xmlns:p="http://www.springframework.org/schema/p"xmlns:util="http://www.springframework.org/schema/util"xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util.xsdhttp://code.alibabatech.com/schema/dubbo    http://code.alibabatech.com/schema/dubbo/dubbo.xsd"><dubbo:application name="demo-consumer"/><dubbo:registry address="multicast://224.5.6.7:1234"/><dubbo:reference id="demoService" interface="com.jack.dubbo.demo.DemoService"/></beans>

总结(稍微简单点,做服务不容易):

1、应用名为:demo-consumer

<dubbo:application name="demo-consumer"/>
2、到注册中心去订阅(注意这个服务提供者消费者注册 同一个服务中心)

<dubbo:registry address="multicast://224.5.6.7:1234"/>
3、找服务接口,为com.jack.dubbo.demo.DemoService
<dubbo:reference id="demoService" interface="com.jack.dubbo.demo.DemoService"/>

最后一个文件:Comsumer.java

package com.jack.dubbo.demo;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Consumer {public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"springConsumer.xml"});context.start();DemoService demoService = (DemoService) context.getBean("demoService");String hello = demoService.sayHello("world");System.out.println(hello);}}

运行Comsumer.java就可以看到结果



已经算是运行一个dubbo服务。。。

原创粉丝点击