dubbo-5分钟入门(看不懂你可以砍我)
1,什么是dubbo?
第一次我听到dubbo时,还误以为别人说的是 double
呢。而且感觉上念做double
可能会更直观一些。我相信一定会有人也这么想过!令人兴奋的是dubbo现在已经成为apache组织的一员了。因此,当您看到apache dubbo时,不要迷茫....因为它实际上就是阿里在国内非常流行的开源框架dubbo。
既然是入门教程,就直接上干货吧!
右边是Dubbo的官方指导文档,不过是英文的----->
《dubbo官方文档 - home》
好吧,如果你们不想看英文版的,在这里我就给大家翻译一下!
.-----------------------------------------------------------------------------------------------------------------------------------
大多数情况下,我们都是借助spring容器运行Dubbo的。既然如此,这里就给大家演示如何使用Spring配置文件配置一个Dubbo 应用吧!当然您也可以尝试使用接口配置方式来实现。
步骤1.
首先在您的电脑上创建一个文件夹: dubbo-demo:
dubbo-demo这个目录接下来我就叫它Double根目录
吧(:
mkdir dubbo-demo
cd dubbo-demo
步骤2.
Next, we are going to create 3 sub-directories under root directory:
然后,在Double根目录
下创建3个子目录:
- dubbo-demo-api: the common service api
- dubbo-demo-provider: the demo provider codes
- dubbo-demo-consumer: the demo consumer codes
就像下面这样:
mkdir dubbo-demo-api
mkdir dubbo-demo-provider
mkdir dubbo-demo-consumer
一,## Service provider(服务提供者)
步骤3.
定义服务接口
DemoService.java :
package org.apache.dubbo.demo;
public interface DemoService {
String sayHello(String name);
}
然后项目结构大概就下面这个鸟样:
.
├── dubbo-demo-api
│ ├── pom.xml
│ └── src
│ └── main
│ └── java
│ └── org
│ └── apache
│ └── dubbo
│ └── demo
│ └── DemoService.java
步骤4.
在service provider
目录下实现服务提供者接口
DemoServiceImpl.java:
package org.apache.dubbo.demo.provider;
import org.apache.dubbo.demo.DemoService;
public class DemoServiceImpl implements DemoService {
public String sayHello(String name) {
return "Hello " + name;
}
}
步骤5.
Spring xml配置式暴露服务
provider.xml:
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- provider's application name, used for tracing dependency relationship -->
<dubbo:application name="demo-provider"/>
<!-- use multicast registry center to export service -->
<dubbo:registry address="multicast://224.5.6.7:1234"/>
<!-- use dubbo protocol to export service on port 20880 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- service implementation, as same as regular local bean -->
<bean id="demoService" class="org.apache.dubbo.demo.provider.DemoServiceImpl"/>
<!-- declare the service interface to be exported -->
<dubbo:service interface="org.apache.dubbo.demo.DemoService" ref="demoService"/>
</beans>
步骤6.
配置日志组件
Configure the logging system
Dubbo 默认使用log4j , 也支持 slf4j, Apache Commons Logging, and JUL logging.
创建:log4j.properties
log4j.rootLogger=info, stdout
og4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n
步骤7:
启动服务提供者
:
Provider.java
package org.apache.dubbo.demo.provider;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Provider {
public static void main(String[] args) throws Exception {
System.setProperty("java.net.preferIPv4Stack", "true");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-provider.xml"});
context.start();
System.out.println("Provider started.");
System.in.read(); // press any key to exit
}
}
最终,项目结构就是如下的鸟样:
├── dubbo-demo-provider
│ ├── pom.xml
│ └── src
│ └── main
│ ├── java
│ │ └── org
│ │ └── apache
│ │ └── dubbo
│ │ └── demo
│ │ └── provider
│ │ ├── DemoServiceImpl.java
│ │ └── Provider.java
│ └── resources
│ ├── META-INF
│ │ └── spring
│ │ └── dubbo-demo-provider.xml
│ └── log4j.properties
二, 服务消费者
步骤8:
使用Spring 配置式添加远程服务(服务提供者接口):
consumer.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- consumer's application name, used for tracing dependency relationship (not a matching criterion),
don't set it same as provider -->
<dubbo:application name="demo-consumer"/>
<!-- use multicast registry center to discover service -->
<dubbo:registry address="multicast://224.5.6.7:1234"/>
<!-- generate proxy for the remote service, then demoService can be used in the same way as the
local regular interface -->
<dubbo:reference id="demoService" check="false" interface="org.apache.dubbo.demo.DemoService"/>
</beans>
步骤9:
启动消费者程序:
Consumer.java :
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.apache.dubbo.demo.DemoService;
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();
// Obtaining a remote service proxy
DemoService demoService = (DemoService)context.getBean("demoService");
// Executing remote methods
String hello = demoService.sayHello("world");
// Display the call result
System.out.println(hello);
}
}
步骤10:
配置日志系统
这里可参考服务提供者
在步骤6中的日志配置。
然后,项目结构就是如下的鸟样:
├── dubbo-demo-consumer
│ ├── pom.xml
│ └── src
│ └── main
│ ├── java
│ │ └── org
│ │ └── apache
│ │ └── dubbo
│ │ └── demo
│ │ └── consumer
│ │ └── Consumer.java
│ └── resources
│ ├── META-INF
│ │ └── spring
│ │ └── dubbo-demo-consumer.xml
│ └── log4j.properties
步骤11:
启动Demo
启动 服务提供者
运行 org.apache.dubbo.demo.provider.Provider
类以启动服务提供者
.
启动 服务消费者
运行 org.apache.dubbo.demo.provider.Consumer
类以启动服务消费者
. 接下来您会在控制台上看到如下输出:
Hello world
入门篇在此完结!
.--------------------------------------------------------------------------------------------------------------------------------------------
完整案例
在Github上的完整案例:
在Gitee上的完整案例:(防止墨西哥高墙
)
- 后续,会把连接添加上。
- 后续,会把连接添加上。
提示:
- 接口部分要分开打包, 并分享给服务消费者和服务提供者
- Hidden realization of service consumer (讲真,我也不知道该怎么翻译这句话!希望有人在评论里指出)
- IoC injection can also be used(这句话的字面意义貌似并不能说明问题,希望大神在评论里指出)