Take you to the world of dubbo

Keywords: Java Zookeeper Distribution

preface

With the deepening of current technology and the increasing number of services, it is not only a large amount of data and a large number of users, but also prone to high concurrency. So distributed systems are slowly emerging. This distributed system is mainly to split a complete system into micro services, so that each service is small and specialized. That is, they perform their respective duties. So you might say, "how can they communicate? If they are separated, how can they form a complete system. At this time, dubbo comes out. It is used to solve the interaction between each micro service, that is, RPC (remote call).

Dubbo has the following functions: remote call, service registration and discovery, load balancing, disaster recovery and clustering.

text

Let's go straight to dry goods!!!

  1. Preliminary preparation:
    1) When we use dubbo, we should first have jdk in our environment.
    2) Because dubbo is for service registration and discovery, it needs a registry. In fact, the so-called registry is used to store the services we have. Therefore, what can be used as a registry is what can be stored. For example, zookeeper and redis. Generally, we use zk. So here we use zk as the registry.
    3) To install zookeeper:
    Download zookeepr first

    After decompression, execute the following command
# Create a data folder in the zookeeper folder to store some temporary data
mkdir data
# Enter conf to modify the name of the configuration file
mv zoo_sample.cfg zoo.cfg
# Enter the zoo.cfg file, modify the dataDir in it, and change the path to the location of the data folder just created
dataDir=/usr/local/tangyuan/zookeeper/zookeeper-3.6.3/data
# Then start zookeeper
./zkServer.sh start  start-up
./zkServer.sh stop   stop it
./zkServer.sh status state

#If you don't report an error, your above operations are normal.
  1. Is it easy to create a project, write code, introduce dubbo's jar and then disappear

Let's take a service party and a consumer as an example. Use the consumer to call the service party remotely.

Service provider:

  • Introducing jar
<!--dubbo of springboot support-->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<!--dubbo frame-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</dependency>

<!--zk rely on-->
<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
</dependency>
<dependency>
    <groupId>com.github.sgroschupf</groupId>
    <artifactId>zkclient</artifactId>
</dependency>
  • configuration file
# Service party
server:
  port: 8081
  servlet:
    context-path: /tangyuan
dubbo:
  application:
    name: dubbo-provider-qishen
  protocol:
    name: dubbo
    port: 20880
    host: 192.168.23.1
  registry:
    address: zookeeper://192.168.23.129:2181
    client: zkclient
    timeout: 60000
  consumer:
    timeout: 60000
  scan:
    basePackages: com.qishen.dubbo_provider
#Consumer
dubbo:
  application:
    name: dubbo-consum-qishen
  registry:
    address: zookeeper://192.168.23.129:2181
    client: zkclient
    timeout: 60000
  consumer:
    timeout: 60000
  scan:
    basePackages: com.qishen.dubbo_consum
#Note that the above two configuration files are not written in one
  • Two important comments in dubbo
Notes used by the service provider@Service
 Consumer injection service usage notes@Reference
  • Advanced features of dubbo
  1. serialize
    dubbo has been encapsulated with serialization and deserialization, so we only need to implement the serializable interface, so that our objects can be transmitted remotely, otherwise an error will be reported when transmitting objects
  2. Address cache
    The so-called address caching means that when our service is called for the first time, the address of the service provider will be cached locally. When the address changes, the registry will notify the consumer to retrieve the address of the service provider and cache it
  3. overtime
    When the service consumer calls the service provider, it will be blocked and waiting. When a large number of requests are at this stage, it will cause an avalanche. Therefore, dubbo provides a timeout mechanism.
    Method: add timeout in @ Service annotation
@Service(timeout=3000,retries=0) //timeoout timeout retries retries
  1. retry
    When we can't connect, it may be caused by network jitter, so we can't have this problem and our program can't run. Therefore, we need to retry, determine the time and send another request. The usage is not mentioned. It's written in timeout
  2. Multi version
    Multiple versions are easy to understand. When we release new functions, if we change the original dubbo content, the previous functions should also be used. Let different users try and fight the glory of the king with you. People don't die, they also have an experience service for others to experience, and put it on the formal service. That's the reason, so here we also pass a generic service Sexual realization
@Sercice(version="1.1.0")
  1. load balancing
    I won't explain the concept of load balancing too much. For example, if a service consumer sends multiple requests to a service provider for processing, do you have to queue up to wait for processing? Think about whether you are in a hurry when you queue up to buy food. What if the food in front is bought by others!!!
    At this time, multiple teams can buy this meal. Relatively speaking, are you in the front row? This is load balancing.
    Be balanced, you know

Load balancing policy:

Random: Random by weight, default. Set random probability by weight.

