Spring Boot整合Dubbo&Zookeeper

Dubbo is a remote service call framework (RPC) developed by Alibaba, which can transparently call remote services, just as simple as calling local services. Up to now, Dubbo has released a version built on Spring Boot, the version number is 0.2.0, which makes it easier and more convenient to integrate with the Spring Boot project. Zookeeper here acts as a service registration center. We register the services provided by each microservice to Zookeeper through Dubbo, and then service consumers obtain and consume the corresponding services from Zookeeper through Dubbo. The architecture diagram of the case in this article can be simply represented by the following diagram:

The final project structure of this case is shown in the figure below:

The project is built using Maven, and the role of each module:

Module description
common-api Define interfaces uniformly for reference by other sub-modules
server-provider The service provider implements the interface in the common-api module, and then exposes it to Zookeeper for use by service consumers
server-consumer Service consumers, obtain and consume services from Zookeeper through Dubbo

Environmental preparation

Zookeeper installation

The Zookeeper service needs to be started before building the project. Zookeeper download address: http://zookeeper.apache.org/releases.html#download .

After downloading, decompress and rename zoo_sample.cfg in the config directory to zoo.cfg (Zookeeper configuration file, the default port is 2181, which can be modified according to actual conditions). Then double-click zkServer.cmd in the bin directory to start it.

Build the parent module

Create a new Maven project, groupId is cc.mrbird, artifactId is dubbo-boot, and packaging is specified as pom. Then introduce Spring Boot, dubbo-spring-boot-starter and Zookeeper related dependencies:

<?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>cc.mrbird</groupId>
    <artifactId>dubbo-boot</artifactId>
    <packaging>pom</packaging>
    <version>1.0</version>

    <name>dubbo-boot</name>
    <description>Spring Boot-Dubbo-ZooKeeper</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <project.version>1.0</project.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </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.8</version>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Build Common-api

Create a new Maven module, artifactId is common-api, the directory structure is as follows:

pom.xml :

<?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>dubbo-boot</artifactId>
        <groupId>cc.mrbird</groupId>
        <version>1.0</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>common-api</artifactId>
</project>

The project only contains a HelloService interface:

package cc.mrbird.common.api;

public interface HelloService {
    String hello(String message);
}

At this point we can start to build service providers and service consumers.

Build Server-Provider

Create a new Maven module for exposing Dubbo service, artifactId is server-provider, and the directory structure is as follows:

The pom content 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">
    <parent>
        <artifactId>dubbo-boot</artifactId>
        <groupId>cc.mrbird</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>server-provider</artifactId>

    <dependencies>
        <dependency>
            <groupId>cc.mrbird</groupId>
            <artifactId>common-api</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>
</project>

Here we have introduced the common-api module for subsequent implementation of corresponding services.

We add an @EnableDubboannotation to the Spring Boot startup class to indicate that we want to enable the dubbo function:

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableDubbo
@SpringBootApplication
public class ProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
        System.out.println("complete");
    }
}

Then configure Dubbo in application.yml:

server:
  port: 8080

dubbo:
  application:
    # 服务名称,保持唯一
    name: server-provider
    # zookeeper地址,用于向其注册服务
  registry:
    address: zookeeper://127.0.0.1:2181
  #暴露服务方式
  protocol:
    # dubbo协议,固定写法
    name: dubbo
    # 暴露服务端口 (默认是20880,不同的服务提供者端口不能重复)
    port: 20880

If Zookeeper is a cluster, the spring.dubbo.registry.addressconfiguration is:

spring:
  dubbo:
    registry:
      address: zookeeper://127.0.0.1:2181?backup=127.0.0.1:2180,127.0.0.1:2182

Next, we cc.mrbird.provider.servicecreate an HelloServiceinterface implementation class under the path :

import cc.mrbird.common.api.HelloService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

@Service(interfaceClass = HelloService.class)
@Component
public class HelloServiceImpl implements HelloService {
    @Override
    public String hello(String message) {
        return "hello," + message;
    }
}

It is worth noting that the @Serviceannotations are provided by Dubbo com.alibaba.dubbo.config.annotation.Service, not the Spring one. Which interfaceClassrefers to the interface of the service to be published.

Through the above configuration, we have HelloServiceexposed the implementation of the interface to Zookeeper, and then we continue to create a service consumer to consume this service.

Build Server-Consumer

Create a new Maven module for consuming Dubbo services, the artifactId is server-consumer, and the directory structure is as follows:

The pom content 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">
    <parent>
        <artifactId>dubbo-boot</artifactId>
        <groupId>cc.mrbird</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>server-consumer</artifactId>

    <dependencies>
        <dependency>
            <groupId>cc.mrbird</groupId>
            <artifactId>common-api</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>
</project>

Similarly, we also add @EnableDubboannotations to the Spring Boot startup class to indicate that we want to enable the dubbo function.

Then configure Dubbo in application.yml:

server:
  port: 8081

dubbo:
  application:
    # 服务名称,保持唯一
    name: server-consumer
    # zookeeper地址,用于从中获取注册的服务
  registry:
    address: zookeeper://127.0.0.1:2181
  protocol:
    # dubbo协议,固定写法
    name: dubbo

As with the service provider, we need to specify the address of Zookeeper, and the protocol is dubbo.

Then we define one TestControllerto demonstrate service consumption:

import cc.mrbird.common.api.HelloService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Reference
    private HelloService helloService;

    @GetMapping("/hello/{message}")
    public String hello(@PathVariable String message) {
        return this.helloService.hello(message);
    }
}

@ReferenceInject the required interface through Dubbo's annotations, similar to Spring's @Autowired.

test

Start Server-Provider and Server-Consumer respectively, and visit http://localhost:8081/hello/mrbird :

The remote service call has been successful.

Here is just a simple understanding of the use of Dubbo through the integration of Spring Boot and Dubbo. It is only a reference point. For more detailed Dubbo configuration, please refer to the official document: http://dubbo.apache.org/zh-cn/docs/user/quick -start.html

 

 

 

Guess you like

Origin blog.csdn.net/u014225733/article/details/100856181