Preface
Due to work reasons, it is necessary to integrate dubbo into the project, so I checked the dubbo related documents and found that dubbo has not been updated at present, so I turned my attention to dubbox. Dubbox is a project developed by Dangdang.com based on dubbo, dubbox. Because one of the company's projects is based on spring mvc 3.0 and the other is based on spring boot, and spring boot has relatively fewer documents, so this article records how to inherit dubbox under spring boot
1. Install zookeeper
1. Introduction to zookeeper
ZooKeeper is a distributed, open source distributed application coordination service. It is an open source implementation of Google's Chubby and an important component of Hadoop and Hbase. It is a software that provides consistent services for distributed applications, and its functions include: configuration and maintenance, domain name services, distributed synchronization, group services, etc.
ZooKeeper's goal is to package complex and error-prone key services, and provide users with simple and easy-to-use interfaces and efficient and stable systems.
Dubbo is a distributed framework that relies on zookeeper. Of course, the secondary development dubbox will definitely rely on zookeeper, so we need to install zookeeper first.
2. Download zookeeper
zookeeper official website address http://zookeeper.apache.org/
Download address http://apache.fayea.com/zookeeper/
Local download address is more convenient: //www.VeVB.COM/softs/578345.html
3. Install zookeeper
Because I configured it in the Windows environment, I will briefly talk about the configuration below Windows. First, unzip the compressed package, then enter the conf folder, copy zoosample.cfg to create a copy, and then rename it to zoo.cfg, because zookeeper only recognizes zoo.cfg, and the default is not available. zoosample.cfg is the default configuration file, but because of the file name, zookeeper cannot recognize it. Of course, it is OK to directly rename zoo_sample.cfg, it just depends on your favorite.
4. Start zookeeper
Just run zkServer.cmd in the bin directory directly in the Windows environment. If it is a Linux environment, run it in the bin directory.
./zkServer.sh start
zookeeper can be started
5. Add dubbox dependencies
<dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.8.4</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.9</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency>
Note that the 2.8.4 version of dubbo here is compiled by myself, which is not available in the maven repository because dubbo is no longer updated, while 2.8.4 is Dangdang.com's dubbox, so if you want to use it, either compile dubbox or use the old version of dubbo
6. Add the interface class of the service provider:
package wang.raye.dubbo.interfaces;public interface DubboInterface { public String hello(String name);}Interface implementation class:
package wang.raye.dubbodemo1;import org.springframework.stereotype.Service;import wang.raye.dubbo.DubboInterface; @Servicepublic class DubboImpl implements DubboInterface { public String hello(String name) { return "hello "+name+" this is dubbodemo1"; }}7. Add dubbo configuration xml xml file to the sources folder. You can name the name at will. Here is dubbo.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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd" default-lazy-init="false" > <!-- Provider application name information, this is equivalent to giving a name. Our dubbo management page is clearer which application exposed-> <dubbo:application name="dubbo-provider1"></dubbo:application> <!--Expose the interface using annotation<dubbo:annotation package="wang.raye.dubbodemo1" /> --> <!--Expose the service address using zookeeper registration center--> <dubbo:registry address="zookeeper://192.168.1.126:2181" check="false" subscribe="false" register=""></dubbo:registry> <!-- Exposure the interface to be exposed--> <dubbo:service interface="wang.raye.dubbo.interfaces.DubboInterface" ref="dubboImpl" /> </beans>
Every node here has an explanation, I believe it will not be too much explanation
8. Configure consumers
References to remote providers in class
package wang.raye.dubbodemo3.controller;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody;import wang.raye.dubbo.DubboInterface;@Controllerpublic class DubboControll { @Autowired private DubboInterface interface1; @RequestMapping("/hello") @ResponseBody public String hello(String name){ return interface1.hello(name); }}Consumer's xml configuration
<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, not matching conditions, do not be the same as the provider --> <dubbo:application name="dubbo-custom-dsp"/> <dubbo:registry address="zookeeper://192.168.1.126:2181" /> <!-- Generate remote service agent, you can use demoService like local beans --> <dubbo:reference id="dubboService" interface="wang.raye.dubbo.interfaces.DubboInterface" /> </beans>
There are comments here, so there should be no need to say more. Moreover, this article does not pay attention to dubbo configuration, but mainly talks about spring boot integration dubbox
9. Reference dubbo configuration xml to add annotations to Spring boot Application class
@ImportResource({"classpath:dubbo.xml"})Because my xml name is dubbo.xml, when you use it, you need to change it to your own xml name. Note: The Application class of the consumer project and the service provider project needs to be configured with this annotation.
Ending
At this point, the spring boot integration duubox is finished. Of course, it must be empty to say this, so the project I tested was uploaded to github. You can refer to it. Of course, if you want to test it, you need to modify the zookeeper address in the dubbo:registry node of dubbo.xml.
Demo address, in order to show the advantages of dubbo, I created two service providers. If I call frequently, I will automatically select any of these two, and even avoid load balancing. It should be done by zookeeper itself, so I feel it is quite good.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.