5. Dubbo2.5.3 generalized reference and generalization Implementation; 5. dubbo2.5.3 generalized

Source: Internet
Author: User

5. Dubbo2.5.3 generalized reference and generalization Implementation; 5. dubbo2.5.3 generalized

Reprinted from source: Http://www.cnblogs.com/hd3013779515/

1. generalized reference

The method of calling a wildcard interface is mainly used when the client does not have an API interface or a model metadata. All POJO in parameters and return values are represented by Map, which is usually used for framework integration. For example: A universal service testing framework can be implemented by calling all services through GenericService.

(1) Service Provider

Overall Structure

Model User. java

package cn.ljh.dubbo.model;public class User {    private int id;    private String username;    private String password;        get set ...}

Service Interface HelloService. java

package cn.ljh.dubbo.service;import cn.ljh.dubbo.model.User;public interface HelloService {    public String sayHello(String name);     public User getUser(User user);}

Service Interface implementation HelloServiceImpl. java

package cn.ljh.dubbo.service.impl;import cn.ljh.dubbo.model.User;import cn.ljh.dubbo.service.HelloService;public class HelloServiceImpl implements HelloService{    public String sayHello(String name) {        return "Hello World:" + name;    }    public User getUser(User user) {        user.setUsername(user.getUsername() + " Response");        return user;    }}

Configuration File applicationProvider. 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: 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"> <! -- Specific implementation bean --> <bean id = "helloservice" class = "cn. lstrap. dubbo. service. impl. HelloServiceImpl"/> <! -- Provider application information for dependency calculation --> <dubbo: application name = "hello-provider"/> <! -- Use the zookeeper registration center to expose the service address --> <dubbo: registry address = "zookeeper: // 192.168.137.130: 2181? Backup = 192.168.137.131: 2181,192.168 .137.132: 2181 "/> <! -- Expose services on port 20880 using dubbo protocol --> <dubbo: protocol name = "dubbo" port = "20880"/> <! -- Declare the service interface to be exposed --> <dubbo: service interface = "cn. lw.dubbo. service. HelloService" ref = "helloservice"/> </beans>

Service provider Startup Program HelloServiceTest. java

