rpc dubbo learning portal framework and environment to build (spring boot + Kotlin)

Before learning, the knowledge base to ensure that there are the following:

  • Java Network Programming
  • Socket data transmission
  • IO flow

rpc Introduction and Implementation

rpc is a remote procedure call shorthand, meaning remote procedure calls.

rpc application scenario is more distributed development, distributed development what is it?

Originally I also wanted to explain myself, but since online Gangster explain very clearly, there is not incompetence, I recommend reading the following recommended several to continue down

At the beginning, the service and calls are on the same machine, called local procedure call

Later, due to customer volume growth, a server can not meet the requirements, then the call is separated and services, are deployed in a different machine, is responsible for calling the service method is called client , is responsible for providing a service method called service machines

Principle figure could step a little more, but just remember that client data is transferred through the Socket or other agreement to the servers, so that service machines for processing, so in the same way the protocol to pass data back

How to implement a simple RPC article, big brother has implemented a simple rpc framework, and the framework for this rpc, and made a number of points that can be optimized:

1. The lack of versatility

I write by giving Calculator interface to a CalculatorRemoteImpl, to achieve remote call calculator, next time if there is a need for long-distance calls on another interface, are not you also have to write the corresponding implementation class long-distance calls? This is certainly inconvenient.

2, Spring Integration

In the realization of a universal proxy object, the next step will be to consider integrating Spring features of the IOC, to create a proxy object through the Spring, it will need to initialize the Spring bean a certain grasp.

3, long or short connection connector

It is to open a Socket connection is established it can not always be invoked when the RPC interface? When is not possible to maintain a number of long connection, then there is every rpc request, the request into the task queue, and then executed by a thread pool to spend? Just a thought, you can refer to how the follow-up Dubbo is achieved.

4, the server thread pool

We are now Server-side, single-threaded, and so every time a request is processed in order to accept a connection to another socket, so performance is certainly bad, is not by a thread pool to achieve simultaneous processing of multiple RPC request? Also just a thought.

5, the service registry

As previously mentioned, to call the service, first you need a service registry, tell you what are the other service instance. Dubbo service registry is configurable, the official recommended Zookeeper. If you are using Zookeeper, then, how to register the instance above, but also how to obtain an instance, these are to be achieved.

6, load balancing

How to choose from multiple instances where one out and make calls, which use the load-balancing. The load balancing policy is certainly more than one, how the policy can be made configurable? But also how to implement these strategies? The same can refer to Dubbo, Dubbo - Load Balancing

7. Result Cache

Server side should really go to each call query interface query anyway? It is not to think about support caching?

8, multi-version control

Server interfaces changed, the old interface how to do?

9, an asynchronous call

After completion of the client call interface, do not want to wait for the server to return, want to do some other things, you can not support?

10, elegant downtime

To shut down the server, the request is not processed, how do?

PS: When using rpc, consider the network problem, we need a retry mechanism

From these questions above, then is there have been some outstanding rpc frameworks, such as dubbo, spring cloud, etc.

dubbo Profile

Dubbo is a high performance Java-based RPC (Remote Procedure Call) Distributed Service Framework (SOA), providing high performance and transparency of RPC remote service call programs, services and SOA governance program, its internal use of open source Alibaba Netty, Zookeeper, ensures high performance and high availability.

FIG dubbo structure:

node Role Description
Provider Expose service provider service
Consumer Call the remote service consumer services
Registry Service registration and discovery registries
Monitor The number of calls statistics monitoring center services and call time
Container Run container services

Among them, registries Registryand surveillance centers Monitorare optional, so below we briefly achieved dubbo (point to point transmission of data)

<dependency>
    <groupId>com.starsone</groupId>
    <artifactId>dubbo-api</artifactId>
    <version>0.0.1</version>
</dependency>

dubbo simple implementation

Project Description: The
project is based on spring boot, it is divided into three parts api, consumerandprovider

  • api is mainly used to declare some interfaces (maven project) services
  • provider is the realization of the service interface (spring boot project through maven dependent api project)
  • consumer is the service interface (spring boot project through maven dependent api) provides remote call provider,

The client is essentially equivalent to the consumer, and the provider is equivalent to the client

1. Create a new project

Use IDEA, create a blank project, and then create a new module

After IDEA will pop up a window of a new module

And then click New api, provider, cousmer three module

api choose maven project, after fill in the relevant information directly to the new package name can (do not need to choose a specific maven structure), while the other two are spring boot project, you can choose spring initializr new, equally, do not check the other dependence, fill in the relevant information package name to New

2.api project declaration service interface

In the api project, we create a new CalculatorService Interface, which defines a method add

public interface CalculatorService {
    int add(int a,int b);
}

3. Configure dependent api project

