这里分了两个项目,一个是server(服务端),cli(客户端),用zookeeper集群做注册中心实现服务
首先需要配置的jar包
除了Spring必须需要的,还需要
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.6</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.32.Final</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
</dependency>
解释一下:
第二个是因为dubbo是底层是基于netty,需要它的依赖,
第三个是因为我没直接用zookeeper客户端,而是用的curator来操作这个版本可以小点,最新的有可能不兼容,报
KeeperErrorCode = ConnectionLoss for /dubbo
第四个zookeeper,要和服务端的zookeeper版本一致
Server(服务端)
目录结构
dubbo是通过暴露实现的接口来调用的,但是客户端也要有自己的接口,和服务端一致,但是不需要实现,实现去服务端调用返回
dubbo-server配置文件
<?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://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://dubbo.apache.org/schema/dubbo
http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 提供方应用信息,用于计算依赖关系,和客户端是不一样的 -->
<dubbo:application name="hello-world-app" />
<!-- 使用zookeeper集群注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://192.168.137.131:2181?backup=192.168.137.132:2181,192.168.137.133:2181" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="server.DemoService" ref="demoService" />
<!-- 本地实现服务的接口的实现类,和id和上面ref对应 -->
<bean id="demoService" class="server.DemoServiceImpl" />
</beans>
接口和实现类
package server;
public interface DemoService {
String getUser(String id);
}
package server;
//这不用写注解,因为配置文件中引用了实现类,等后面用注解实现再写
public class DemoServiceImpl implements DemoService{
@Override
public String getUser(String id) {
return "王一";
}
}
测试类
package dubbo.server.test;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"dubbo-server.xml"});
context.start();
System.in.read(); // 按任意键退出
}
}
cli(客户端)
dubbo-cli.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://dubbo.apache.org/schema/dubbo"
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">
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="consumer-of-helloworld-app" />
<!-- 使用zookeeper集群注册中心暴露发现服务地址 -->
<dubbo:registry address="zookeeper://192.168.137.131:2181?backup=192.168.137.132:2181,192.168.137.133:2181" />
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="demoService" interface="server.DemoService" />
</beans>
接口(不需要实现类)
package server;
public interface DemoService {
String getUser(String id);
}
测试类
package dubbo.cli.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import server.DemoService;
public class Test {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"dubbo-cli.xml"});
context.start();
DemoService demoService = (DemoService)context.getBean("demoService"); // 获取远程服务代理
String hello = demoService.getUser("1001"); // 执行远程方法
System.out.println( hello ); // 显示调用结果
}
}
结果
在zookeeper集群节点上创建成功
这样就完成基于xml配置的一个小例子了
配置文件与标签
下面的图片来自dubbo官网
配置之间的关系
标签解释
标签 | 用途 | 解释 |
---|---|---|
<dubbo:service/> | 服务配置 | 用于暴露一个服务,定义服务的元信息,一个服务可以用多个协议暴露,一个服务也可以注册到多个注册中心 |
<dubbo:reference/> | 引用配置 | 用于创建一个远程服务代理,一个引用可以指向多个注册中心 |
<dubbo:protocol/> | 协议配置 | 用于配置提供服务的协议信息,协议由提供方指定,消费方被动接受 |
<dubbo:application/> | 应用配置 | 用于配置当前应用信息,不管该应用是提供者还是消费者 |
<dubbo:module/> | 模块配置 | 用于配置当前模块信息,可选 |
<dubbo:registry/> | 注册中心配置 | 用于配置连接注册中心相关信息 |
<dubbo:monitor/> | 监控中心配置 | 用于配置连接监控中心相关信息,可选 |
<dubbo:provider/> | 提供方配置 | 当 ProtocolConfig 和 ServiceConfig 某属性没有配置时,采用此缺省值,可选 |
<dubbo:consumer/> | 消费方配置 | 当 ReferenceConfig 某属性没有配置时,采用此缺省值,可选 |
<dubbo:method/> | 方法配置 | 用于 ServiceConfig 和 ReferenceConfig 指定方法级的配置信息 |
<dubbo:argument/> | 参数配置 | 用于指定方法参数配置 |
对于上面表格中详细的配置请去schema 配置参考手册看看
不同粒子间的覆盖关系
- 方法级优先,接口级次之,全局配置再次之。
- 如果级别一样,则消费方优先,提供方次之。