Package cn. lstrap. dubbo. service; import java. io. IOException; import org. springframework. context. support. classPathXmlApplicationContext; public class HelloServiceTest {public static void main (String [] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext (new String [] {"applicationProvider. xml "}); context. start (); System. out. println ("Service Registration successful"); try {System. in. read (); // keep this program running, indicating that the service is always provided} catch (IOException e) {e. printStackTrace ();}}}

 

(2) Service consumers

Overall Structure

Model User. java and service interface HelloService. java do not exist in service consumers.

Configuration File applicationConsumer. xml

Add in dubbo: referencegeneric="true"

<? 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 = "hello-consumer"/> <! -- Use the zookeeper registration center to expose the service address --> <dubbo: registry address = "zookeeper: // 192.168.137.130: 2181? Backup = 192.168.137.131: 2181,192.168 .137.132: 2181 "/> <! -- Generate a remote service proxy. You can use HelloService like a local bean --> <dubbo: reference id = "helloService" interface = "cn. lstrap. dubbo. service. helloService "generic =" true "/> </beans>

Service consumer Startup Program HelloServiceConsumerTest. java

The generalized interface GenericService and parameter map are used in the program.

Package cn. lstrap. dubbo. service; import java. io. IOException; import java. util. hashMap; import java. util. map; import org. springframework. context. support. classPathXmlApplicationContext; import com. alibaba. dubbo. rpc. service. genericService; public class HelloServiceConsumerTest {public static void main (String [] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext (new String [] {"applicationConsumer. xml "}); context. start (); // The method of calling a generic interface is mainly used when the client does not have an API interface or a model metadata. All POJO in parameters and returned values are represented by Map. // com. alibaba. dubbo. rpc. service. genericService can reference GenericService providerService = (GenericService) context instead of all interfaces. getBean ("helloService"); // the basic type and Date, List, Map, and so on do not need to be converted. directly call Object result = providerService. $ invoke ("sayHello", new String [] {"java. lang. string "}, new Object [] {" Tom "}); System. out. println (result); // use Map to represent the POJO parameter. If the returned value is POJO, it is automatically converted to Map <String, Object> user = new HashMap <String, Object> (); user. put ("id", "100"); user. put ("username", "Lily"); user. put ("password", "yyy"); // If POJO is returned, it is automatically converted to Map Object result2 = providerService. $ invoke ("getUser", new String [] {"cn. lstrap. dubbo. model. user "}, new Object [] {user}); Map <String, Object> user2 = (Map <String, Object>) result2; System. out. println ("id:" + user2.get ("id") + "username:" + user2.get ("username") + "password:" + user2.get ("password ")); try {System. in. read (); // keep this program running} catch (IOException e) {e. printStackTrace ();}}}

(3) Test Results

The following shows the correct call and parameter conversion.

2. General Implementation

The wildcard interface implementation method is mainly used when the server side does not have an API interface or a model metadata. All POJO in parameters and returned values are represented by Map, which is usually used for framework integration. For example: A universal remote service Mock framework can be implemented to process all service requests through the GenericService interface.

(1) Service Provider

Overall Structure

Model User. java and service interface HelloService. java do not exist.

Service Interface implementation HelloServiceImpl. java

GenericService interface used

package cn.ljh.dubbo.service.impl;import java.util.Map;import com.alibaba.dubbo.rpc.service.GenericException;import com.alibaba.dubbo.rpc.service.GenericService;public class HelloServiceImpl implements GenericService{    public Object $invoke(String method, String[] parameterTypes, Object[] args)            throws GenericException {                if ("sayHello".equals(method)) {            return "Hello World:" + args[0];        }else if ("getUser".equals(method)) {            Map<String, Object> user = (Map<String, Object>)args[0];            user.put("username", user.get("username") + " Response");            return user;        }                return null;    }}

Configuration File applicationProvider. 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: 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"> <! -- Specific implementation bean --> <bean id = "helloservice" class = "cn. lstrap. dubbo. service. impl. HelloServiceImpl"/> <! -- Provider application information for dependency calculation --> <dubbo: application name = "hello-provider"/> <! -- Use the zookeeper registration center to expose the service address --> <dubbo: registry address = "zookeeper: // 192.168.137.130: 2181? Backup = 192.168.137.131: 2181,192.168 .137.132: 2181 "/> <! -- Expose services on port 20880 using dubbo protocol --> <dubbo: protocol name = "dubbo" port = "20880"/> <! -- Declare the service interface to be exposed --> <dubbo: service interface = "cn. lw.dubbo. service. HelloService" ref = "helloservice"/> </beans>

Service provider Startup Program HelloServiceTest. java

Package cn. lstrap. dubbo. service; import java. io. IOException; import org. springframework. context. support. classPathXmlApplicationContext; public class HelloServiceTest {public static void main (String [] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext (new String [] {"applicationProvider. xml "}); context. start (); System. out. println ("Service Registration successful"); try {System. in. read (); // keep this program running, indicating that the service is always provided} catch (IOException e) {e. printStackTrace ();}}}

(2) Service consumers

Overall Structure

Model User. java

package cn.ljh.dubbo.model;public class User {    private int id;    private String username;    private String password;        get set...}

Service Interface HelloService. java

package cn.ljh.dubbo.service;import cn.ljh.dubbo.model.User;public interface HelloService {    public String sayHello(String name);     public User getUser(User user);}

Configuration File applicationConsumer. 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: 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 = "hello-consumer"/> <! -- Use the zookeeper registration center to expose the service address --> <dubbo: registry address = "zookeeper: // 192.168.137.130: 2181? Backup = 192.168.137.131: 2181,192.168 .137.132: 2181 "/> <! -- Generate a remote service proxy. You can use HelloService like a local bean --> <dubbo: reference id = "helloService" interface = "cn. lstrap. dubbo. service. helloService "/> </beans>

Service consumer Startup Program HelloServiceConsumerTest. java

Package cn. lstrap. dubbo. service; import java. io. IOException; import org. springframework. context. support. classPathXmlApplicationContext; import cn. lstrap. dubbo. model. user; public class HelloServiceConsumerTest {public static void main (String [] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext (new String [] {"applicationConsumer. xml "}); context. start (); HelloService providerService = (HelloService) context. getBean ("helloService"); System. out. println (providerService. sayHello ("Tom"); User user = new User (); user. setId (100); user. setUsername ("Lily test"); user. setPassword ("yyy"); User user2 = providerService. getUser (user); System. out. println ("id:" + user2.getId () + "username:" + user2.getUsername () + "password:" + user2.getPassword (); try {System. in. read (); // keep this program running} catch (IOException e) {e. printStackTrace ();}}}

(3) Test Results

The following shows the correct call and parameter conversion.

 

Refer:

Http://dubbo.io/User+Guide-zh.htm

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.