Start with the <sofa:XXX> tab to see how Sofa-boot fits into Spring

Source: Internet
Author: User

Objective

Sofa-boot at this stage the XML-enabled way to define beans in spring, through which we can remove references from the RPC and make calls from the spring container, how did he handle the custom tags? Take a look at them.

How do I use it?

Official examples:

<?xmlVersion= "1.0" encoding= "UTF-8"?><beansxmlns="Http://www.springframework.org/schema/beans"xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"xmlns:sofa="Http://sofastack.io/schema/sofaboot"xmlns:context="Http://www.springframework.org/schema/context"xsi:schemalocation="Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://sofastack.io/schema/sofaboot http://sofastack.io/schema/sofaboot.xsd "default-autowire="ByName">    <beanid="Personserviceimpl"class="Com.alipay.sofa.boot.examples.demo.rpc.bean.PersonServiceImpl"/>    <sofa:serviceref="Personserviceimpl"interface="Com.alipay.sofa.boot.examples.demo.rpc.bean.PersonService">        <sofa:binding.bolt/>        <sofa:binding.rest/>    </sofa:service>    <sofa:referenceid="Personreferencebolt"interface="Com.alipay.sofa.boot.examples.demo.rpc.bean.PersonService">        <sofa:binding.bolt/>    </sofa:reference>    <sofa:referenceid="Personreferencerest"interface="Com.alipay.sofa.boot.examples.demo.rpc.bean.PersonService">        <sofa:binding.rest/>    </sofa:reference>    <beanid="Personfilter"class="Com.alipay.sofa.boot.examples.demo.rpc.bean.PersonServiceFilter"/></beans>

A conspicuous sofa label. So how do you know how he handles the contents of these tags?

A: You can find it from the xmlns namespace above. If you write a custom label, you must be familiar with it.

If you have not written it, simply introduce it. Spring supports user-defined labeling and is subject to the following specifications.

    1. Writing an XSD file is the attribute that defines the label.

    2. Inheriting abstract classes NamespaceHandlerSupport and implementing the Init method, which is the class that handles namespaces, requires not only the implementation of the Init method, you also inherit AbstractSingleBeanDefinitionParser the class and parse the label yourself. The usual wording is this:
      registerBeanDefinitionParser("tagName",new UserBeanDefinitionParser());

    3. After completing the above steps, you need to write and file in the Meta-inf directory spring.handlers spring.schemas , the former key is the namespace name, value is NamespaceHandlerSupport the specific implementation, the latter key is the XSD namespace, value is the XSD specific file (class Path).

After completing the above steps, Spring will check the namespace when loading the XML configuration file, and if it is customized, it will find the corresponding parser based on the key of the namespace, that is, NamespaceHandlerSupport parsing the custom label.

So, our aim is to see how the sofa tag is parsed, then find the analytic sofa NamespaceHandlerSupport .

Find the source of sofa tags

Global search through idea or other tools, we found the source under the start module under the resources of the Meta-inf directory, the following files in the directory:

    1. Rpc.xsd
    2. Sofaboot.xsd
    3. Spring.handlers
    4. Spring.schemas

These documents we are more concerned about, focus on the handlers file. The contents are as follows:

http\://sofastack.io/schema/sofaboot=com.alipay.sofa.infra.config.spring.namespace.handler.SofaBootNamespaceHandler

SofaBootNamespaceHandlerObviously is the clear space processing class, inherits the Spring's NamespaceHandlerSupport .

The Init method of this class is extended through the SPI of Java to find the Sofaboottagnamesupport tag support class, which currently has 2 implementations: Servicedefinitionparser and Referencedefinitionpar Ser

Two classes support a different element. One is the referral service and one is the publishing service.

This is more clear. After getting two classes, register to Spring's parsers map. Key is the element name and value is the parser.

The specific parsing above says, service and reference.

Sofa uses a template pattern on this design, uses an abstract class AbstractContractDefinitionParser , and defines a doparseinternal abstract method for subclasses to implement.