Originally, after the provider and the consumer needs to reference projects are dubbo-spring-boot-starter this dependence, dubbo-spring-boot-starter dependent already included dubbo dependent, so you can rely dubbo of the need to write

Since then our provider and consumer items are api need to reference this project, so we can put the provider and the consumer needed to rely dubbo-spring-boot-starteradded to this project api

After the provider and consumer items it is dependent on the api project, also dependent on the success of thedubbo-spring-boot-starter

<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>2.7.5</version>
</dependency>

4.provider and consumer project references api project dependencies

Because before api project we are creating a maven project, so adding a dependency is very simple, adding a dependency on provider and consumer to their pom.xml

5.provider implement the service interface api

We create a new class in the project provider CalculatorServiceImpl, to implement CalculatorServicethe interface

@Service(interfaceName = "calculatorService")
class CalculatorServiceImpl:CalculatorService {
    override fun add(a: Int, b: Int): Int {
        val result = a+b
        println("$a+$b=$result")
        return result
    }
}

Note that this comment is dubbo of Service package inside the notes, rather than the spring of Service , define the interface name interfaceNamefor the calculatorServicecontainer to find, convenient after

6. Configure provider project

We need to modify the spring boot configuration file, and here I use the form yml be configured, reading more comfortable

spring:
  application:
    name: dubbo-provider-application
dubbo:
  scan:
    #扫描指定包是否包含有dubbo中Service注解的类
    base-packages: com.starsone.provider.service
  protocol:
    name: dubbo #协议,默认为dubbo(其他协议webserovice、Thrift、Hessain、http)
    port: 12345 #端口,默认为20880
  registry:
    address: N/A #不需要注册中心

PS: If you do not develop like scanning contains Service annotations in the configuration file, you can add application class provider project in turn dubbo automatic scanning of notes@EnableDubbo

provider project structure diagram:

7. Run provider

Since we have not introduced the registry, so we have to run provider, ip address

After consumer items, to make springr container according to ip address and port number to find the corresponding instance and automatic loading

The output log, we can see ip address

8.consumer obtain service objects

@Component
class MyRunner:ApplicationRunner {
    @Reference(url ="dubbo://192.168.52.1:12345",interfaceName = "calculatorService" )
    private lateinit var calculatorService: CalculatorService

    override fun run(args: ApplicationArguments?) {
        println(calculatorService.add(5,14))
    }
}

Here, because it is simple to consider, not using a web-dependent, therefore, use this interface to test the ApplicationRunner, spring loaded container to complete this interface will automatically callback

Reference is dubbo notes in the comments, after consumer project run, the consumer will be passed dubbo data according to this url and other information, call the provider of remote service, then, provider receives and processes the data and returns data to the consumer, it is not with rpc feeling?

9. Configure and consumer test

Configured, only the name of the application configuration

After that, we run the consumer's application, you can see the results

Similarly, the provider, and also is to print out the parameters passed over consumer items

The introduction of registry

In front of realization, there is no registry, is a way of directly connected, but, in fact, distributed development, with multiple service machines

The client should request to the registry, the registry query the current service is idle, and according to a strategy, select a service, set its ip address back to the client, then the client by ip address, and the service machine connected for operation rpc

dubbo framework recommended ZooKeeper as a registration center

ZooKeeper is a distributed, open-source coordination service for distributed applications, is an open source implementation of Google's Chubby, is a key component of Hadoop and Hbase. It is to provide a consistent service for distributed applications, provides features include: configuration maintenance, domain name service, distributed synchronization, group services.

1. Download the zookeeper

http://mirror.bit.edu.cn/apache/zookeeper/

Note that the download here is consistent with the version the best version of the project relies

2. Import dependent zookeeper

We need to modify rely api project, so dependent on provider and consumer is able to modify the two projects

<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.4.14</version>
    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
        <exclusion>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </exclusion>
        <exclusion>
            <groupId>io.netty</groupId>
            <artifactId>netty</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<!-- Zookeeper客户端 -->
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-recipes</artifactId>
    <version>4.2.0</version>
</dependency>

3. Configure the registry provider and consumer of

provider:

consumer:

4. Cancel consumer url specified

Since we are using the registry, there is no need to specify the url, the url deleted in Reference Notes

5. Run zookeeper

Extract the downloaded archive zookeeper, into the conf directory, the zoo_sample.cfgfile changedzoo.cfg

Go to the bin directory, click on the zkserver.cmdfile, run the zookeeper

6. Run provider and consumer

First run provider, after running the consumer, you can see the results

This article also toss a few days, more than a dozen reference to the article, get step by step test was successful, and some knowledge points and not too deep, like dubbo console, such as how to set up the monitoring center, when the latter study further supplement it

reference

Guess you like

Origin www.cnblogs.com/stars-one/p/12534295.html