Java study notes-Day87 Dubbo distributed service framework


1. Evolution of application system architecture


Technology is born for business, and architecture also emerges for business. With the development of business and the growth of the number of users, the number of systems has increased, and the call dependencies have become more complex. In order to ensure the requirements of high system availability and high concurrency, the system architecture has slowly migrated from the monolithic era to the service SOA era. According to the different requirements of different services on system resources, we can configure system resources more reasonably to maximize the utilization of system resources.

(1) Single application framework :
When the website traffic is very small, only one application is needed, and all functions such as single payment etc. are deployed together to reduce deployment nodes and costs. In this case, for simplifying CRUD workload data access framework (ORM) is the key. Disadvantages: The single system architecture makes more and more resources occupied during the development process, and it becomes more and more difficult to maintain as the traffic increases.

(2) Vertical application framework :
When the number of visits gradually increases, the acceleration caused by the increase of a single application by the machine becomes smaller and smaller. The application is divided into several unrelated applications to improve efficiency. At this time, the web framework (MVC) used to accelerate the development of front-end pages is the key. In the vertical architecture, the same logic code needs to be copied continuously and cannot be reused.

(3) Distributed application architecture :
When there are more and more vertical applications, the interaction between applications is inevitable. The core business is extracted as an independent service, and a stable service center is gradually formed, so that front-end applications can respond more quickly. Changing market demand. In this case, for improving service multiplexing and integration of distributed service framework (RPC) is the key.

(4) flow computing architecture :
With the further development of the service, more and more service calls and dependencies between services are increasingly complex, the birth of architecture service-oriented architecture (SOA Service-Oriented Arthitecture) , Therefore, a series of corresponding technologies have been derived, such as a service framework that encapsulates behaviors such as service provision, service invocation, connection processing, communication protocol, serialization method, service discovery, service routing, and log output. At this time, the resource scheduling and governance center (SOA) used to improve machine utilization is the key. Microservices are the evolution of SOA architecture.

Insert picture description here

2. Introduction to Dubbo


Dubbo is a high-performance and excellent distributed service framework open sourced by Alibaba. It enables applications to realize service output and input functions through high-performance and transparent RPC, and can be seamlessly integrated with the Spring framework. Dubbo provides support for more than 3 billion visits to more than 2,000 services every day, and is widely used in the business of various member sites of the Alibaba Group and other companies.

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.

Main core components:
(1) Remoting: network communication framework, which implements sync-over-async and request-response message mechanisms.
(2) RPC: An abstraction of remote procedure calls that supports load balancing, disaster recovery and cluster functions.
(3) Registry: The service catalog framework is used for service registration and service event publishing and subscription.

Three, Dubbo's architecture


Insert picture description here
Dubbo service call process:

  • 0: The service container is responsible for starting, loading, and running the service provider.
  • 1: When the service provider starts, it registers the service it provides with the registration center.
  • 2: Service consumers subscribe to the registration center for the services they need when they start.
  • 3: The registration center returns the list of service provider addresses to the consumer. If there is a change, the registration center will push the change data to the consumer based on the long connection.
  • 4: Service consumer, from the provider address list, based on the soft load balancing algorithm, select one provider to call, and if the call fails, select another call.
  • 5: Service consumers and providers accumulate the number of calls and call time in the memory, and send statistical data to the monitoring center every minute.

Dubbo registration center: For service providers, it needs to publish services, and due to the complexity of the application system, the number and types of services are also expanding; for service consumers, it is most concerned about how to obtain the services it needs. For complex application systems, a large number of service calls need to be managed. For service providers and service consumers, they may also have both roles, that is, they need to provide services and consume services. Through unified management of services, the process and management of internal applications for service release/use can be effectively optimized. The service registry can complete the unification of services to the outside through a specific agreement.

The registry provided by Dubbo has the following types: Multicast registry, Zookeeper registry, Redis registry, Simple registry.

Fourth, the advantages and disadvantages of Dubbo


Advantages of Dubbo:

  • Transparent remote method invocation.
  • Call remote methods like local methods (just simple configuration, no API intrusion).
  • Soft load balancing and fault tolerance mechanism.
  • It can replace hardware load balancers such as nginx lvs in the intranet.
  • Service registration center automatic registration & configuration management.
  • There is no need to write down the service provider address, the registration center will automatically query the provider IP based on the interface name.
  • Using distributed coordination services such as zookeeper as the service registry, most project configurations can be moved into the zookeeper cluster.
  • Service interface monitoring and governance.
  • Dubbo-admin and Dubbo-monitor provide complete service interface management and monitoring functions. For different interfaces of different applications, multi-version, multi-protocol, and multi-registration management can be carried out.

