Background: 1. What is Dubbo and how to use the Dubbo sharding service?
2: You need to know how to register the required services to Dubbo
3: You may also need to know how to use zookeeper
Topic of this chapter: describes how to call Dubbo services based on a specific example.
First, ensure that you have introduced the following JAR packages:
If your project is not an mavn project and you are calling the Dubbo service in storm. The above jar packages are the jar packages you need to put in the project.
See the picture on the right. Here, I made a simple distinction:
Dubbo: All duboo-related jar packages
Lib: All jar packages required by storm
Notstorm: not the jar package required by storm
Spring: All jar Packages containing spring
Second, you must configure the application. xml file. A specific example 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"> <! -- 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 = "service. MQ. Kafka"/> <! -- Use the zookeeper registration center to expose the service address 223.203.216.238 192.168.50.247: 2181? Backup = 192.168.50.248: 2181,192.168 .50.249: 2181 --> <Dubbo: registryaddress = "zookeeper: // 192.168.50.247: 2181? Backup = 192.168.50.248: 2181,192.168 .50.249: 2181 "/> <! -- <Dubbo: Registry address = "zookeeper: // 192.168.36.238: 2181? Backup = 192.168.36.239: 2181,192.168 .36.240: 2181,192.168 .36.241: 2181,192.168 .36.242: 2181 "/> --> <! -- <Dubbo: Registry address = "zookeeper: // 192.168.50.247: 2181? Backup = 192.168.50.248: 2181,192.168 .50.249: 2181 "/> --> <! -- Expose services on port 1500 using Dubbo protocol --> <Dubbo: protocol name = "Dubbo" Port = "20000"/> <! -- Interface --> <! -- <Dubbo: Service timeout = "5000" interface = "com. wooboo. Service. Kafka. kafkaservie" ref = "kafkaserviceimpl"/> --> <! -- Implementation class --> <! -- <Bean id = "kafkaserviceimpl" class = "com. wooboo. service. kafka. kafkaserviceimpl "/> --> <Dubbo: Reference id =" kafkaservice "interface =" com. wooboo. service. kafka. kafkaservie "/> <Dubbo: Reference id =" productcacheservice "interface =" com. wooboo. service. product. service. productservice "timeout =" 50000 "/> <Dubbo: Reference id =" productcategoryservice "interface =" com. wooboo. service. productcategory. service. productcategoryservice "timeout =" 5000 "/> </beans>
In the preceding Configuration:
1 Reference ID: identifies the name of the class referenced by a specific project.
2 interface: indicates the name of the interface provided to us through the Dubbo service. Here, you usually need to register the interface name to the zookeeper service.
3 zookeeper address:
<dubbo:registryaddress="zookeeper://192.168.50.247:2181?backup=192.168.50.248:2181,192.168.50.249:2181" />
Next, please refer to a specific Applet: Get other information about the product based on the product ID
Package test; import Java. util. set; import Org. springframework. context. support. classpathxmlapplicationcontext; import COM. wooboo. common. bean. productcache; import COM. wooboo. common. bean. productcategorycache; import COM. wooboo. service. kafka. kafkaservie; import COM. wooboo. service. product. service. productservice; import COM. wooboo. service. productcategory. service. productcategoryservice; public class testdubbo {public static classpathxmlapplicationcontext context = NULL; static {context = new classpathxmlapplicationcontext (New String [] {"applicationcontext. XML "}); context. start ();} public static void main (string [] ARGs) throws exception {// serviceproductservice productservice = (productservice) Context of the product. getbean ("productcacheservice"); // serviceproductcategoryservice categoryservice = (productcategoryservice) Context of the product category. getbean ("productcategoryservice"); long goodsid = 627l; productcache = productservice. getproduct (goodsid); // long productcategoryid = cache. getproductcategoryid (); // productcategorycache categorycache = categoryservice. getproductcategory (productcategoryid); long parrent = categorycache. getparent (); set <long> child = categorycache. getchildren (); // system. err. println (parrent);} public static void P (Object O) {system. err. println (O. tostring ());}}
In this small program, please pay attention to our local
ProductCategoryService
This class must exist locally. In the process of calling a method, getbean () passes the ID name we configured in the XML file.
At this point, a small service call is complete. As for how to register a service with Dubbo and provide the service, please google it on your own.