一、前言:
本篇文章,主要带没有接触过dubbo框架的同学,学习一下dubbo的入门,正所谓万事开头难,先帮大家揭开dubbo的面纱。
DUBBO是一个分布式微服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,是阿里巴巴SOA服务化治理方案的核心框架。从服务模型的角度来看,Dubbo采用的是一种非常简单的模型,要么是提供方提供服务,要么是消费方消费服务,所以基于这一点可以抽象出服务提供方(Provider)和服务消费方(Consumer)两个角色。
dubbo的官方入口: http://dubbo.apache.org/zh-cn/docs/user/quick-start.html,提供了很好的帮助说明,接下来,先给大家提供一些简单的demo。
二、代码分享:
接下来,写一个服务提供方,客服端代码,注册中心采用zookeeper
服务端API:
//编写服务接口,将服务接口打成jar包,供服务端,客户端使用:
public interface ILoginService {
String login(String name,String pwd);
}
服务端:
//服务端的代码:
package org.example;
/**
* @program: dubbo-server-sample
* @description
* @author: 大龄程序猿
* @create: 2020-08-09 17:43
**/
public class LoginServiceImpl implements ILoginService {
@Override
public String login(String name, String pwd) {
if(name.equals("admin")&& pwd.equals("admin"))
{
return "SUCCESS";
}else
{
return "FAILED";
}
}
}
//启动程序
package org.example;
import org.apache.dubbo.container.Main;
public class App
{
public static void main( String[] args )
{
Main.main(args);
}
}
//服务端的配置
在resources目录下 新建META-INF/spring目录
在该目录下面新建application.xml文件:
服务端启动日志:
2020-08-09 23:32:40 [ main:4871 ] - [ DEBUG ] -Dio.netty.noPreferDirect: false
2020-08-09 23:32:40 [ main:4871 ] - [ DEBUG ] -Dio.netty.maxDirectMemory: 891289600 bytes
2020-08-09 23:32:40 [ main:4871 ] - [ DEBUG ] -Dio.netty.uninitializedArrayAllocationThreshold: -1
2020-08-09 23:32:40 [ main:4872 ] - [ DEBUG ] java.nio.ByteBuffer.cleaner(): available
2020-08-09 23:32:40 [ main:4884 ] - [ DEBUG ] org.jctools-core.MpscChunkedArrayQueue: available
2020-08-09 23:32:40 [ main:4952 ] - [ DEBUG ] -Dio.netty.processId: 6080 (auto-detected)
2020-08-09 23:32:40 [ main:4955 ] - [ DEBUG ] -Djava.net.preferIPv4Stack: false
2020-08-09 23:32:40 [ main:4956 ] - [ DEBUG ] -Djava.net.preferIPv6Addresses: false
2020-08-09 23:32:40 [ main:5031 ] - [ DEBUG ] Loopback interface: lo (Software Loopback Interface 1, 127.0.0.1)
2020-08-09 23:32:40 [ main:5032 ] - [ DEBUG ] Failed to get SOMAXCONN from sysctl and file \proc\sys\net\core\somaxconn. Default: 200
2020-08-09 23:32:40 [ main:5131 ] - [ DEBUG ] -Dio.netty.machineId: 20:68:9d:ff:fe:1b:59:34 (auto-detected)
2020-08-09 23:32:40 [ main:5140 ] - [ DEBUG ] -Dio.netty.threadLocalMap.stringBuilder.initialSize: 1024
2020-08-09 23:32:40 [ main:5141 ] - [ DEBUG ] -Dio.netty.threadLocalMap.stringBuilder.maxSize: 4096
2020-08-09 23:32:40 [ main:5156 ] - [ DEBUG ] -Dio.netty.leakDetection.level: simple
2020-08-09 23:32:40 [ main:5157 ] - [ DEBUG ] -Dio.netty.leakDetection.targetRecords: 4
2020-08-09 23:32:40 [ main:5195 ] - [ DEBUG ] -Dio.netty.allocator.numHeapArenas: 8
2020-08-09 23:32:40 [ main:5195 ] - [ DEBUG ] -Dio.netty.allocator.numDirectArenas: 8
2020-08-09 23:32:40 [ main:5195 ] - [ DEBUG ] -Dio.netty.allocator.pageSize: 8192
2020-08-09 23:32:40 [ main:5195 ] - [ DEBUG ] -Dio.netty.allocator.maxOrder: 11
2020-08-09 23:32:40 [ main:5195 ] - [ DEBUG ] -Dio.netty.allocator.chunkSize: 16777216
2020-08-09 23:32:40 [ main:5195 ] - [ DEBUG ] -Dio.netty.allocator.tinyCacheSize: 512
2020-08-09 23:32:40 [ main:5195 ] - [ DEBUG ] -Dio.netty.allocator.smallCacheSize: 256
2020-08-09 23:32:40 [ main:5195 ] - [ DEBUG ] -Dio.netty.allocator.normalCacheSize: 64
2020-08-09 23:32:40 [ main:5195 ] - [ DEBUG ] -Dio.netty.allocator.maxCachedBufferCapacity: 32768
2020-08-09 23:32:40 [ main:5195 ] - [ DEBUG ] -Dio.netty.allocator.cacheTrimInterval: 8192
2020-08-09 23:32:40 [ main:5195 ] - [ DEBUG ] -Dio.netty.allocator.useCacheForAllThreads: true
2020-08-09 23:32:40 [ main:5208 ] - [ DEBUG ] -Dio.netty.allocator.type: pooled
2020-08-09 23:32:40 [ main:5208 ] - [ DEBUG ] -Dio.netty.threadLocalDirectBufferSize: 0
2020-08-09 23:32:40 [ main:5208 ] - [ DEBUG ] -Dio.netty.maxThreadLocalCharBufferSize: 16384
2020-08-09 23:32:40 [ main:5224 ] - [ INFO ] [DUBBO] qos-server bind localhost:22222, dubbo version: 2.7.8, current host: 192.168.213.1
2020-08-09 23:32:40 [ main:5227 ] - [ INFO ] [fixed-192.168.213.102_8848] [subscribe] org.example.ILoginService::.configurators+dubbo
2020-08-09 23:32:40 [ main:5227 ] - [ INFO ] [fixed-192.168.213.102_8848] [add-listener] ok, tenant=, dataId=org.example.ILoginService::.configurators, group=dubbo, cnt=1
2020-08-09 23:32:40 [ main:5251 ] - [ DEBUG ] [DUBBO] package org.apache.dubbo.remoting;
import org.apache.dubbo.common.extension.ExtensionLoader;
public class Transporter$Adaptive implements org.apache.dubbo.remoting.Transporter {
public org.apache.dubbo.remoting.Client connect(org.apache.dubbo.common.URL arg0, org.apache.dubbo.remoting.ChannelHandler arg1) throws org.apache.dubbo.remoting.RemotingException {
if (arg0 == null) throw new IllegalArgumentException("url == null");
org.apache.dubbo.common.URL url = arg0;
String extName = url.getParameter("client", url.getParameter("transporter", "netty"));
if(extName == null) throw new IllegalStateException("Failed to get extension (org.apache.dubbo.remoting.Transporter) name from url (" + url.toString() + ") use keys([client, transporter])");
org.apache.dubbo.remoting.Transporter extension = (org.apache.dubbo.remoting.Transporter)ExtensionLoader.getExtensionLoader(org.apache.dubbo.remoting.Transporter.class).getExtension(extName);
return extension.connect(arg0, arg1);
}
public org.apache.dubbo.remoting.RemotingServer bind(org.apache.dubbo.common.URL arg0, org.apache.dubbo.remoting.ChannelHandler arg1) throws org.apache.dubbo.remoting.RemotingException {
if (arg0 == null) throw new IllegalArgumentException("url == null");
org.apache.dubbo.common.URL url = arg0;
String extName = url.getParameter("server", url.getParameter("transporter", "netty"));
if(extName == null) throw new IllegalStateException("Failed to get extension (org.apache.dubbo.remoting.Transporter) name from url (" + url.toString() + ") use keys([server, transporter])");
org.apache.dubbo.remoting.Transporter extension = (org.apache.dubbo.remoting.Transporter)ExtensionLoader.getExtensionLoader(org.apache.dubbo.remoting.Transporter.class).getExtension(extName);
return extension.bind(arg0, arg1);
}
}, dubbo version: 2.7.8, current host: 192.168.213.1
2020-08-09 23:32:40 [ main:5271 ] - [ DEBUG ] [DUBBO] package org.apache.dubbo.remoting;
import org.apache.dubbo.common.extension.ExtensionLoader;
public class Dispatcher$Adaptive implements org.apache.dubbo.remoting.Dispatcher {
public org.apache.dubbo.remoting.ChannelHandler dispatch(org.apache.dubbo.remoting.ChannelHandler arg0, org.apache.dubbo.common.URL arg1) {
if (arg1 == null) throw new IllegalArgumentException("url == null");
org.apache.dubbo.common.URL url = arg1;
String extName = url.getParameter("dispatcher", url.getParameter("dispather", url.getParameter("channel.handler", "all")));
if(extName == null) throw new IllegalStateException("Failed to get extension (org.apache.dubbo.remoting.Dispatcher) name from url (" + url.toString() + ") use keys([dispatcher, dispather, channel.handler])");
org.apache.dubbo.remoting.Dispatcher extension = (org.apache.dubbo.remoting.Dispatcher)ExtensionLoader.getExtensionLoader(org.apache.dubbo.remoting.Dispatcher.class).getExtension(extName);
return extension.dispatch(arg0, arg1);
}
}, dubbo version: 2.7.8, current host: 192.168.213.1
2020-08-09 23:32:40 [ main:5302 ] - [ INFO ] [DUBBO] Start NettyServer bind /0.0.0.0:20880, export /192.168.117.1:20880, dubbo version: 2.7.8, current host: 192.168.213.1
2020-08-09 23:32:40 [ main:5308 ] - [ DEBUG ] [DUBBO] package org.apache.dubbo.common.threadpool;
import org.apache.dubbo.common.extension.ExtensionLoader;
public class ThreadPool$Adaptive implements org.apache.dubbo.common.threadpool.ThreadPool {
public java.util.concurrent.Executor getExecutor(org.apache.dubbo.common.URL arg0) {
if (arg0 == null) throw new IllegalArgumentException("url == null");
org.apache.dubbo.common.URL url = arg0;
String extName = url.getParameter("threadpool", "fixed");
if(extName == null) throw new IllegalStateException("Failed to get extension (org.apache.dubbo.common.threadpool.ThreadPool) name from url (" + url.toString() + ") use keys([threadpool])");
org.apache.dubbo.common.threadpool.ThreadPool extension = (org.apache.dubbo.common.threadpool.ThreadPool)ExtensionLoader.getExtensionLoader(org.apache.dubbo.common.threadpool.ThreadPool.class).getExtension(extName);
return extension.getExecutor(arg0);
}
}, dubbo version: 2.7.8, current host: 192.168.213.1
2020-08-09 23:32:40 [ main:5332 ] - [ INFO ] initializer namespace from System Property :null
2020-08-09 23:32:40 [ main:5333 ] - [ INFO ] initializer namespace from System Environment :null
2020-08-09 23:32:40 [ main:5333 ] - [ INFO ] initializer namespace from System Property :null
2020-08-09 23:32:40 [ main:5350 ] - [ DEBUG ] server list provided by user: [192.168.213.102:8848]
2020-08-09 23:32:40 [ com.alibaba.nacos.client.naming.updater:5350 ] - [ DEBUG ] server list provided by user: [192.168.213.102:8848]
2020-08-09 23:32:40 [ com.alibaba.nacos.naming.failover:5504 ] - [ DEBUG ] failover switch is not found, failover00-00---000-VIPSRV_FAILOVER_SWITCH-000---00-00
2020-08-09 23:32:40 [ main:5512 ] - [ INFO ] [DUBBO] Load registry cache file C:\Users\Administrator.USER-20191002SO\.dubbo\dubbo-registry-dubbo-server-192.168.213.102-8848.cache, data: {org.example.ILoginService=dubbo://192.168.117.1:20880/org.example.ILoginService?anyhost=true&application=dubbo-server&category=providers&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&interface=org.example.ILoginService&metadata-type=remote&methods=login&path=org.example.ILoginService&pid=2320&protocol=dubbo&release=2.7.8&side=provider×tamp=1596972847380}, dubbo version: 2.7.8, current host: 192.168.213.1
2020-08-09 23:32:40 [ main:5515 ] - [ INFO ] [DUBBO] Register: dubbo://192.168.117.1:20880/org.example.ILoginService?anyhost=true&application=dubbo-server&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&interface=org.example.ILoginService&metadata-type=remote&methods=login&pid=6080&release=2.7.8&side=provider×tamp=1596987159654, dubbo version: 2.7.8, current host: 192.168.213.1
2020-08-09 23:32:40 [ main:5564 ] - [ INFO ] [BEAT] adding beat: {"ip":"192.168.117.1","metadata":{"side":"provider","methods":"login","release":"2.7.8","deprecated":"false","dubbo":"2.0.2","pid":"6080","interface":"org.example.ILoginService","generic":"false","path":"org.example.ILoginService","protocol":"dubbo","metadata-type":"remote","application":"dubbo-server","dynamic":"true","category":"providers","anyhost":"true","timestamp":"1596987159654"},"period":5000,"port":20880,"scheduled":false,"serviceName":"DEFAULT_GROUP@@providers:org.example.ILoginService::","stopped":false,"weight":1.0} to beat map.
2020-08-09 23:32:40 [ main:5571 ] - [ INFO ] [REGISTER-SERVICE] public registering service DEFAULT_GROUP@@providers:org.example.ILoginService:: with instance: {"enabled":true,"ephemeral":true,"healthy":true,"instanceHeartBeatInterval":5000,"instanceHeartBeatTimeOut":15000,"ip":"192.168.117.1","ipDeleteTimeout":30000,"metadata":{"side":"provider","methods":"login","release":"2.7.8","deprecated":"false","dubbo":"2.0.2","pid":"6080","interface":"org.example.ILoginService","generic":"false","path":"org.example.ILoginService","protocol":"dubbo","metadata-type":"remote","application":"dubbo-server","dynamic":"true","category":"providers","anyhost":"true","timestamp":"1596987159654"},"port":20880,"weight":1.0}
2020-08-09 23:32:40 [ main:5576 ] - [ DEBUG ] Request from server: http://192.168.213.102:8848/nacos/v1/ns/instance?app=unknown&metadata=%7B%22side%22%3A%22provider%22%2C%22methods%22%3A%22login%22%2C%22release%22%3A%222.7.8%22%2C%22deprecated%22%3A%22false%22%2C%22dubbo%22%3A%222.0.2%22%2C%22pid%22%3A%226080%22%2C%22interface%22%3A%22org.example.ILoginService%22%2C%22generic%22%3A%22false%22%2C%22path%22%3A%22org.example.ILoginService%22%2C%22protocol%22%3A%22dubbo%22%2C%22metadata-type%22%3A%22remote%22%2C%22application%22%3A%22dubbo-server%22%2C%22dynamic%22%3A%22true%22%2C%22category%22%3A%22providers%22%2C%22anyhost%22%3A%22true%22%2C%22timestamp%22%3A%221596987159654%22%7D&ip=192.168.117.1&weight=1.0&ephemeral=true&serviceName=DEFAULT_GROUP%40%40providers%3Aorg.example.ILoginService%3A%3A&encoding=UTF-8&groupName=DEFAULT_GROUP&namespaceId=public&port=20880&enable=true&healthy=true
2020-08-09 23:32:40 [ main:5599 ] - [ INFO ] [DUBBO] Subscribe: provider://192.168.117.1:20880/org.example.ILoginService?anyhost=true&application=dubbo-server&bind.ip=192.168.117.1&bind.port=20880&category=configurators&check=false&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&interface=org.example.ILoginService&metadata-type=remote&methods=login&pid=6080&release=2.7.8&side=provider×tamp=1596987159654, dubbo version: 2.7.8, current host: 192.168.213.1
2020-08-09 23:32:40 [ main:5604 ] - [ DEBUG ] failover-mode: false
2020-08-09 23:32:40 [ main:5605 ] - [ DEBUG ] failover-mode: false
2020-08-09 23:32:40 [ main:5608 ] - [ DEBUG ] Request from server: http://192.168.213.102:8848/nacos/v1/ns/instance/list?app=unknown&healthyOnly=false&namespaceId=public&clientIP=192.168.117.1&serviceName=DEFAULT_GROUP%40%40providers%3Aorg.example.ILoginService&udpPort=56057&encoding=UTF-8
2020-08-09 23:32:40 [ main:5617 ] - [ INFO ] [DUBBO] Notify urls for subscribe url provider://192.168.117.1:20880/org.example.ILoginService?anyhost=true&application=dubbo-server&bind.ip=192.168.117.1&bind.port=20880&category=configurators&check=false&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&interface=org.example.ILoginService&metadata-type=remote&methods=login&pid=6080&release=2.7.8&side=provider×tamp=1596987159654, urls: [empty://192.168.117.1:20880/org.example.ILoginService?anyhost=true&application=dubbo-server&bind.ip=192.168.117.1&bind.port=20880&category=providers&check=false&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&interface=org.example.ILoginService&metadata-type=remote&methods=login&pid=6080&release=2.7.8&side=provider×tamp=1596987159654], dubbo version: 2.7.8, current host: 192.168.213.1
2020-08-09 23:32:40 [ main:5618 ] - [ DEBUG ] failover-mode: false
2020-08-09 23:32:40 [ main:5619 ] - [ INFO ] [LISTENER] adding DEFAULT_GROUP@@providers:org.example.ILoginService:: with to listener map
2020-08-09 23:32:40 [ main:5620 ] - [ DEBUG ] failover-mode: false
2020-08-09 23:32:40 [ main:5620 ] - [ INFO ] [LISTENER] adding DEFAULT_GROUP@@providers:org.example.ILoginService with to listener map
2020-08-09 23:32:40 [ com.alibaba.nacos.naming.client.listener:5621 ] - [ INFO ] [DUBBO] Notify urls for subscribe url provider://192.168.117.1:20880/org.example.ILoginService?anyhost=true&application=dubbo-server&bind.ip=192.168.117.1&bind.port=20880&category=configurators&check=false&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&interface=org.example.ILoginService&metadata-type=remote&methods=login&pid=6080&release=2.7.8&side=provider×tamp=1596987159654, urls: [empty://192.168.117.1:20880/org.example.ILoginService?anyhost=true&application=dubbo-server&bind.ip=192.168.117.1&bind.port=20880&category=providers&check=false&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&interface=org.example.ILoginService&metadata-type=remote&methods=login&pid=6080&release=2.7.8&side=provider×tamp=1596987159654], dubbo version: 2.7.8, current host: 192.168.213.1
2020-08-09 23:32:40 [ com.alibaba.nacos.naming.client.listener:5622 ] - [ INFO ] [DUBBO] Notify urls for subscribe url provider://192.168.117.1:20880/org.example.ILoginService?anyhost=true&application=dubbo-server&bind.ip=192.168.117.1&bind.port=20880&category=configurators&check=false&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&interface=org.example.ILoginService&metadata-type=remote&methods=login&pid=6080&release=2.7.8&side=provider×tamp=1596987159654, urls: [empty://192.168.117.1:20880/org.example.ILoginService?anyhost=true&application=dubbo-server&bind.ip=192.168.117.1&bind.port=20880&category=providers&check=false&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&interface=org.example.ILoginService&metadata-type=remote&methods=login&pid=6080&release=2.7.8&side=provider×tamp=1596987159654], dubbo version: 2.7.8, current host: 192.168.213.1
2020-08-09 23:32:40 [ DubboSaveMetadataReport-thread-1:5640 ] - [ INFO ] [DUBBO] store provider metadata. Identifier : org.apache.dubbo.metadata.report.identifier.MetadataIdentifier@6c639062; definition: FullServiceDefinition{parameters={side=provider, metadata-type=remote, application=dubbo-server, release=2.7.8, methods=login, deprecated=false, dubbo=2.0.2, dynamic=true, interface=org.example.ILoginService, generic=false, anyhost=true}} ServiceDefinition [canonicalName=org.example.ILoginService, codeSource=file:/D:/repository/Dubbo/dubbo-server-sample/dubbo-server-api/target/classes/, methods=[MethodDefinition [name=login, parameterTypes=[java.lang.String, java.lang.String], returnType=java.lang.String]]], dubbo version: 2.7.8, current host: 192.168.213.1
2020-08-09 23:32:40 [ main:5763 ] - [ INFO ] [fixed-192.168.213.102_8848] [publish-single] ok, dataId=dubbo-server, group=mapping-org.example.ILoginService, tenant=, config=1596987160832
2020-08-09 23:32:40 [ main:5764 ] - [ INFO ] [DUBBO] Dubbo service[null] mapped to interface name[org.example.ILoginService]., dubbo version: 2.7.8, current host: 192.168.213.1
2020-08-09 23:32:40 [ main:5765 ] - [ DEBUG ] Returning cached instance of singleton bean 'dubboLifecycleComponentApplicationListener'
2020-08-09 23:32:40 [ main:5765 ] - [ DEBUG ] Returning cached instance of singleton bean 'dubboBootstrapApplicationListener'
2020-08-09 23:32:40 [ main:5777 ] - [ INFO ] [DUBBO] DubboBootstrap is ready., dubbo version: 2.7.8, current host: 192.168.213.1
2020-08-09 23:32:40 [ main:5777 ] - [ INFO ] [DUBBO] DubboBootstrap has started., dubbo version: 2.7.8, current host: 192.168.213.1
2020-08-09 23:32:40 [ main:5779 ] - [ DEBUG ] Could not find key 'spring.liveBeansView.mbeanDomain' in any property source
2020-08-09 23:32:40 [ main:5781 ] - [ DEBUG ] Returning cached instance of singleton bean 'lifecycleProcessor'
2020-08-09 23:32:40 [ main:5781 ] - [ DEBUG ] Returning cached instance of singleton bean 'dubboLifecycleComponentApplicationListener'
2020-08-09 23:32:40 [ main:5782 ] - [ DEBUG ] Returning cached instance of singleton bean 'dubboBootstrapApplicationListener'
2020-08-09 23:32:40 [ main:5782 ] - [ INFO ] [DUBBO] Dubbo SpringContainer started!, dubbo version: 2.7.8, current host: 192.168.213.1
[2020-08-09 23:32:40] Dubbo service server started!-----------》表示dubbo已经启动完毕。
客服端:
//客服端的代码:
package org.example;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
ILoginService loginService=null;
ApplicationContext context=new
ClassPathXmlApplicationContext
("classpath:META-INF/spring/application.xml");
loginService=context.getBean(ILoginService.class);
System.out.println("---------------------"+loginService.login("admin","admin1"));
}
}
//客户端的配置
在resources目录下 新建META-INF/spring目录
在该目录下面新建application.xml文件:
客户端调用的部分日志: