[Original] RPC call is implemented through Dubbo annotation, while dubbo annotation rpc call

Source: Internet
Author: User

[Original] RPC call is implemented through Dubbo annotation, while dubbo annotation rpc call
There are two ways to start the Dubbo service: 1 is configured through xml, 2 is implemented through annotation, which is similar to Spring.

 

 

  • The XML configuration is as follows:
    <? 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: dubbo = "http://code.alibabatech.com/schema/dubbo" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <! -- Provider application information for dependency calculation --> <dubbo: application name = "find_all_goods"/> <! -- Use the multicast broadcast registration center to expose the service address --> <! -- <Dubbo: registry address = "multicast: // 224.5.6.7: 1234"/> --> <! -- Use the zookeeper registration center to expose the service address --> <dubbo: registry address = "zookeeper: // 127.0.0.1: 2181"/> <! -- Expose services on port 20880 using dubbo protocol --> <dubbo: protocol name = "dubbo" port = "20880"/> <! -- Declare the service interface to be exposed (note the interface, not the implementation class) --> <dubbo: service interface = "com. zdd. dubbo. provider. goodsService "ref =" goodsService "/> <! -- Here is the specific implementation class. The id must be consistent with the Service Interface ref exposed above, dubbo registers the corresponding service through this --> <bean id = "goodsService" class = "com. zdd. dubbo. provider. goodsServiceImpl "> </bean> </beans>

    The code above exposes the interface through the dubbo: service interface tag and automatically registers it to zookeeper when the program starts.

 

  • The consumer call method is configured as follows:
    <? 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: dubbo = "http://code.alibabatech.com/schema/dubbo" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <! -- Consumer application name, used to calculate dependencies. It is not a matching condition. Do not use the same name as the provider --> <dubbo: application name = "dubbo-consumer"/> <! -- Use the multicast broadcast registration center to expose the Discovery Service address --> <! -- <Dubbo: registry address = "multicast: // 224.5.6.7: 1234"/> --> <dubbo: registry address = "zookeeper: // 127.0.0.1: 2181"/> <! -- Generate a remote service proxy, which can be called like a local bean --> <dubbo: reference id = "goodsService" interface = "com. zdd. dubbo. provider. goodsService "/> </beans>

    Then we can call the local service like spring. The Code is as follows:

ApplicationContext  dubbo_cusumer = new ClassPathXmlApplicationContext("file:D:/workspace/GZDTL_TRUNK/zdd-web-consumer/spring/dubbo-consumer.xml");       dubbo_cusumer.getBean("goodsService");
View Code

 

The preceding section uses xml to configure dubbo. The following section uses annotations to configure dubbo. You can compare the differences.

 

 

 

  • The dubbo. xml configured by annotation is as follows:
    <? 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: dubbo = "http://code.alibabatech.com/schema/dubbo" xmlns: context = "http://www.springframework.org/schema/context" xsi: schemaLocation = "http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/beans http://www.spri Ngframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd "> <! -- Provider application information for dependency calculation --> <dubbo: application name = "dubbo_provider"/> <! -- Use the multicast broadcast registry to expose the service address <dubbo: registry address = "multicast: // 224.5.6.7: 1234"/> --> <! -- Use the zookeeper registration center to expose the service address --> <dubbo: registry address = "zookeeper: // 127.0.0.1: 2181"/> <! -- Expose services on port 20880 using dubbo protocol --> <dubbo: protocol name = "dubbo" port = "20880"/> <! -- Specific implementation bean <bean id = "demoService" class = "com. unj. dubbotest. provider. impl. DemoServiceImpl"/> --> <! -- Declare the service interface to be exposed <dubbo: service interface = "com. unj. dubbotest. provider. DemoService" ref = "demoService"/> --> <! -- Expose the interface using annotations --> <dubbo: annotation package = "com. dubbo. provide"/> <! -- Add spring annotation scan --> <! -- <Context: component-scan base-package = "com. dubbo. provide"/> --> </beans>

    The preceding scan uses dubbo: annotation package to scan com. dubbo. all the packages in provide, similar to spring's context: component-scan base-package, scan dubbo-related annotations, in this way, we do not need to configure dubbo: service interface for each service interface in xml.

 

  • The annotation-based service implementation class is as follows:
    package com.dubbo.provide;import com.alibaba.dubbo.config.annotation.Service;@Servicepublic class DubboPro implements DubboProInterface {    public void print() {        System.out.println("is ok");            }}

    Note that the Service annotation is the dubbo annotation instead of spring, which is located in the annotaion directory of dubbo. jar:

  • The consumer annotation method call class is as follows:

package com.dubbo.consumer;import org.springframework.stereotype.Component;import com.alibaba.dubbo.config.annotation.Reference;import com.dubbo.provide.DubboProInterface;@Componentpublic class DubboConsumer{    @Reference     DubboProInterface dubboProInterface;        public void print(){        System.out.println(dubboProInterface);        dubboProInterface.print();    }}
View Code

Through the @ Reference annotation, dubbo will automatically help our proxy interface during scanning, and then call the remote service through rpc

 

 

References: http://dubbo.io/User+Guide-zh.htm#UserGuide-zh-%E6%B3%A8%E8%A7%A3%E9%85%8D%E7%BD% AE

 

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.