The abstract parent class resolves some common properties, all of which are common properties:

How can you be sure it's a common attribute? First, after parsing the XML, it must be injected into the bean, so what is this bean? In fact, in an AbstractSingleBeanDefinitionParser abstract class, there is a method that returns a Bean type, which corresponds to a tag, and their parent class is AbstractContractFactoryBean .

The common properties of this class correspond to the definitions in the above image.

With these basic information, or can not be used directly, after all, is a string. We want to see how SOFA is merging itself with the Spring container.

How to integrate with Spring?

Take a look at their subclasses, the Referencedefinitionparser class (Servicefactorybean similar). The corresponding Bean is Referencefactorybean, which defines the properties of load Balancing (loadbalance) separately.

Ah, from the name of this class, very familiar with, before the analysis of Spring source, I saw Factorybean. The returned Bean can be modified by the GetObject method.

Take a look at the class diagram of this class

As we expected, the key extension points in Spring are inherited. And his GetObject method will return a proxy:

   @Override    publicgetObjectthrows Exception {        return proxy;    }

The method that specifically creates the proxy is the com.alipay.sofa.runtime.service.component.ReferenceComponent Createproxy method of the class, and then the adapter is called. The role of the adapter is the role of glue, the spring container and the third-party framework to glue together, Spring provides the interface is Factorybean and other interfaces, and the third-party framework can be in the GetObject method.

Sofa-boot adapter in the om.alipay.sofa.rpc.boot.runtime.adapter package, the implementation is the Rpcbindingadapter class, where the Outbinding method is used to publish the service, Inbinding method is used to reference the service, which is directly used Sofa-rpc consum Erconfig and Providerconfig. That should be familiar. Ha ha.

Take a look at the code (exception handling has been removed):

 @Override   object outbinding  (object contract, rpcbinding binding, Object target, Sofaruntimecontext sofaruntimecontext) {String uniqueName = Providerconfigcontainer. (contract) contract, binding); Providerconfig providerconfig = Providerconfigcontainer. getproviderconfig     (UniqueName); Providerconfig. export     (); if  (Providerconfigcontainer. isallowpublish  ()) {Registry Registry = Registryconfigcontainer. getregistry         (); Providerconfig. setregister         (true ); Registry. register     (Providerconfig); } return  Boolean. true ;}  
@OverridepublicinBinding(Object contract, RpcBinding binding, SofaRuntimeContext sofaRuntimeContext) {    ConsumerConfig consumerConfig = ConsumerConfigHelper.getConsumerConfig((Contract) contract, binding);    ConsumerConfigContainer.addConsumerConfig(binding, consumerConfig);    Object result = consumerConfig.refer();    binding.setConsumerConfig(consumerConfig);    return result;}

So, when the Spring container uses the GetObject method, it gets the proxy object. Isn't it simple?

One thing to note is that Servicefactorybean's design and Referencefactorybean are really similar, but! But! His GetObject method is to implement the class itself, which is the design of the RPC framework itself, because it does not need a proxy, only need to publish the service on the line, when the client received the information, directly call the implementation of the class of the specified method is good, no client is so complex.

Of course, there is also the concept of a component in SOFA, and we have time to take a good look at this piece of design.

Summarize

This time we started with the XML tag of the Sofa-boot configuration file and analyzed how he integrated into Spring, and in fact, the whole is similar to what we associate with, using Spring's Factorybean GetObject method to return the proxy, if it's a client, at GE In the TObject method, a dynamic proxy is created, which will use SOFA-RPC, so the task of merging RPC and Spring must be an adapter. The implementation of SOFA is the Rpcbindingadapter class. In this class, RPC and Spring are adapted.

The publishing service is a little simpler to compare to the reference service, and the whole point is to publish the service through annotations, and of course the rpcbindingadapter used to adapt it. However, no proxy is used (not required).

Well, this is the process of SOFA and Spring Fusion.

Bye!

Start with the <sofa:XXX> tab to see how Sofa-boot fits into Spring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.