分布式服务Dubbo从入门到"精通"之自定义Schema

相信,大多数小伙伴们仔细阅读了上篇文章并翻阅了Dubbo的部分代码,一定会对其有一个大体的了解吧。

当然,不了解也没问题,反正不是我不想让你不了解。好了,言归正传,今天呢,与大家分享的主题如题目所述《如何自定义Schema》。

01164137-7f0eb33f471341c0afec0dace55c06c2.png

项目大体如下(请自行忽略报错和它的丑):

12345.png

小伙伴们,有么有发现跟上一篇的结构其实是差不多的,唯一的差别是,这个只是个demo。

定义一个类,ServiceConfig:

public class ServiceConfig {
    private String interfaceVal;
    private Object ref;

    public ServiceConfig() {
        super();
    }

    public ServiceConfig(String interfaceVal, Object ref) {
        super();
        this.interfaceVal = interfaceVal;
        this.ref = ref;
    }

    public String getInterfaceVal() {
        return interfaceVal;
    }

    public void setInterfaceVal(String interfaceVal) {
        this.interfaceVal = interfaceVal;
    }

    public Object getRef() {
        return ref;
    }

    public void setRef(Object ref) {
        this.ref = ref;
    }

}

定义一个人 PeopleConfig:

public class PeopleConfig {
    private String name;
    private String sex;
    private Integer age;
    
