What is RPC (Remote Calls)

  • RPC in Plain English
  • When a program calls a program that is not in a project (or a host, or a process, or a program that is not directly usable), it has to use a program that is not directly usable, resulting in RPC

What is Dubbo and Zookeeper

  • Dubbo2.7 document

Most classic

  • The node role
node The role that
Provider The service provider that exposes the service
Consumer Service consumer that invokes the remote service
Registry A registry for service registration and discovery
Monitor A monitoring center that collects statistics on service invocation times and invocation time
Container Service run container
  • Zookeeper is the registry, a role in Dubbo.
  • The Dubbo is a framework, and we just fill it with what it needs.
  • As to why this framework is needed, the Dubbo documentation explains what requirements it satisfies and won’t go into detail.

How to use Dubbo+Zookeeper

steps

  1. To download the Zookeeper
  2. Start the zookeeper
  3. Dubo-admin (optional), just a visual interface for Dubbo (like MySql for Nativecat or SQLyog).
  4. Create two unrelated programs (SpringBoot project)

The detailed steps

Steps 1 and 2

  • To download Zookeeper, download the bin package in Windows.
  • To configure environment variables, be sure to use the administrator to run the command line, the problem is not running as an administrator.
  • Go to the bin folder in the decompression path and find the execution file in it. On Windows, zkserver. CMD, run,
  • Go to the bin folder in the decompression path and find the execution file in it. On Windows, zkcli. CMD, run,
  • If you have configured the zooKeeper environment variables, enter zkServer. CMD on the console. Do not go to the specified directory
  • Do not close the CMD console after ZooKeeper starts

Step 3,

  • Making download Dubbo – Admin
  • Package the download into a JAR package and run it as an administratorjava -jar ***.jar
  • Open your browser http://localhost:7001

Step 4.

  • Import dependence
<dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> < version > 2.7.9 < / version > < / dependency > < the dependency > < groupId > org. Apache. The curator < / groupId > < artifactId > curator - framework < / artifactId > < version > 5.1.0 < / version > < / dependency > < the dependency > < the groupId > org. Apache. Curator < / groupId > < artifactId > curator - recipes < / artifactId > < version > 5.1.0 < / version > < / dependency > < the dependency > < groupId > org. Apache. Zookeeper < / groupId > < artifactId > zookeeper < / artifactId > < version > 3.7.0 < / version > <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-1og4j12</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> The < version > 0.1 < / version > < / dependency > < / dependencies >Copy the code
  • Create a SpringBoot project called Provider

interface

public interface TicketService {
    public String getTicket(a);
}
Copy the code

Implementing an interface

@DubboService
public class TicketServiceImpl implements TicketService {
    @Override
    public String getTicket(a) {
        return "Here are your tickets."; }}Copy the code

The configuration application. Properpites

server.port=8001
# Port number for this service
dubbo.application.name=provider-server
# the name
dubbo.registry.address=Zookeeper: / / 127.0.0.1:2181
# Register at zookeeper
dubbo.scan.base-packages=com.boot.service
# Scan this package for @dubboService and register it with ZooKeeper
Copy the code
  • Create another SpringBoot project, consumer

Import dependencies are the same as above


Use the provider’s class

import org.springframework.stereotype.Service;

@Service// Let Spring host it
public class UserService {
    @DubboReference// Reference remote objects
    TicketService ticketService;
    public void buyTicket(a){
        String s =ticketService.getTicket();
        System.out.println(The consumer says: \" I want the ticket,, \"+"\n"+"Said the provider."+"\" "+s+"\" "); }}Copy the code

Create the same interface as the provider

public interface TicketService {
    String getTicket(a);
}
Copy the code

The configuration application. Properpites

server.port=8002
dubbo.application.name=consumer-server
dubbo.registry.address=Zookeeper: / / 127.0.0.1:2181
Copy the code
  • Test the
@SpringBootTest
class ConsumerServerApplicationTests {
    @Autowired
    UserService userService;
    @Test
    void contextLoads(a)  { userService.buyTicket(); }}Copy the code

summary

Annotations associated

  • @DubboServiceTell Dubbo that this is a provider for the consumer to call
  • @DubboReference, telling Dubbo to use objects registered with ZooKeeper

configuration

  • Both providers and consumers must register with ZooKeeper
  • The Provider also needs to configure which packages to scan for annotations.

Small nodules