Disadvantages of Dubbo:

  • Only JAVA language is supported.

Five, the use of Dubbo

1. Build a zookeeper registration center


(1) Visit the website https://hub.docker.com/and enter zookeeper in the search box to search.

Insert picture description here
(2) by docker pull zookeeperthe mirror zookeeper pulled local repository.

Insert picture description here

(3) by docker run --name 自定义名 --restart always -d -p 2181:2181 镜像编号starting container. For docker run --name zk01 --restart always -d -p 2181:2181 a7dfca58680aexample: .

Insert picture description here

2. Create service providers and consumers

2.1. Create a jar package of public classes and interfaces


(1) First create an empty project named dubbo, New -> Project -> Empty Project.

Insert picture description here
(2) Create a commons module in an empty project. Select the Module SDK of the module, then select maven-archtype-quickstart, and click Next.
Insert picture description here
Insert picture description here
Insert picture description here
(3) Create public classes and interfaces for the service provider and consumer.

  • User.java
package com.etc.entity;

import java.io.Serializable;

public class User implements Serializable {
    
    
    private Integer id;
    private String name;

    @Override
    public String toString() {
    
    
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

    public Integer getId() {
    
    
        return id;
    }

    public void setId(Integer id) {
    
    
        this.id = id;
    }

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }
}
  • UserService.java
package com.etc.service;

import com.etc.entity.User;

public interface UserService {
    
    
    public User getUserById(Integer id);
}

(4) Click the Maven option on the right side of the window, find the install of commond, and double-click install with the mouse.
Insert picture description here
(5) After the install operation of commmons is completed, the Target directory will be generated, and there is a jar file in the Target directory.
Insert picture description here

2.2, create a Provider service provider


(1) Create a dubboprovide module in the dubbo project. The module type is Spring Intializr, and Spring Web dependency is added.

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
(2) Click the dubboprovide module first, and then click the Shift key twice. Enter the maven keyword in the search box, click the Add Maven Projects option, and select the pom.xml file of the dubboprovide module. After completion, the Maven option of the dubboprovide module will appear on the right.

Insert picture description here
Insert picture description here
Insert picture description here

(3) Add dependency in the pom.xml file of the dubboprovide module.

<!-- Dubbo Spring Boot Starter -->
<dependency>
	<groupId>com.alibaba.boot</groupId>
	<artifactId>dubbo-spring-boot-starter</artifactId>
	<version>0.2.1.RELEASE</version>
</dependency>
<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>dubbo</artifactId>
	<version>2.6.5</version>
</dependency>
<!--Zookeeper客户端Curator-->
<dependency>
	<groupId>org.apache.curator</groupId>
	<artifactId>curator-recipes</artifactId>
	<version>2.12.0</version>
</dependency>

(4) Add the jar package commons-1.0-SNAPSHOT.jar of common classes and interfaces to the src directory of the dubboprovide module. Right-click the jar package and click Add as Library.

Insert picture description here
(5) Modify the application.properties configuration file.

#端口
server.port=8081
#appname
spring.application.name=dubbo-provider-user
#dubbo app name
dubbo.application.name=dubbo-provider-user
#扫描的包
dubbo.scan.base-packages=com.etc.dubboprovide.service
#注册中心地址
dubbo.registry.address=zookeeper://192.168.170.128:2181

(6) Create a Service implementation class. The implementation class has two @Service annotations, one is Spring's @Service annotation, and the other is Dubbo's @Service annotation (supports remote calls).

package com.etc.dubboprovide.service.impl;

import com.etc.entity.User;
import com.etc.service.UserService;
import org.springframework.stereotype.Service;

@Service
@com.alibaba.dubbo.config.annotation.Service
public class UserServiceImpl implements UserService {
    
    
    @Override
    public User getUserById(Integer id) {
    
    
        User user = new User();
        user.setName("Tom");
        user.setId(1);
        return user;
    }
}

(7) Add @EnableDubbo annotation before starting the class.

package com.etc.dubboprovide;

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

@SpringBootApplication
@EnableDubbo
public class DubboprovideApplication {
    
    

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

}

