SpringBoot+Dubbo+Zookeeper implements a simple distributed architecture

preface

What is a distributed system?
The emergence of distributed system is to complete the calculation that can not be completed by a single computer in a more cost-effective way and more ordinary machines.
Four stages of architecture evolution
Single application architecture
Deploy all functions together to reduce deployment nodes and costs. However, it is not conducive to maintenance and upgrading.
Vertical application architecture
Split the application into multiple applications, and the communication mechanism is generally RESTful. But what? Although it is divided into multiple applications, it is inevitable that there are interactive parts between applications, which promotes the birth of the next architecture.
Distributed service architecture
Extract the core business and deploy it as an independent service for multiple vertical applications. Distributed service framework (RPC) is the key. RPC [Remote Procedure Call] refers to Remote Procedure Call. Common services can be deployed in the server and calls between services can be realized by RPC.
Flow computing architecture
Add a dispatching center to manage the cluster capacity in real time based on the access pressure.

Build a framework

1. Create a module springboot Dubbo zookeeper and use the web dependency of springboot.
2. Write an interface:

package com.he.Service;
public interface TicketService {
    public String getTicket();
}

3. Write an implementation class:
Here @ Service means to register in zookeeper

package com.he.Service;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

//Service registration and discovery. The new version is @ DubboService
@Service
@Component
public class TicketServiceImpl implements TicketService {
    @Override
    public String getTicket() {
        return "ticket";
    }
}

4. In application Configure the registry address in properties and scan the specified package. The called method is to add the configuration Dubbo of the service under the scanning specified package scan. Base packages can only be used after they are registered in zookeeper and spring. When calling, the address of the registration center is the same as that of the called configuration

server.port=8081
#Current app name
dubbo.application.name=provider-server
#Address of Registration Center
dubbo.registry.address=zookeeper://127.0.0.1:2181
#Scan the services under the specified package
dubbo.scan.base-packages=com.he.Service

5. Add the annotation @ EnableDubbo in the startup file of springboot
6. Import dependent packages

<!-- Dubbo Spring Boot Starter -->
<dependency>
   <groupId>org.apache.dubbo</groupId>
   <artifactId>dubbo-spring-boot-starter</artifactId>
   <version>2.7.3</version>
</dependency>    
<dependency>
   <groupId>com.github.sgroschupf</groupId>
   <artifactId>zkclient</artifactId>
   <version>0.1</version>
</dependency>
<!-- introduce zookeeper -->
<dependency>
   <groupId>org.apache.curator</groupId>
   <artifactId>curator-framework</artifactId>
   <version>2.12.0</version>
</dependency>
<dependency>
   <groupId>org.apache.curator</groupId>
   <artifactId>curator-recipes</artifactId>
   <version>2.12.0</version>
</dependency>
<dependency>
   <groupId>org.apache.zookeeper</groupId>
   <artifactId>zookeeper</artifactId>
   <version>3.4.14</version>
   <!--Resolve log conflicts-->
   <exclusions>
       <exclusion>
           <groupId>org.slf4j</groupId>
           <artifactId>slf4j-log4j12</artifactId>
       </exclusion>
   </exclusions>
</dependency>

7. Create a new module to call the previous module and import the same package
8. Configure dubbo related attributes

server.port=8082
#Current app name
dubbo.application.name=consumer-server
#Address of Registration Center
dubbo.registry.address=zookeeper://127.0.0.1:2181

9. Create the called class

package com.he.Service;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

@Service //Inject into container
public class Consumerservice {
    @Reference //If the specified service is referenced remotely, it will match according to the full class name to see who has registered the full class name to the registry
    TicketService ticketService;

    public void bugTicket(){
        String ticket = ticketService.getTicket();
        System.out.println("Buy it at the registry"+ticket);
    }
}

10. Write test class

package com.he;

import com.he.Service.Consumerservice;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringbootConsumerApplicationTests {

    @Autowired
    Consumerservice consumerservice;
    @Test
    void contextLoads() {
        consumerservice.bugTicket();
    }

}

11. Note that the paths here need to be the same, and the first service interface needs to be added to prevent error reporting.

Start test

Start zookeeper, start the first module springboot Dubbo zookeeper, and then start the test method. The zookeeper here is equivalent to a registry, which is used to receive the registration of the first module and make it callable. Final output

Keywords: Spring Boot Zookeeper Distribution

Added by Jak-S on Thu, 24 Feb 2022 17:03:34 +0200