Dubbo is a high performance and excellent open source service framework of Alibaba company, which enables applications to achieve output and input functions of services through high performance RPC, and can be seamlessly integrated with Spring framework.
The above introduction comes from Baidu Encyclopedia, the specific dubbo related information can be found by themselves, this article only introduces SpringBoot simple integration of Dubbo.
1. Install the zookeeper
1.1 Download this document from the official website. 3.4.12 is used as an exampleMirrors.hust.edu.cn/apache/zook…
1.2 Download and decompress ZooKeeper
1.3 Go to the conf directory
1.4 Change the name of zoo_sample. CFG to zoo.cfg
1.5 Modify the content as follows. Note that I have decompressed ZooKeeper to disk E
The dataDir and dataDirLog properties can be modified to suit your needs.
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=E:\\zookeeper\\data
dataDirLog=E:\\zookeeper\\log
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
Copy the code
1.6 Start ZooKeeper, go to the bin directory, and double-click zkserver. CMD
Create project springboot_dubbo_server, add dubbo dependency to project, complete POM as follows:
<? xml version="1.0" encoding="UTF-8"? > <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> < modelVersion > 4.0.0 < / modelVersion > < groupId > com. Dalaoyang < / groupId > < artifactId > springboot_dubbo_server < / artifactId > < version > 0.0.1 - the SNAPSHOT < / version > < packaging > jar < / packaging > < name > springboot_dubbo_server < / name > <description>springboot_dubbo_server</description> <parent> <groupId>org.springframework.boot</groupId> The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 1.5.9. RELEASE < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> < project. Reporting. OutputEncoding > utf-8 < / project. Reporting. OutputEncoding > < Java version > 1.8 < / Java version > </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.dubbo.springboot</groupId> </dependencies> </dependencies> </dependencies> </dependencies> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>Copy the code
The configuration file is as follows:
# # the port number
server.port=8880
## Dubbo Service provider configurationSpring. Dubbo. Application. The name = dubbo_server spring. The dubbo. Registry. Address = zookeeper: / / 39.108.123.128:2181 spring.dubbo.protocol.name=dubbo spring.dubbo.protocol.port=20880 spring.dubbo.scan=com.dalaoyang.dubboCopy the code
Define a Service Interface: HelloService.java
package com.dalaoyang.dubbo;
/**
* @author dalaoyang
* @Description
* @project springboot_learn
* @package com.dalaoyang.dubbo
* @email [email protected]
* @date 2018/6/14
*/
public interface HelloService {
String SayHello(String name);
}
Copy the code
The implementation class of the interface: HelloServiceImp.java
package com.dalaoyang.dubbo.imp;
import com.alibaba.dubbo.config.annotation.Service;
import com.dalaoyang.dubbo.HelloService;
/**
* @author dalaoyang
* @Description
* @project springboot_learn
* @package com.dalaoyang.dubbo.imp
* @email [email protected]
* @date 2018/6/14
*/
@Service(version = "1.0.0")
public class HelloServiceImpl implements HelloService {
@Override
public String SayHello(String name) {
return "Hello , "+name; }}Copy the code
At this point the Dubbo service provider has been created.
3. Create a new project springboot_dubbo_client. The POM is the same as the provider, and the code is as follows:
<? xml version="1.0" encoding="UTF-8"? > <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> < modelVersion > 4.0.0 < / modelVersion > < groupId > com. Dalaoyang < / groupId > < artifactId > springboot_dubbo_client < / artifactId > < version > 0.0.1 - the SNAPSHOT < / version > < packaging > jar < / packaging > < name > springboot_dubbo_client < / name > <description>springboot_dubbo_client</description> <parent> <groupId>org.springframework.boot</groupId> The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 1.5.9. RELEASE < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> < project. Reporting. OutputEncoding > utf-8 < / project. Reporting. OutputEncoding > < Java version > 1.8 < / Java version > </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.dubbo.springboot</groupId> </dependencies> </dependencies> </dependencies> </dependencies> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>Copy the code
The configuration file is as follows:
# # the port number
server.port=8881
##Dubbo Service Consumer configurationSpring. Dubbo. Application. The name = dubbo_client spring. The dubbo. Registry. Address = zookeeper: / / 39.108.123.128:2181 spring.dubbo.scan=com.dalaoyang.controllerCopy the code
The HelloService interface is as follows:
package com.dalaoyang.dubbo;
/**
* @author dalaoyang
* @Description
* @project springboot_learn
* @package com.dalaoyang.dubbo
* @email [email protected]
* @date 2018/6/14
*/
public interface HelloService {
String SayHello(String name);
}
Copy the code
Create a controller to test, note that the version number is the same as the provider version number, and the Dubbo scan package is scanned to the class we want to use, as follows:
package com.dalaoyang.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.dalaoyang.dubbo.HelloService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author dalaoyang
* @Description
* @project springboot_learn
* @package com.dalaoyang.controller
* @email [email protected]
* @date 2018/6/14
*/
@RestController
public class HelloController {
@Reference(version = "1.0.0")
HelloService helloService;
@GetMapping("sayHello")
public String sayHello(String name){
returnhelloService.SayHello(name); }}Copy the code
At this point the Dubbo service caller is also created.
Respectively, start the service provider and service the caller projects, in your browser to http://localhost:8881/sayHello? Name =dalaoyang, as shown in the figure, to prove that the call succeeded.
For more information about springboot-dubbo, see github.com/JeffLi1993/…
Source download: Old Yang code cloud
Personal website: www.dalaoyang.cn