(8) Run DubboprovideApplication startup class.

Insert picture description here

2.2, create a Consumer consumer


(1) Create a dubboconsumer module in the dubbo project. The module type is Spring Intializr, and Spring Web dependency is added.

Insert picture description here
Insert picture description here

Insert picture description here
Insert picture description here
(2) Click the dubboconsumer module first, and then click the Shift key twice. Enter the maven keyword in the search box, click the Add Maven Projects option, and select the pom.xml file of the dubboconsumer module. After completion, the Maven option of the dubboconsumer module will appear on the right.

Insert picture description here

Insert picture description here
Insert picture description here
(3) Add dependency in the pom.xml file of the dubboprovide module.

<!-- Dubbo Spring Boot Starter -->
<dependency>
	<groupId>com.alibaba.boot</groupId>
	<artifactId>dubbo-spring-boot-starter</artifactId>
	<version>0.2.1.RELEASE</version>
</dependency>
<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>dubbo</artifactId>
	<version>2.6.5</version>
</dependency>
<dependency>
	<groupId>org.apache.curator</groupId>
	<artifactId>curator-recipes</artifactId>
	<version>2.12.0</version>
</dependency>

(4) Add the jar package commons-1.0-SNAPSHOT.jar of common classes and interfaces to the src directory of the dubboconsumer module. Right-click the jar package and click Add as Library.

Insert picture description here
(5) Modify the application.properties configuration file.

#端口
server.port=8082
#appname
spring.application.name=dubbo-consumer-user
#dubbo app name
dubbo.application.name=dubbo-consumer-user
#注册中心地址
dubbo.registry.address=zookeeper://192.168.170.128:2181

(6) Create Service connection, Service implementation class and Controller class. The @Reference annotation in the Service implementation class refers to the service object generated by the server.

  • UService.java
package com.etc.dubboconsumer.service;

import com.etc.entity.User;

public interface UService {
    
    
    public User getProviderById(Integer id);
}

  • UServiceImpl.java
package com.etc.dubboconsumer.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.etc.dubboconsumer.service.UService;
import com.etc.entity.User;
import com.etc.service.UserService;

import org.springframework.stereotype.Service;

@Service
public class UServiceImpl implements UService {
    
    
    @Reference
    private UserService userService;

    @Override
    public User getProviderById(Integer id) {
    
    
        return userService.getUserById(id);
    }
}

  • UController.java
package com.etc.dubboconsumer.controller;

import com.etc.dubboconsumer.service.UService;
import com.etc.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UController {
    
    
    @Autowired
    UService uService;

    @GetMapping("u/{id}")
    public User getUser(@PathVariable("id") Integer id){
    
    
        return uService.getProviderById(id);
    }
}

(7) Add @EnableDubbo annotation before starting the class.

package com.etc.dubboconsumer;

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

@SpringBootApplication
@EnableDubbo
public class DubboconsumerApplication {
    
    

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

}

(8) Run DubboconsumerApplication startup class. Access address 127.0.0.1:8082/u/1to see data from another module (dubboprovide) provides.

Insert picture description here
Insert picture description here

3. Build Dubbo Monitor management console


(1) Visit the webpage https://github.com/apache/dubbo-admin, and download the code of dubbo-admin-develop through Code -> Download ZIP.

Insert picture description here
(1) Modify the application.properties in the dubbo-admin-develop\dubbo-admin-server\src\main\resources directory.

  • admin.server.port: Specify the port of the management console.
  • admin.config-center: Specify the address of the configuration center.
  • admin.registry.address: Specify the address of the registration center.
  • admin.metadata-report.address: Specify the address of the metadata center.
  • admin.root.user.name: Set the login account.
  • admin.root.user.password: Set the login password.
    Insert picture description here
    (2) In the DOS window, enter the root directory of dubbo-admin-develop and run: mvn clean package -Dmaven.skip.test=trueto compile.
    Insert picture description here
    (3) After the compilation is successful, the jar package will be generated under dubbo-admin-develop\dubbo-admin-distribution\target. In the DOS window, enter dubbo-admin-develop\dubbo-admin-distribution\target to run java -jar dubbo-admin-0.3.0-SNAPSHOT.jar.

Insert picture description here
Insert picture description here
(4) Visit the webpage 127.0.0.1:8083, enter the Dubbo management console, enter the account root and password root to log in, and then you can operate.
Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_42141141/article/details/115273156