架构
转自官网。
节点角色说明
- Provider:暴露服务的服务提供方
- Consumer:调用远程服务的服务消费方
- Registry:服务注册与发现的注册中心
- Monitor:统计服务的调用次数和调用时间的监控中心
- Container:服务运行容器
调用关系说明
- 服务容器负责启动,加载,运行服务提供者。
- 服务提供者在启动时,向注册中心注册自己提供的服务。
- 服务消费者在启动时,向注册中心订阅自己所需的服务。
- 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
- 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
- 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
实例
整体结构
dubbo-demo
pom
dubbo
com.learn
1.0-SNAPSHOT
4.0.0
dubbo-demo
pom
dubbo-demo-interface
dubbo-demo-xml
2.7.4.1
org.apache.dubbo
dubbo
${dubbo.version}
org.apache.dubbo
dubbo-dependencies-zookeeper
${dubbo.version}
pom
dubbo-demo-interface
pom
dubbo-demo
com.learn
1.0-SNAPSHOT
4.0.0
dubbo-demo-interface
HelloService
public interface HelloService {
String sayHello(String name);
}
dubbo-demo-xml-provider
暴露服务的服务提供方。
pom
dubbo-demo-xml
com.learn
1.0-SNAPSHOT
4.0.0
dubbo-demo-xml-provider
1.7.25
org.apache.dubbo
dubbo
org.apache.dubbo
dubbo-dependencies-zookeeper
pom
com.learn
dubbo-demo-interface
1.0-SNAPSHOT
Application
public class Application {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-provider.xml");
context.start();
System.in.read();
}
}
HelloServiceImpl,服务方的实现
@Service("helloService")
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return "Hello " + name;
}
}
dubbo-provider.xml
- application:应用名称,建议和项目一致
- protocol:服务提供者协议配置
- registry:注册中心配置,由于一直报zookeeper连接不上,所以这边把超时时间设置长了
- service:服务提供者暴露服务配置,把HelloService暴露出去。
dubbo.properties
dubbo.application.qos.port=22222
配置这个,是因为qos-server启动的时候,端口会占用。qos是dubbo的在线运维命令,dubbo2.5.8新版本重构了telnet模块,提供了新的telnet命令支持。
log4j.properties
###set log levels###
log4j.rootLogger=info, stdout
###output to the console###
log4j.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
dubbo-demo-xml-consumer
Application,消费方,远程调用服务方的方法。
public class Application {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-consumer.xml");
context.start();
HelloService helloService = context.getBean("helloService", HelloService.class);
String result = helloService.sayHello("张三");
System.out.println(result);
}
}
dubbo-consumer.xml
- referenc:服务消费者引用服务配置
dubbo.properties
dubbo.application.qos.port=33333
log4j.properties同上
运行
先看看zookeeper节点
[zk: localhost:2181(CONNECTED) 5] ls /
[zookeeper]
运行提供方的application,可以看到zookeeper有多个节点了
[zk: localhost:2181(CONNECTED) 2] ls /ddubbo/com.learn.dubbo.demo.HelloService
[configurators, providers]
查看providers的信息:
dubbo%3A%2F%2F192.168.102.186%3A20881%2Fcom.learn.dubbo.demo.HelloService%3Fanyhost%3Dtrue%26application%3Ddubbo-provider%26bean.name%3Dcom.learn.dubbo.demo.HelloService%26deprecated%3Dfalse%26dubbo%3D2.0.2%26dynamic%3Dtrue%26generic%3Dfalse%26interface%3Dcom.learn.dubbo.demo.HelloService%26methods%3DsayHello%26pid%3D9692%26release%3D2.7.4.1%26side%3Dprovider%26timestamp%3D1577178472840
可以看到暴露了当前服务器的地址
dubbo%3A%2F%2F192.168.102.186%3A20881%2Fcom.learn.dubbo.demo.HelloService
运行消费方的application,控制台输出了信息
zookeeper的节点,多了consumers和routers
[configurators, consumers, providers, routers]