In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use dubbo generalization calls through API". In daily operations, I believe many people have doubts about how to use dubbo generalization calls through API. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use dubbo generalization calls through API". Next, please follow the editor to study!
What is generalization?
The official website explains: the generalized interface invocation method is mainly used when the client does not have API interfaces and model class elements. All POJO in the parameters and return values are represented by Map, which is usually used for framework integration, such as implementing a general service testing framework, which can be implemented by calling all services through GenericService.
I ran to ask the team leader, what was the generalization they were talking about the other day? What scene do we need to use?
Generalization means that you do not know that you do not care about the lower-level implementation, you only need to agree on the interfaces and parameters, and the implementation is implemented by the lower-level implementation, but the scalability and availability are extremely low, so you might as well agree on an interface in which the provider registers on ZK and the consumer goes to ZK to pull it, or simply provide a jar package and then call it through ordinary registration.
As for the scenario, the third-party data service that is temporarily accessed cannot be called directly. We provide an interface in the middle, and the implementation is implemented by them.
Using generalization calls through Spring
Declare generic= "true" in the Spring configuration:
Get the barService in the Java code and start generalizing the call:
GenericService barService = (GenericService) applicationContext.getBean ("barService")
Object result = barService.$invoke ("sayHello", new String [] {"java.lang.String"}, new Object [] uses generalization to call import org.apache.dubbo.rpc.service.GenericService through API
...
/ / reference remote service
/ / this instance is very heavy and encapsulates all connections with registries and service providers. Please cache
ReferenceConfig reference = new ReferenceConfig ()
/ / weakly typed API name
Reference.setInterface ("com.xxx.XxxService")
Reference.setVersion ("1.0.0")
/ / declared as a generalized interface
Reference.setGeneric (true)
/ / all interface references can be replaced with org.apache.dubbo.rpc.service.GenericService
GenericService genericService = reference.get ()
/ / basic types and Date,List,Map do not need to be converted, just call
Object result = genericService.$invoke ("sayHello", new String [] {"java.lang.String"}, new Object [] {"world"})
/ / use Map to represent the POJO parameter. If the returned value is POJO, it will also be automatically converted to Map.
Map person = new HashMap ()
Person.put ("name", "xxx")
Person.put ("password", "yyy")
/ / if POJO is returned, it will be automatically converted to Map.
Object result = genericService.$invoke ("findPerson", new String []
{"com.xxx.Person"}, new Object [] {person})
...
The realization of generalization
Expose the generalization implementation through API.
/ / all interfaces can be replaced by org.apache.dubbo.rpc.service.GenericService.
GenericService xxxService = new XxxGenericService ()
/ / this instance is very heavy and encapsulates all connections with registries and service providers. Please cache
ServiceConfig service = new ServiceConfig ()
/ / weakly typed API name
Service.setInterface ("com.xxx.XxxService")
Service.setVersion ("1.0.0")
/ / point to a common service implementation
Service.setRef (xxxService)
/ / exposure and Registration Services
Service.export ()
Register with the ZK implementation
Public static void main (String [] args) {ReferenceConfig reference = new ReferenceConfig (); / / the application configuration of the current dubbo consumer, if not set, will throw an exception ApplicationConfig applicationConfig = new ApplicationConfig (); applicationConfig.setName ("xxx_test_service"); / / registry configuration RegistryConfig registryConfig = new RegistryConfig () / / the registry protocol needs to be configured here, such as zookeeper registryConfig.setAddress ("zookeeper://127.0.0.1:2181"), registryConfig.setGroup ("test_group"), reference.setApplication (applicationConfig), reference.setRegistry (registryConfig) below / / set the reference property of the call. Only the protocol, interface name, version and timeout reference.setProtocol ("dubbo") are set below; / / the convention interface reference.setInterface ("com.xxx.test.TestService"); reference.setVersion ("1.0.0"); reference.setTimeout (1000); / / declared as generalization interface reference.setGeneric (true) / / GenericService can catch all implementations GenericService genericService = reference.get (); / / construct complex parameters. In the following example, the first two parameters are of type string, and the second is a complex type, but both can be constructed through map. Map param = new HashMap (); param.put ("test1", "a"); param.put ("test2", "b"); Map thirdParam = new HashMap (); thirdParam.put ("class", "java.util.Map"); thirdParam.put ("subParam1", "c"); thirdParam.put ("subParam2", "d"); param.put ("test3", thirdParam) Object result = genericService.$invoke ("myMethod", new String [] {"java.lang.String", "java.lang.String", "com.xxxtest.MyParam"}, new Object [] {"123", "ddd", param}); System.out.println (JSON.toJSONString (result));} at this point, the study on "how to use dubbo generalization calls through API" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.