前言
今天小编给大家带来dubbo的简单介绍,也是dubbo第一篇。好废话不多说开始新的征程。
Dubbo概要
DUBBO是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,是阿里巴巴SOA服务化治理方案的核心框架。
Dubbo架构图
流程说明
- Provider(提供者)绑定指定端⼝并启动服务
- 提供者连接注册中心,并将本机IP、端⼝、应⽤信息和提供服务信息发送⾄注册中心存储
- Consumer(消费者),连接注册中心 ,并发送应⽤信息、所需要服务信息⾄注册中心
- 注册中心根据消费者所求服务信息匹配对应的提供者列表发送⾄Consumer 应用缓存。
- Consumer 在发起远程调⽤时基于缓存的消费者列表择其⼀发起调用。
- Provider 状态变更会实时通知注册中心、在由注册中心实时推送⾄Consumer
代码示例
简单示例
public class DubboRemoteTest {
@Test
public void clientTest(){
ApplicationConfig app = new ApplicationConfig("dubbo-client");
ReferenceConfig<IWhateverService> referenceConfig = new ReferenceConfig<>();
referenceConfig.setApplication(app);
referenceConfig.setInterface(IWhateverService.class);
referenceConfig.setUrl("dubbo://127.0.0.1:8080/");
IWhateverService whateverService = referenceConfig.get();
String serviceMsg = whateverService.printParam("hello world");
System.out.println(serviceMsg);
}
@Test
public void openService() throws IOException {
//服务配置
ServiceConfig<IWhateverService> serviceConfig = new ServiceConfig<>();
ApplicationConfig app = new ApplicationConfig("dubbo-server");
//应用
serviceConfig.setApplication(app);
//协议
ProtocolConfig protocolConfig = new ProtocolConfig();
protocolConfig.setName("dubbo");
protocolConfig.setPort(8080);
serviceConfig.setProtocol(protocolConfig);
RegistryConfig registry = new RegistryConfig("multicast://224.1.1.1:2223");
serviceConfig.setRegistry(registry);
//设置接口,设置接口所对应的实体类
serviceConfig.setInterface(IWhateverService.class);
serviceConfig.setRef(new WhateverServiceImpl());
serviceConfig.export();
System.out.println("服务已启动");
//不让服务停止
int read = System.in.read();
}
}
public interface IWhateverService {
String printParam(String msg);
}
public class WhateverServiceImpl implements IWhateverService {
@Override
public String printParam(String msg) {
System.out.println(msg);
return "accept param success";
}
}
打印结果
//服务端打印
服务已启动
hello world
//客户端打印
accept param success
以上是简单示例,接下来小编写一个简单的集群
public class DubboServiceTest {
public IWhateverService getClient() {
ApplicationConfig app = new ApplicationConfig("dubbo-client");
ReferenceConfig<IWhateverService> referenceConfig = new ReferenceConfig<>();
referenceConfig.setApplication(app);
referenceConfig.setInterface(IWhateverService.class);
RegistryConfig registry = new RegistryConfig("multicast://224.1.1.1:2223");
referenceConfig.setRegistry(registry);
// referenceConfig.setUrl("dubbo://127.0.0.1:8080/");
return referenceConfig.get();
}
public static void main(String[] args) throws IOException {
IWhateverService client = new DubboServiceTest().getClient();
while (System.in.read() != 'q') {
System.out.println(client.printParam("ok"));
}
}
// 开启服务1
@Test
public void openServer1() throws IOException {
openService(12345);
System.in.read();
}
@Test
public void openServer2() throws IOException {
openService(23456);
System.in.read();
}
@Test
public void openServer3() throws IOException {
openService(34567);
System.in.read();
}
public void openService(int port) throws IOException {
//服务配置
ServiceConfig<IWhateverService> serviceConfig = new ServiceConfig<>();
ApplicationConfig app = new ApplicationConfig("dubbo-server");
//应用
serviceConfig.setApplication(app);
//协议
ProtocolConfig protocolConfig = new ProtocolConfig();
protocolConfig.setName("dubbo");
protocolConfig.setPort(port);
serviceConfig.setProtocol(protocolConfig);
RegistryConfig registry = new RegistryConfig("multicast://224.1.1.1:2223");
serviceConfig.setRegistry(registry);
//设置接口,设置接口所对应的实体类
serviceConfig.setInterface(IWhateverService.class);
serviceConfig.setRef(new WhateverServiceImpl(port));
serviceConfig.export();
System.out.println(port + "服务已启动");
}
}
修改实现类
public class WhateverServiceImpl implements IWhateverService {
private Integer port;
public WhateverServiceImpl(Integer port) {
this.port = port;
}
@Override
public String printParam(String msg) {
System.out.println(msg);
return "accept param success port:" + port;
}
}
调用并查看打印结果
//客户端调用
54
accept param success port:34567
accept param success port:34567
accept param success port:12345
总结
以上就是小编今天为大家带来的dubbo文章,非常简单,而且我们在实际开发中不可能是这么写代码的,只是让大家了解一下dubbo服务需要哪些参数,他主要有服务配置,应用,协议,以及接口组成。调用段也就相当简单了。其实聪明的小伙伴应该已经发现他真正的服务发现以及接口注入使用动态代理实现的。当然上面同样没使用我们dubbo常用的注册中心zookeeper,使用 new RegistryConfig(“multicast://224.1.1.1:2223”),大家可以自己学习一下哦。后续为大家奉上。好今天就讲到这儿。谢谢大家的浏览
感谢与参考
感谢源码阅读网鲁班大叔讲解。