    public PeopleConfig() {
        super();
    }
    public PeopleConfig(String name, String sex, Integer age) {
        super();
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

处理器 RpcBeanDefinitionParser:

import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.w3c.dom.Element;

import com.itstyle.rpc.cinfig.PeopleConfig;
import com.itstyle.rpc.cinfig.ServiceConfig;

public class RpcBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
    private Class<?> beanClass;

    public RpcBeanDefinitionParser(Class<?> beanClass) {
        this.beanClass = beanClass;
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    protected Class getBeanClass(Element element) {
        return this.beanClass;
    }

    protected void doParse(Element element, BeanDefinitionBuilder bean) {
        if (ServiceConfig.class.equals(this.beanClass)) {
            String interfaceVal = element.getAttribute("interface");
            String refVal = element.getAttribute("ref");
            Object reference = new RuntimeBeanReference(refVal);

            bean.addPropertyValue("interfaceVal", interfaceVal);
            bean.addPropertyValue("ref", reference);
        }else if(PeopleConfig.class.equals(this.beanClass)){
            String name = element.getAttribute("name");
            String sex = element.getAttribute("sex");
            String age = element.getAttribute("age");
            bean.addPropertyValue("name", name);
            bean.addPropertyValue("sex", sex);
            bean.addPropertyValue("age", age);
        }
    }
}

控制器 RpcNamespaceHandler:

import org.springframework.beans.factory.xml.NamespaceHandlerSupport;

import com.itstyle.rpc.cinfig.PeopleConfig;
import com.itstyle.rpc.cinfig.ServiceConfig;

public class RpcNamespaceHandler extends  NamespaceHandlerSupport {
    //初始化配置
    public void init() {
        registerBeanDefinitionParser("service", new RpcBeanDefinitionParser(ServiceConfig.class));
        registerBeanDefinitionParser("people", new RpcBeanDefinitionParser(PeopleConfig.class));
    }
}

当然,这里我还建了一个service:

public interface IBeanService {
    String say();
}
public class BeanServiceImpl implements IBeanService {
    @Override
    public String say() {
        return "科帮网欢迎你";
    }
}

好了,接下来就开始我们的配置吧: spring.schemas:

http\://code.itstyle.com/schema/itstyle/itstyle.xsd=META-INF/itstyle.xsd

spring.handlers:

http\://code.itstyle.com/schema/itstyle=com.itstyle.rpc.cinfig.spring.schema.RpcNamespaceHandler

itstyle.xsd:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsd:schema xmlns="http://code.itstyle.com/schema/itstyle"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:tool="http://www.springframework.org/schema/tool"
    targetNamespace="http://code.itstyle.com/schema/itstyle">
    
    <!-- 向一个文档添加带有不同目标命名空间的多个 schema  -->
    <xsd:import namespace="http://www.w3.org/XML/1998/namespace"/>
    <xsd:import namespace="http://www.springframework.org/schema/beans"/>
    <xsd:import namespace="http://www.springframework.org/schema/tool"/>
  
  <xsd:complexType name="serviceType">
     <xsd:complexContent>
        <xsd:extension base="beans:identifiedType" >
             <xsd:attribute name="interface" type="xsd:string" />
             <xsd:attribute name="ref" type="xsd:string" />
         </xsd:extension>
      </xsd:complexContent>
  </xsd:complexType>
  <xsd:element name="service" type="serviceType">
        <xsd:annotation> 
            <xsd:documentation><![CDATA[ The annotation config ]]></xsd:documentation> 
        </xsd:annotation>
  </xsd:element>
  
  <xsd:complexType name="peopleType">
     <xsd:complexContent>
        <xsd:extension base="beans:identifiedType" >
             <xsd:attribute name="name" type="xsd:string" />
             <xsd:attribute name="sex"  type="xsd:string" />
             <xsd:attribute name="age"  type="xsd:string" />
         </xsd:extension>
      </xsd:complexContent>
  </xsd:complexType>
  <xsd:element name="people" type="peopleType">
        <xsd:annotation> 
            <xsd:documentation><![CDATA[ The annotation config ]]></xsd:documentation> 
        </xsd:annotation>
  </xsd:element>
</xsd:schema>

spring-itstyle.xml:

发图,是让大家注意重点

34567.png

<?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:itstyle="http://code.itstyle.com/schema/itstyle"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://code.itstyle.com/schema/itstyle
        http://code.itstyle.com/schema/itstyle/itstyle.xsd
        ">
    <bean id="say" class="com.itstyle.rpc.service.impl.BeanServiceImpl"></bean>
    <itstyle:service id="service" interface="com.itstyle.rpc.service.IBeanService" ref="say"/>
    
    <itstyle:people id="xiaoming" name="小明" sex="男" age="10"/>
</beans>

好了,最后我们写一个测试ItstyleSchema:

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.itstyle.rpc.cinfig.PeopleConfig;
import com.itstyle.rpc.cinfig.ServiceConfig;
import com.itstyle.rpc.service.IBeanService;

public class ItstyleSchema {
    @SuppressWarnings("resource")
    @Test
    public void test() {

        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                "classpath:spring-itstyle.xml");
        context.start();
        //服务接口
        ServiceConfig service = (ServiceConfig) context.getBean("service");
        IBeanService  service1 = (IBeanService) service.getRef();
        //实现方法
        System.out.println(service1.say());
        //xiaoming
        PeopleConfig people = (PeopleConfig) context.getBean("xiaoming");
        System.out.println("我是:"+people.getName());
    }
}

4567.png

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏JackeyGao的博客

Leetcode 算法 - 1. Two Sum

这是我解的第一道题, 当时就懵比了, 写了若干方法均超时了. 最后到网上搜索的答案. 需要用哈希表, python 字典其实就是哈希表的实现. 感受下哈希表的使...

843
来自专栏小樱的经验随笔

POJ 1804 Brainman(5种解法,好题,【暴力】,【归并排序】,【线段树单点更新】,【树状数组】,【平衡树】)

Brainman Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 1057...

4147
来自专栏二进制文集

Java 回调机制

Teacher 中,有一个解决问题的对象:Student,在Student中解决问题之后,再通过引用调用Teacher中的tellAnswer接口,所以叫回调。

3813
来自专栏IMWeb前端团队

JSON Schema 介绍及应用

本文作者:IMWeb jerytang 原文出处:IMWeb社区 未经同意,禁止转载 ? 一、如何描述 JSON ? JSON (JavaScript...

30810
来自专栏noteless

[十一]JavaIO之DataInputStream 和 DataOutputStream

DataInputStream和DataOutputStream 继承了各自的FilterInputStream以及FilterOutputStream

1032
来自专栏算法修养

Code Forces 149DColoring Brackets(区间DP)

 Coloring Brackets time limit per test 2 seconds memory limit per test 256...

3066
来自专栏HansBug's Lab

1901: Zju2112 Dynamic Rankings

1901: Zju2112 Dynamic Rankings Time Limit: 10 Sec  Memory Limit: 128 MB Submit: ...

2776
来自专栏Android开发指南

Android回调接口的写法

1.3K5
来自专栏ml

Guava------------Cache使用方法

    简单从这几个方面描述一下如何使用Cache,对Cache的各种原理介绍此处不涉及.     1.使用场景     2.如何使用Cache     3.创...

7088
来自专栏xingoo, 一个梦想做发明家的程序员

【设计模式】—— 状态模式State

  模式意图   允许一个对象在内部改变它的状态,并根据不同的状态有不同的操作行为。   例如,水在固体、液体、气体是三种状态,但是展现在我们面前的确实不同的...

1987

扫码关注云+社区

领取腾讯云代金券