dubbo源码解析-服务暴露与发现
一、概述
dubbo是一个简单易用的RPC框架,通过简单的提供者,消费者配置就能完成无感的网络调用。那么在dubbo中是如何将提供者的服务暴露出去,消费者又是如何获取到提供者相关信息的呢?
二、dubbo与spring的整合
在了解dubbo的服务注册和服务发现之前,我们首先需要掌握一个知识点:Spring中自定义Schema
三、Spring自定义Schema
Dubbo 现在的设计是完全无侵入,也就是使用者只依赖于配置契约。在 Dubbo 中,可以使用 XML 配置相关信息,也可以用来引入服务或者导出服务。
配置完成,启动工程,Spring 会读取配置文件,生成注入相关Bean。那 Dubbo 如何实现自定义 XML 被 Spring 加载读取呢 ?
从 Spring 2.0 开始,Spring 开始提供了一种基于 XML Schema 格式扩展机制,用于定义和配置 bean
入门案例
学习和使用Spring XML Schema 扩展机制并不难,需要下面几个步骤:
1、创建配置属性的JavaBean对象
2、创建一个 XML Schema 文件,描述自定义的合法构建模块,也就是xsd文件
3、自定义处理器类,并实现NamespaceHandler接口
4、自定义解析器,实现BeanDefinitionParser接口(最关键的部分)
5、编写Spring.handlers和Spring.schemas文件配置所有部件
定义JavaBean对象,在spring中此对象会根据配置自动创建
public class User {
private String id;
private String name;
private Integer age;
//省略getter setter方法
}
定义spring.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:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:itheima="http://www.itheima.com/schema/user"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.itheima.com/schema/user http://www.itheima.com/schema/user.xsd">
<itheima:user id="user" name="zhangsan" age="12"></itheima:user>
</beans>
定义spring.handlers文件,根据Spring.xml的xsi:schemaLocation="http://www.itheima.com/schema/user" 与NamespaceHandler类的对应关系;必须放在classpath下的META-INF文件夹中
http\://www.itheima.com/schema/user=com.itheima.schema.UserNamespaceHandler
自定义UserNamespaceHandler类
package com.itheima.schema;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
public class UserNamespaceHandler extends NamespaceHandlerSupport {
public void init() {
registerBeanDefinitionParser("user", new UserBeanDefinitionParser());
}
}
BeanDefinitionParser是标签对应的解析器,Spring读取到对应标签时会使用该类进行解析;
public class UserBeanDefinitionParser extends
AbstractSingleBeanDefinitionParser {
protected Class getBeanClass(Element element) {
return User.class;
}
protected void doParse(Element element, BeanDefinitionBuilder bean) {
String name = element.getAttribute("name");
String age = element.getAttribute("age");
String id = element.getAttribute("id");
if (StringUtils.hasText(id)) {
bean.addPropertyValue("id", id);
}
if (StringUtils.hasText(name)) {
bean.addPropertyValue("name", name);
}
if (StringUtils.hasText(age)) {
bean.addPropertyValue("age", Integer.valueOf(age));
}
}
}
定义spring.schemas文件,根据Spring.xml的xsi:schemaLocation="http://www.itheima.com/schema/user.xsd" 与 user.xsd文件对应关系;必须放在classpath下的META-INF文件夹中。
http\://www.itheima.com/schema/user.xsd=META-INF/user.xsd
在META-INF下定义user.xsd文件,使用xsd用于描述标签的规则
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema
xmlns="http://www.itheima.com/schema/user"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:beans="http://www.springframework.org/schema/beans"
targetNamespace="http://www.itheima.com/schema/user"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xsd:import namespace="http://www.springframework.org/schema/beans" />
<xsd:element name="user">
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="beans:identifiedType">
<xsd:attribute name="name" type="xsd:string" />
<xsd:attribute name="age" type="xsd:int" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
</xsd:schema>
dubbo源码解析-服务暴露与发现的更多相关文章
- 2、Dubbo源码解析--服务发布原理(Netty服务暴露)
一.服务发布 - 原理: 首先看Dubbo日志,截取重要部分: 1)暴露本地服务 Export dubbo service com.alibaba.dubbo.demo.DemoService to ...
- dubbo源码解析-zookeeper创建节点
前言 在之前dubbo源码解析-本地暴露中的前言部分提到了两道高频的面试题,其中一道dubbo中zookeeper做注册中心,如果注册中心集群都挂掉,那发布者和订阅者还能通信吗?在上周的dubbo源码 ...
- dubbo源码解析-spi(4)
前言 本篇是spi的第四篇,本篇讲解的是spi中增加的AOP,还是和上一篇一样,我们先从大家熟悉的spring引出AOP. AOP是老生常谈的话题了,思想都不会是一蹴而就的.比如架构设计从All in ...
- Dubbo 源码解析四 —— 负载均衡LoadBalance
欢迎来我的 Star Followers 后期后继续更新Dubbo别的文章 Dubbo 源码分析系列之一环境搭建 Dubbo 入门之二 --- 项目结构解析 Dubbo 源码分析系列之三 -- 架构原 ...
- dubbo源码解析-spi(一)
前言 虽然标题是dubbo源码解析,但是本篇并不会出现dubbo的源码,本篇和之前的dubbo源码解析-简单原理.与spring融合一样,为dubbo源码解析专题的知识预热篇. 插播面试题 你是否了解 ...
- Dubbo 源码分析 - 服务调用过程
注: 本系列文章已捐赠给 Dubbo 社区,你也可以在 Dubbo 官方文档中阅读本系列文章. 1. 简介 在前面的文章中,我们分析了 Dubbo SPI.服务导出与引入.以及集群容错方面的代码.经过 ...
- dubbo源码解析五 --- 集群容错架构设计与原理分析
欢迎来我的 Star Followers 后期后继续更新Dubbo别的文章 Dubbo 源码分析系列之一环境搭建 博客园 Dubbo 入门之二 --- 项目结构解析 博客园 Dubbo 源码分析系列之 ...
- dubbo源码解析-spi(3)
前言 在上一篇的末尾,我们提到了dubbo的spi中增加了IoC和AOP的功能.那么本篇就讲一下这个增加的IoC,spi部分预计会有四篇,因为这东西实在是太重要了.温故而知新,我们先来回顾一下,我们之 ...
- Dubbo源码(四) - 服务引用(消费者)
前言 本文基于Dubbo2.6.x版本,中文注释版源码已上传github:xiaoguyu/dubbo 上一篇文章,讲了Dubbo的服务导出: Dubbo源码(三) - 服务导出(生产者) 本文,咱们 ...
- Netty源码解析---服务端启动
Netty源码解析---服务端启动 一个简单的服务端代码: public class SimpleServer { public static void main(String[] args) { N ...
随机推荐
- appExtend.js
appextend.js是自己编写的一个手机网页过渡时显示loading的一个js插件. appextend.js : var appExtend = function () { this.setHi ...
- Ping测试记录脚本
@echo off echo PingTest del PingTest_result.txt timeout /t 10 echo=> PingTest_result.txt :TEST ec ...
- dart变量声明和变量类型
ps==>所有的代码必须放在main方法中 main方法有两种写法 1==> main() { print("你好,dart我们相遇了"); } 2==> voi ...
- P1787 [入门赛 #22]非众数 Hard Version 题解
P1787 [入门赛 #22]非众数 Hard Version 题解 原题传送门 这里对 pjh0625 的题解进行了详细解释 1. 读题 题目要求计算给定字符串中非众数子串的数量. 非众数子串 的定 ...
- Excel中使用VLOOKUP对两个单元格关联
一.背景 exl中需要关联两个Excel,根据主键合并两个单元格数据 二.使用方法 1.表1---列包含在id.姓名.年龄 2.表2---列包含姓名.性别 3.期望根据[姓名]列为主键,关联两个表数据 ...
- 埋点-App层丢失率
一.建表语句 create table dws_bhv_habo_measure_lostrate_mb_di( version_flag bigint comment '版本标签 2:web丢失 3 ...
- 开源的 DeepSeek-R1「GitHub 热点速览」
春节假期回来,一睁眼全是王炸级的开源模型 DeepSeek-R1! GitHub 地址→github.com/deepseek-ai/DeepSeek-R1 DeepSeek-R1 开源还不到一个月, ...
- FreeSql学习笔记——2.插入
前言 由于还没有表结构,就先从新增开始,插入一些数据后才好做查询.修改.删除操作. 初始化 前面注入FreeSql时设置过自动同步表结构,那么就不用管数据库了,只需要在项目中定义实体,就会自动生成表结 ...
- [WC2006] 水管局长 题解
最大值最小的路径肯定在最小生成树上,考虑用 \(LCT\) 维护最小生成树,只需要维护长度最长的边即可实现.由于 \(LCT\) 维护最小生成树不支持删边,所以采用倒序加边的方式处理. 时间复杂度 \ ...
- const char*和char*的区别