SpringBoot+Dubbo+Zookeeper implements a simple distributed architecture


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
public class TicketServiceImpl implements TicketService {
    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

#Current app name
#Address of Registration Center
#Scan the services under the specified package

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

<!-- Dubbo Spring Boot Starter -->
<!-- introduce zookeeper -->
   <!--Resolve log conflicts-->

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

#Current app name
#Address of Registration Center

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;

class SpringbootConsumerApplicationTests {

    Consumerservice consumerservice;
    void contextLoads() {


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