Springboot integrates Dubbo and Zookeeper to achieve distributed development

1. Installation of Zookeeper

1. Overview of Zookeeper

ZooKeeper is a distributed coordination service used to manage large hosts. Coordinating and managing services in a distributed environment is a complex process. ZooKeeper solves this problem through its simple architecture and API. ZooKeeper allows developers to focus on the core application logic without worrying about the distributed nature of the application.

The ZooKeeper framework was originally built on "Yahoo!" to access their applications in a simple and robust way. Later, Apache ZooKeeper became the standard for organized services used by Hadoop, HBase, and other distributed frameworks. For example, Apache HBase uses ZooKeeper to track the state of distributed data.

(More information: https://www.w3cschool.cn/zookeeper/zookeeper_overview.html)

2. Installation and startup of Zookeeper

Go to the official website: https://archive.apache.org/dist/zookeeper/ to
download the corresponding installation package, I choose the zookeeper-3.3.6 version. Other higher versions, such as zookeeper-3.5.8 and zookeeper-3.6.2 may not start on Windows 10 (pro-test), you can try if you are not satisfied. The reason I did not explore.
Insert picture description here
After the download is complete, change zoo_sample.cfg to zoo.cfg in the zookeeper-3.3.6\conf\ directory,
Insert picture description here
and then double-click the zkServer.cmd file in the zookeeper-3.3.6\bin directory to start zookeeper (Windows)

As shown in the figure, the startup is successful, it will always stop in that cmd window (indicating that the service is running) until you close the command window (the service is stopped)
Insert picture description here

2. Springboot integrates Dubbo and Zookeeper to achieve distributed development

Dubbo is a high-performance and excellent service framework open sourced by Alibaba, which enables applications to realize service output and input functions through high-performance RPC, and can be seamlessly integrated with the Spring framework.

Dubbo is a high-performance, lightweight open source Java RPC framework that provides three core capabilities: interface-oriented remote method invocation, intelligent fault tolerance and load balancing, and automatic service registration and discovery.

1. Create the service interface module dubbotest-api

Generally speaking, the
Insert picture description here
dubbotest-api project is a maven project, which mainly defines interfaces and entity classes to provide interfaces, similar to the dao layer of the database.
The main code 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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>dubbotest-api</artifactId>
    <!-- 打包成正式版,这样生产者与消费者就可以引用 -->
    <version>1.0.0</version>
</project>

UserService.java

package com.example.duubo.service;

/*
*  springboot接口
*  ye
*  2020.12.04
* */
public interface UserService {
    
    
    public String hello(String name);
}

2. Develop the dubbotest-provider producer module

dubbotest-provider is a producer that can provide services by implementing the dubbotest-api interface, or you can create your own services. At this point, it is similar to the service layer of springboot. The
main code 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 https://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.4.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>dubbotest-provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dubbotest-provider</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <!--dubbo -->
        <dependency>
            <groupId>com.alibaba.spring.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
        <!-- zookeeper -->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>

        <!-- 接口的jar包 -->
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>dubbotest-api</artifactId>
            <version>1.0.0</version>
        </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>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

application.properties

# 端口
server.port = 8081

# Dubbo配置,不能少,工程的名字
spring.application.name=dubbotest-provider

# 表示提供者,可以省略
spring.dubbo.server=true

# 注册中心地址
spring.dubbo.registry=zookeeper://localhost:2181

Entry class

package com.example.dubbotestprovider;

import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubboConfiguration  //开启自动配置支持
public class DubbotestProviderApplication {
    
    

    public static void main(String[] args) {
    
    
        SpringApplication.run(DubbotestProviderApplication.class, args);
    }

}

UserServiceImpl.java

package com.example.dubbotestprovider.serviceimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.example.duubo.service.UserService;
import org.springframework.stereotype.Component;

/*
*  服务提供者
*  ye
*  2020.12.04
* */

@Component
// 相当于 <dubbo:service interface="" ref="">
@Service(interfaceClass = UserService.class,timeout = 10000)
public class UserServiceImpl implements UserService {
    
    
    @Override
    public String hello(String name) {
    
    
        return "你好呀~"+name;
    }
}

3. Develop dubbotest-consumer consumer module

The consumer is a role that uses the service of the producer, similar to the controller layer of springboot. The
specific code 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 https://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.4.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>dubbotest-consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dubbotest-consumer</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <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>

        <!--dubbo -->
        <dependency>
            <groupId>com.alibaba.spring.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>

        <!-- zookeeper -->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>


        <!-- 接口的jar包 -->
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>dubbotest-api</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

application.properties

# 端口
server.port = 8082

# Dubbo配置,不能少
spring.application.name=dubbotest-consumer

# 表示提供者,可以省略
spring.dubbo.appname=springboot-dubbo-consumer

# 注册中心地址
spring.dubbo.registry=zookeeper://localhost:2181

Entry class

package com.example.dubbotestconsumer;

import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubboConfiguration  //开启自动配置支持
public class DubbotestConsumerApplication {
    
    

    public static void main(String[] args) {
    
    
        SpringApplication.run(DubbotestConsumerApplication.class, args);
    }

}

UserController.java

package com.example.dubbotestconsumer.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.example.duubo.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/*
*  消费者
*  ye
*  2020.12.04
*
* */

@RestController
public class UserController {
    
    

    //引用远程的dubbo服务 <dubbo:refrence id="" interface="">
    @Reference
    private UserService userService;

    @GetMapping("/dubbotest")
    public String hello(){
    
    
        return userService.hello("Chasing stars");
    }
}

4. Test

Open the zookeeper service, run the producer and consumer modules, enter http://localhost:8082/dubbotest in the browser, and the
following picture appears: it
Insert picture description here
means the test is successful!

Reference material: https://www.w3cschool.cn/zookeeper/zookeeper_overview.html
Reference material: https://baike.baidu.com/item/Dubbo/18907815?fr=aladdin

Guess you like

Origin blog.csdn.net/weixin_43520670/article/details/110675604