URL也就是Uniform Resource Locator,中文叫统一资源定位符。Dubbo中无论是服务消费方,或者服务提供方,或者注册中心。都是通过URL进行定位资源的。所以今天来聊聊Dubbo中的统一URL资源模型是怎么样的。
标准的URL格式如下:
protocol://username:password@host:port/path?key=value&key=value
在Dubbo中URL也是主要由上面的参数组成。
1 public URL(String protocol, String username, String password, String host, int port, String path, Map<String, String> parameters) { 2 if ((username == null || username.length() == 0) 3 && password != null && password.length() > 0) { 4 throw new IllegalArgumentException("Invalid url, password without username!"); 5 } 6 this.protocol = protocol; 7 this.username = username; 8 this.password = password; 9 this.host = host; 10 this.port = (port < 0 ? 0 : port); 11 this.path = path; 12 // trim the beginning "/" 13 while(path != null && path.startsWith("/")) { 14 path = path.substring(1); 15 } 16 if (parameters == null) { 17 parameters = new HashMap<String, String>(); 18 } else { 19 parameters = new HashMap<String, String>(parameters); 20 } 21 this.parameters = Collections.unmodifiableMap(parameters); 22 }
可以从上面源码看出:
大致样子如下:
dubbo://192.168.1.6:20880/moe.cnkirito.sample.HelloService?timeout=3000
描述一个 dubbo 协议的服务
zookeeper://127.0.0.1:2181/org.apache.dubbo.registry.RegistryService?application=demo-consumer&dubbo=2.0.2&interface=org.apache.dubbo.registry.RegistryService&pid=1214&qos.port=33333×tamp=1545721981946
描述一个 zookeeper 注册中心
consumer://30.5.120.217/org.apache.dubbo.demo.DemoService?application=demo-consumer&category=consumers&check=false&dubbo=2.0.2&interface=org.apache.dubbo.demo.DemoService&methods=sayHello&pid=1209&qos.port=33333&side=consumer×tamp=1545721827784
描述一个消费者
1、解析服务
2、直接暴露服务端口
3、向注册中心暴露服务端口
4、直接引用服务
5、从注册中心引用服务
1、协议简介
聊完了Dubbo中的URL模型就来聊聊Dubbo中的协议。协议是双方确定的交流语义,协议在双方传输数据中起到的了交换作用,没有协议就无法完成数据交换。在dubbo中就是Codec2
@SPI public interface Codec2 { @Adaptive({Constants.CODEC_KEY}) void encode(Channel channel, ChannelBuffer buffer, Object message) throws IOException; @Adaptive({Constants.CODEC_KEY}) Object decode(Channel channel, ChannelBuffer buffer) throws IOException; enum DecodeResult { NEED_MORE_INPUT, SKIP_SOME_INPUT } }
encode是将通信对象编码到ByteBufferWrapper,decode是将从网络上读取的ChannelBuffer解码为Object。
2、协议图解
具体的解释如下:
到这里就稍微解释了一下Dubbo内部的URL以及Dubbo的协议字段。可以说这两个对我们了解Dubbo起到了很大的作用。
Dubbo(五):Dubbo中的URL统一资源模型与Dubbo协议
原文:https://www.cnblogs.com/Cubemen/p/12312981.html