RoundRobin: Poll by weight.

LeastActive: Minimum number of active calls,Random of the same active number.

ConsistentHash: uniformity Hash,Requests with the same parameters are always sent to the same provider.

Server configuration:

@Service(weight = 100)

Consumer configuration:

//@Reference(loadbalance = "roundrobin")
//@Reference(loadbalance = "leastactive")
//@Reference(loadbalance = "consistenthash")
@Reference(loadbalance = "random")//Default random by weight
  1. Cluster fault tolerance
    The above load balancing understand that the cluster is simpler. The so-called cluster is that one team can buy food, and now there are multiple teams can buy food. This is the cluster.

Cluster mode:

Failover Cluster:Failure retry. Default value. When failure occurs, retry other servers. By default, retry 2 times. Use retries Configuration. Generally used for read operations
Failfast Cluster :Rapid failure,launch-An error is reported immediately after a call fails. It is usually used for write operations.
Failsafe Cluster:Fail safe. When an exception occurs, it is ignored directly. An empty result is returned.
Failback Cluster:Automatic recovery after failure,Background record failed request,Regular retransmission.
Forking Cluster :Call multiple servers in parallel, and return as long as one succeeds.
Broadcast Cluster: Broadcast call all providers,Call one by one. If any one reports an error, it will report an error.

This only needs to be configured by consumers. The teams have been placed there. Now it depends on how you use these teams. It can be used in the way of cluster.

Consumer configuration:

@Reference(cluster = "failover")//Remote injection
private UserService userService;
  1. service degradation
    The last one is service degradation, which may be difficult to understand literally. Let me explain:
    Everyone has bought things in Taobao and Jingdong on the double 11. They sold so much at that time. Why hasn't the server crashed yet? Of course, it's not entirely because the service is degraded. First of all, people don't necessarily have dubbo. Moreover, there are many solutions to this problem. They may have used all or some of them. I'm assuming to use it here, for example nothing more.
    The so-called service degradation is that when the server is under great pressure, we can set some service pages not to process or handle them in a simple way. For example, when you buy things on double 11, do you mainly buy things, or do you use the current account password and registered account. The answer is obvious. For these unimportant services, we can use the method of degradation to deal with them Ensure that sufficient resources are released to deal with the core business - selling things.
    Method of service degradation:
  • mock= force:return null: it means that the method calls of the consumer to the service directly return null values and do not initiate remote calls. It is used to shield the impact on the caller when unimportant services are unavailable

  • mock=fail:return null: indicates that the method call of the consumer to the service returns null value after failure, and no exception is thrown. It is used to tolerate the impact on callers when unimportant services are unstable

The concepts are almost the same. Let me show you some code (here is only the core code)

  • Entity class
package com.qishen.dubbo_inter.pojo;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author Qishen ~ Tangyuan FengJY
 * @version 1.0
 * @description
 * @date 2021/10/20 17:17
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User implements Serializable {
    private String id;
    private String name;
    private String phone;
}
#Note: this interface must be implemented when writing entity classes
  • Consumer code
package com.qishen.dubbo_consum.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qishen.dubbo_inter.inter.UserService;

import com.qishen.dubbo_inter.pojo.User;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Qishen ~ Tangyuan FengJY
 * @version 1.0
 * @description
 * @date 2021/10/20 18:52
 */
@RestController
public class UserController {

    @Reference(version = "1.1")
    private UserService userService;

    @RequestMapping ("/a/{id}")
  public User getUserId(@PathVariable String id){
      return userService.getUser(id);
  }
}

  • Service party code
package com.qishen.dubbo_provider.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.qishen.dubbo_inter.inter.UserService;
import com.qishen.dubbo_inter.pojo.User;


/**
 * @author Qishen ~ Tangyuan FengJY
 * @version 1.0
 * @description
 * @date 2021/10/20 18:50
 */
@Service(version = "1.1")
public class UserServiceImpl implements UserService {
    @Override
    public User getUser(String id) {
        return new User(id,"Zhang San","1234");
    }
}

As mentioned earlier in the configuration file, there is not much code!!!

Source code: https://gitee.com/fengjiyuan/dubbo_demo.git

Welfare: usually, we want to see how our dubbo service is running. Fortunately, there is a tool called dubbo admin,

It looks like this. It's actually a springboot+vue project. You can download it yourself https://github.com/apache/dubbo-admin/
We just need to modify the configuration file

This is my top, and then start it. I'll just introduce more. This is relatively simple.

Finally: I think there are many people who use nacos as the registration center (I'll have the opportunity to introduce this later), so zookeeper knows what it is, and there's no need to learn it in particular.

Posted by Julian on Wed, 20 Oct 2021 22:40:45 -0700