Environment installation
Dubbo uses zookeeper as the registration center. You must first install zookeeper.
Install zookeeper on Windows as follows:
https://blog.csdn.net/qq_33316784/article/details/88563482
Install zookeeper on Linux as follows:
https://www.cnblogs.com/expiator/p/9853378.html
SpringBoot new project
If you still don’t know how to create a new SpringBoot project, you can refer to: https://www.cnblogs.com/expiator/p/15844275.html
Service providerDubbo-provider
Created dubbo-provider module as a service provider.
Dubbo has multiple groupIds, among which the version of org.apache.dubbo is more suitable for SpringBoot2.X, and the annotations will not expire or become invalid.
Introduce the org.apache.dubbo package, and set the version number of dubbo to 2.7.5.
pom.xml 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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>dubbo-provider</artifactId> <version>1.0-SNAPSHOT</version> <name>dubbo-provider</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-boot.version>2.2.2.RELEASE</spring-boot.version> <dubbo.version>2.7.5</dubbo.version> <curator.version>4.2.0</curator.version> <zookeeper.version>3.4.12</zookeeper.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> <!-- dubbo, please use the groupId of org.apache.dubbo --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>${dubbo.version}</version> </dependency> <!-- zookeeper dependency --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>${zookeeper.version}</version> </dependency> <!-- zookeeper's api management dependencies --> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>${curator.version}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> </project>
application.yml
Add application.yml under the resources file and configure it as follows:
dubbo: application: # Application Name name: dubbo-provider protocol: #Protocol name name: dubbo #Protocol port port: 20880 registry: # Registration center address address: zookeeper://127.0.0.1:2181 server: # Modify the port number to avoid port conflicts port: 8081
pojo class:
@Data is a lombok annotation, equivalent to getter, setter, toString and other methods.
@NoArgsConstructor is also a lombok annotation, which is equivalent to a constructor without parameters.
@AllArgsConstructor is the constructor that contains all parameters.
If you don’t use lombok, you can replace it with the corresponding method.
@Data @NoArgsConstructor @AllArgsConstructor public class User implements Serializable { private static final long serialVersionUID = -4294369157631461921L; Long userId; String userName; String userInfo; }
service:
public interface UserService { String getUserInfo(); User getUserById(String userId); }
service implementation class:
Note that the @service annotation, the introduced package is org.apache.dubbo.config.annotation.Service
import org.apache.dubbo.config.annotation.Service; import org.springframework.stereotype.Component; /** * Note that for the @service annotation, the introduced package is org.apache.dubbo.config.annotation.Service */ @Service @Component public class UserServiceImpl implements UserService { @Override public String getUserInfo() { return "userTest"; } @Override public User getUserById(String userId) { User user = new User(); user.setUserId(Long.valueOf(userId)); user.setUserInfo("test"); user.setUserName("lin"); return user; } }
SpringBoot startup class:
Notice:
/** * The application that provides the service configures the DubboComponentScan annotation and specifies the file where the scanned service is located. */ @SpringBootApplication @DubboComponentScan("com.example.service") public class DubboProviderApplication { public static void main(String[] args) { SpringApplication.run(DubboProviderApplication.class, args); } }
install service provider dubbo-provider
In the idea, for dependencies on the service provider dubbo-provider, use Maven to clean and install it to the Maven repository, so that it can be provided to other modules.
Service consumer dubbo-consumer
Create a new maven project for service consumer dubbo-consumer.
pom.xml
In pom.xml, the dependency of the service consumer dubbo-consumer is similar to the previous one, except that the dependency of the service provider dubbo-provider must be introduced so that the Service can be called.
<!-- Introduce service provider dependencies, pay attention to the version number --> <dependency> <groupId>com.example</groupId> <artifactId>dubbo-provider</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
application.yml
Similar to dubbo-provider, you can change the port number to 8082 to avoid port number conflicts.
ConsumerController class:
import com.example.pojo.User; import com.example.service.UserService; import org.apache.dubbo.config.annotation.Reference; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/user") public class ConsumerController { /** * Call the remote service through the @Reference annotation, which is the service of the service provider dubbo-provider */ @Reference private UserService userService; @GetMapping("/info") public String getUserById() { return userService.getUserInfo(); } @GetMapping("/{id}") public User getUserById(@PathVariable String id) { return userService.getUserById(id); } }
DubboConsumerApplication startup class:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DubboConsumerApplication { public static void main(String[] args) { SpringApplication.run(DubboConsumerApplication.class, args); } }
Start project
- First start zookeeper.
Install and start zookeeper on Windows as follows:
https://blog.csdn.net/qq_33316784/article/details/88563482
Install and start zookeeper on Linux as follows:
https://www.cnblogs.com/expiator/p/9853378.html
If zookeeper is not started, an error will be reported:
Caused by: java.lang.IllegalStateException: zookeeper not connected at org.apache.dubbo.remoting.zookeeper.curator.CuratorZookeeperClient.<init>(CuratorZookeeperClient.java:80) ~[dubbo-2.7.5.jar:2.7.5] ... 32 common frames omitted \t
- The port number is occupied and needs to be modified, otherwise an error will be reported.
Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that's listening on port 8080 or configure this application to listen on another port.
- Start error:
Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.
Solution: https://www.cnblogs.com/expiator/p/15837518.html
- Start the Provider first, then the Consumer
The successful startup is as follows:
- Open the browser and access the interface of ConsumerController
Note that the port number used is that of the service consumer.
http://localhost:8082/user/123
Reference materials:
https://www.jb51.net/article/178946.htm