1, Explanation of terms (from Baidu Encyclopedia):
Spring Boot is a new framework provided by pivot team. It is designed to simplify the initial construction and development process of new spring applications. The framework uses a specific way to configure, so that developers no longer need to define templated configurations.
Dubbo is a high-performance and excellent open source service framework of Alibaba company, which enables applications to realize service output and input functions through high-performance RPC, and can be seamlessly integrated with Spring framework.
ZooKeeper is a distributed, open-source distributed application coordination service. It is an open-source implementation of Chubby of Google and an important component of Hadoop and Hbase. It is a software that provides consistency services for distributed applications. Its functions include configuration maintenance, domain name service, distributed synchronization, group service, etc.
2, Project construction steps:
1. Install the local Zookeeper registry at: Apache Downloads
After downloading, unzip it, and then open the configuration and copy a zoo_sample.cfg, change the name to zoo.cfg, and open zoo_ CFG, modify the data saving path, as shown in the figure below, to a locally available path, and create the corresponding folder directory without:
Start zookeeper service, as shown in the following figure:
So far, Zookeeper has been started successfully.
2. Open the IDEA development tool, create a parent project based on maven SpringBoot, and then create three sub module modules based on the parent project. The final project structure is as follows:
1. Open the parent project pom.xml file and add the three sub modules just created, as shown in the following figure:
Note: < packaging > POM < / packaging > needs to be added manually.
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.0.RELEASE</version> <relativePath/> </parent> <groupId>org.example</groupId> <artifactId>springboot-dubbo-demo</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>dubbo-api</module> <module>dubbo-provider</module> <module>dubbo-customer</module> </modules> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies>
2. Dubbo API module
Add a new service interface class HelloService with the following code:
public interface HelloService { String sayHello(String name); }
The pom.xml code in Dubbo API is as follows:
<parent> <artifactId>springboot-dubbo-demo</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>dubbo-api</artifactId> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties>
3. Dubbo provider module
Add the implementation interface class HelloServiceImpl. The code is as follows:
import com.alibaba.dubbo.config.annotation.Service; import com.api.service.HelloService; @Service //Publishing services public class HelloServiceImpl implements HelloService { @Override public String sayHello(String name) { return "Hello, " + name; } }
Add dubbo annotation to the DubboProviderApplication startup class in dubbo provider. The code is as follows:
@EnableDubbo @SpringBootApplication public class DubboProviderApplication { public static void main(String[] args) { SpringApplication.run(DubboProviderApplication.class, args); } }
pom.xml code in Dubbo provider is as follows:
<parent> <artifactId>springboot-dubbo-demo</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>dubbo-provider</artifactId> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <java.version>1.8</java.version> <zookeeper.version>3.4.13</zookeeper.version> <dubbo.version>0.2.0</dubbo.version> </properties> <dependencies> <dependency> <artifactId>dubbo-api</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>${dubbo.version}</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>${zookeeper.version}</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency> </dependencies>
The configuration of application.yml in Dubbo provider is as follows:
server: port: 8000 dubbo: application: name: dubbo-provider protocol: name: dubbo port: 20880 registry: address: zookeeper://127.0.0.1:2181
be careful:
- @Service is the annotation of dubbo publishing service;
- @EnableDubbo is the annotation to start the dubbo service;
- Log4j and slf4j-log4j12 need to be excluded, otherwise they will conflict with zookeeper;
4. Dubbo customer module
A new control class HelloController is added to facilitate testing. The code is as follows:
@RestController public class HelloController { @Reference //Reference service private HelloService helloService; @GetMapping("/sayHello") private String sayHello(@RequestParam String name){ System.out.println("call sayHello succeed..." + " name:" + name); return helloService.sayHello(name); } }
The pom.xml code in Dubbo customer is as follows:
<parent> <artifactId>springboot-dubbo-demo</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>dubbo-customer</artifactId> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <java.version>1.8</java.version> <zookeeper.version>3.4.13</zookeeper.version> <dubbo.version>0.2.0</dubbo.version> </properties> <dependencies> <dependency> <artifactId>dubbo-api</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </dependency> <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>${dubbo.version}</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>${zookeeper.version}</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency> </dependencies>
The configuration of application.yml in Dubbo customer is as follows:
server: port: 9000 dubbo: application: name: dubbo-customer registry: address: zookeeper://127.0.0.1:2181
Note: @ Reference is the annotation of dubbo Reference service;
So far, the distributed project framework based on dubbo has been built
3, Project consumption service function test:
1) First run the startup service provider Dubbo provider module
2) Then start the service consumer Dubbo customer module
3) The test results are as follows: