Manual building of springboot + dubbo + zookeeper frame RPC

1. What is RPC?

rpc stands for Remote Procedure Call , Chinese = remote calls. We first look at local call.

Local calls

For example: a method we call the service layer in the program in the controller, which is a local call on a single machine. That this method (service) and the program is invoked in the same process. In this way we passed the bottom pointer (address) can be found directly call which way to go.

Remote call

But for some concurrency more than two hundred thousand or more, based on high concurrency, high performance, high reliability considerations generally use distributed applications, will share the things you can separate out into a top class server, so that other service to call. This is rpc remote service call.

2, how to RPC services?

1, we often do in the system and B / S this architecture, let Impl expose a service interface, and then make the call. But it is a big problem, each time sending Http need to write code, so not very good, very elegant. We like to consider is whether a local call , like it?

2, used in conjunction with the Spring IoC, service object through injection Spring, injection, if the object is scanned annotations added @Reference, then give it generates a proxy object, the proxy object into the container. The interior of the proxy is to be achieved through the RPC remote procedure call httpClient.

@Reference
private Service service;

...

service.add(1,2);

...

Today we say dubbo is to use this line of thought.

RPC two issues to be addressed:

(1) to solve the distributed system, problems between service calls.

(2) remote call, to be able to call as easy as local, so that the caller can not perceive the logic of remote calls.

RPC realization of ideas

application is rpc callers, Client Stub is above us when it comes to the proxy object, that is, that looks like Service implementation class, in fact, is internal to the proxy object through a remote call rpc way, as Client Run-time Library, it is the realization of a remote call kit, such as the jdk the Socket, and finally realize the transmission of data is achieved through the underlying network.

This process is the most important thing is to serialization and de-serialization , because the data transmission packet must be binary, you just throw a Java object in the past, people do not know, you have to serialize Java objects into a binary format, pass an end to Server, Server receives after, then deserialized into Java objects.

3, using Springboot + Dubbo + Zookeeper implement RPC.

First are brief dubbo and zookeeper

Some dubbo core functions:

(1) remote method invocation transparency, just as the same as calling a local method call remote methods. Each method call between different services (that is to say a little more straightforward interface calls), a service may be a service provider may also serve consumers.

(2) load balancing and fault tolerance, load balancing strategy default is random, configurable options.

(3) automatic registration and service discovery, no longer need to write the dead service provider address, IP address registry based query interface name service provider, and can be smoothly add or remove services provider. Common Zookeeper do registry.

Zookeeper:

zookeeper is a registered center for registering client and server services.

First of all it, bread service and sausage service on separate servers, we tubes they call provider (provider), we assume they are good related configuration dubbo and zookeeper, when they start the program, it will register with the service center they are a good information, we use zookeeper visualization tools can be seen reflected in java is @Service this comment, this spring is not provided, it is provided dubbo.

Then we start the client called the consumer (consumers), after starting @Reference found this comment, it will go to the registration center to register to find the corresponding service, if it only wanted a service such as (ham), then registered only once.

Finally, calling, consumer long-distance call service if necessary, will send an application to the registry, and the registry returns a list of such services, then select a service call, and it will be added to the list of cached (non-essential), under call to directly use the list on it.

 

Note: Consumers and providers only start when the zookeeper interact with the once and only once.

Because: the registry is down does not affect the operation of consumers and providers, even zookeeper server goes down, consumers and providers are still operating normally. When consumers start to obtain a list of service providers from the zookeeper. All registry and monitoring center downtime, does not affect the provider has been running and consumers, consumers in the local cache of the provider list. After registration center dawdle away, but you can not register the new service. Can not register the new service, the old service does not affect the operation.

Code:

Project directory:

The top maven pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.rpc</groupId>
    <artifactId>MyRPC</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>dubbo-api</module>
        <module>dobbo-consumer</module>
        <module>dubbo-provider</module>
    </modules>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <curator-framework.version>4.0.1</curator-framework.version>
        <zookeeper.version>3.4.13</zookeeper.version>
        <dubbo.starter.version>0.2.0</dubbo.starter.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>${dubbo.starter.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>${curator-framework.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>${zookeeper.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

dobbo-fire

package com.rpc.service;

public interface MistraService {

    String printInfo(String msg);
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>MyRPC</artifactId>
        <groupId>com.rpc</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-api</artifactId>


</project>

dobbo-provider

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>MyRPC</artifactId>
        <groupId>com.rpc</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-provider</artifactId>
    <dependencies>
        <dependency>
            <groupId>com.rpc</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>


</project>
package com.rpc.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.rpc.service.MistraService;

@Service(version = "${mistra.service.version}")
public class MistraServiceImpl implements MistraService {

    @Override
    public String printInfo(String msg) {
        return "hello " + msg;
    }
}
package com.rpc;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class DubboProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(DubboProviderApplication.class, args);
    }
}
spring.application.name = dubbo-provider
server.port = 9090
dubbo.application.name = dubbo-provider
mistra.service.version = 1.0.0
dubbo.protocol.name = dubbo
dubbo.protocol.port = 20880
dubbo.registry.address = zookeeper://localhost:2181
dubbo.provider.timeout = 1000

Dobbo-consomer

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>MyRPC</artifactId>
        <groupId>com.rpc</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dobbo-consumer</artifactId>
    <dependencies>
        <dependency>
            <groupId>com.rpc</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>


</project>

 

package com.rpc;

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);
    }

}

 

package com.rpc.rest;

import com.alibaba.dubbo.config.annotation.Reference;
import com.rpc.service.MistraService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("test")
public class TestRestController {

    @Reference(version = "${mistra.service.version}")
    private MistraService mistraService;

    @RequestMapping("/sayHello/{name}")
    public String sayHello(@PathVariable("name") String name) {
        return mistraService.printInfo(name);
    }
}
spring.application.name = dubbo-consumer
server.port = 9091
dubbo.application.name = dubbo-consumer
mistra.service.version = 1.0.0
dubbo.protocol.name = dubbo
dubbo.protocol.port = 20880
dubbo.registry.address = zookeeper://localhost:2181
dubbo.consumer.timeout = 5000

Address of the project: [email protected]: Zesystem / my-rpc.git

Published 134 original articles · won praise 91 · views 160 000 +

Guess you like

Origin blog.csdn.net/weixin_44588495/article/details/104855903