Project directory structure:
Step 1: Create a SpringBoot project. Either Maven project or Spring Initializer can be selected here. A Maven project (SpringBoot-Dubbo) is created here. The pom.xml file 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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ly</groupId>
<artifactId>SpringBoot-Dubbo</artifactId>
<packaging>pom</packaging>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.3.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
Step 2: Add a Module, take dubbo-provider as an example,
you can use Maven to create or use Spring Initializr:
Step 3: Service provider dubbo-provider related configuration
Add dubbo usage dependencies and zookeeper dependencies in the pom.xml file:
<?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">
<parent>
<artifactId>SpringBoot-Dubbo</artifactId>
<groupId>com.ly</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.qunhongtech</groupId>
<artifactId>dubbo-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-customer</name>
<modelVersion>4.0.0</modelVersion>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.3.0.RELEASE</version>
</dependency>
<!--dubbo依赖-->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<!--zookeeper客户端依赖-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.13</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
Modify the configuration file: application.yml
server:
port: 8081 #Tomcat端口号
dubbo:
application:
name: dubbo-provider #应用名称
protocol:
name: dubbo
port: 20880 #dubbo服务暴漏的端口号
registry:
address: zookeeper://127.0.0.1:2181 #zookeeper服务的地址以及端口号
Step 4: Service consumer dubbo-customer related configuration
Add dubbo usage dependencies and zookeeper dependencies in the pom.xml file:
<?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">
<parent>
<artifactId>SpringBoot-Dubbo</artifactId>
<groupId>com.ly</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.ly</groupId>
<artifactId>dubbo-customer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-customer</name>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.13</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
Modify the configuration file: application.yml
server:
port: 8082 #Tomcat端口号
dubbo:
application:
name: dubbo-customer #应用名称
registry:
address: zookeeper://127.0.0.1:2181 #zookeeper服务的地址以及端口号
Step 5: Create a new interface in dubbo-api and implement it in dubbo-provider
Create an interface in dubbo-api: HelloService, you need to add dependencies in dubbo-provider and dubbo-customer when using:
public interface HelloService {
String hello();
}
Implement the HelloService interface in dubbo-provider:
Create the interface implementation class HelloServiceImpl to implement the HelloService interface: The implementation code is as follows, note that the annotation @Service used here is not the service annotation of spring, but the annotation of dubbo:import com.alibaba.dubbo.config.annotation.Service;
@Service //发布服务
public class HelloServiceImpl implements HelloService {
@Override
public String hello() {
return "Hello World...";
}
}
And add an annotation to the launcher DubboProviderApplication: @EnableDubbo
Step 6: Try to call the HelloService interface remotely in dubbo-customer:
Create a new HelloController class, the code is as follows, you need to use dubbo's Reference annotation when you want to introduce a remote service:
@RestController
public class HelloController {
@Reference //引用服务
private HelloService helloService;
@GetMapping("/hello")
private String hello(){
System.out.println("Hello World...");
return helloService.hello();
}
}
Test the code, first start the service provider: dubbo-provider, an error occurs because we have not started zookeeper
Download and install zookeeper, open the connection and select the appropriate version to install directly: https://zookeeper.apache.org/releases.html
The downloaded compressed package can be used in linux or windows. Here is Windows as an example:
unzip the compressed package, find the bin directory and open it: the
directory is as follows, find zkServer.cmd, and double-click to start it:
The results are as follows:
Start again in sequence: 服务提供发:dubbo-provider
, 服务消费方:dubbo-customer
, and open the browser for testing. The results are as follows: