dubbo notes

Introduction to Dubbo

Apache Dubbo is a high-performance Java RPC framework. Its predecessor is Alibaba's open source, lightweight open source Java RPC
Framework, which can be seamlessly integrated with the Spring framework. Dubbo provides three core capabilities: interface oriented remote method invocation, intelligent fault tolerance and load balancing, and automatic service registration and issuance
present

Introduction to RPC

The full name of RPC is remote procedure call, that is, remote procedure call. For example, there are two servers A and B, and one should be deployed on server A
Use. An application is deployed on server B. The Application on server A wants to call the method provided by the application on server B. because the two applications do not
In a memory space, it cannot be called directly, so it is necessary to express the semantics of the call and convey the data of the call through the network.
It should be noted that RPC is not a specific technology, but refers to the whole network remote call process.

Dubbo architecture

Node role description:

nodeRole name
ProviderService provider of exposed services
ConsumerThe service consumer that invokes the remote service
RegistryRegistry for service registration and discovery
MonitorThe monitoring center that counts the number and time of service calls
ContainerService run container

The dashed lines are asynchronous access, and the solid lines are synchronous access. The blue dashed lines: the functions completed at startup, and the red dashed lines (solid lines) are all programs that have been run
Functions performed in the process
Call relationship description: 0. The service container is responsible for starting, loading and running the service provider.

  1. When a service provider starts, it registers its services with the registry.
  2. When the service consumer starts, it subscribes to the service it needs from the registry.
  3. The registry returns the service provider address list to the consumer. If there is any change, the registry will push the change data to the consumer based on the long connection
    consumer.
  4. The service consumer selects a provider from the provider address list based on the soft load balancing algorithm to call. If the call fails
    If it fails, choose another one to call.
  5. Service consumers and providers accumulate call times and call times in memory and regularly send statistical data to monitoring every minute
    Heart.

Service registry zookeeper installation steps

1. Upload zookeeper's compressed package (zookeeper-3.4.6.tar.gz) to linux system;
2. Decompress the compressed package tar -zxvf zookeeper-3.4.6.tar.gz -C /usr;
3. Enter the zookeeper-3.4.6 directory and create the data directory mkdir data;
4. Enter the conf directory and put the zoo_ Change the name of sample.cfg to zoo.cfg cd conf, and the command is mv zoo_sample.cfg zoo.cfg ;
5. Open the zoo.cfg file and modify the data attribute: dataDir=/usr/zookeeper-3.4.6/data;
6. Enter the bin directory of Zookeeper and start the service command. / zkServer.sh start;
(stop service command. / zkServer.sh stop; view service status: / zkServer.sh status; client connection. / zkCli.sh);

Dubbo simple implementation:

service provider

1. Create a service provider project:

pom.xml file:

<?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.qun</groupId>
    <artifactId>dubbo_provider</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <spring.version>5.0.5.RELEASE</spring.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>${spring.version}</version>
    </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>${spring.version}</version>
        </dependency> <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>${spring.version}</version>
    </dependency>
        <!-- dubbo relevant -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</version>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency> <dependency>
        <groupId>javassist</groupId>
        <artifactId>javassist</artifactId>
        <version>3.12.1.GA</version>
    </dependency> <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.47</version>
    </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
            <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <configuration>
                    <!-- Specify port -->
    <port>8081</port>
                    <!-- Request path -->
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

applicationContext-service.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://code.alibabatech.com/schema/dubbo
         http://code.alibabatech.com/schema/dubbo/dubbo.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- The current application name is used to calculate the dependency between applications in the registry. Note: the application names of consumers and providers are different -->
    <dubbo:application name="dubbo_provider" />
    <!-- Connect service registry zookeeper ip by zookeeper Server ip address-->
    <dubbo:registry address="zookeeper://192.168.195.128:2181"/>
    <!-- Registration agreement and port Default 20880-->
    <dubbo:protocol name="dubbo" port="20881"></dubbo:protocol>
    <!-- Scan the specified package and join@Service Annotated classes are published as services -->
    <dubbo:annotation package="com.qun.service.impl" />
</beans>

Configure the web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

Create service interface and service implementation class:

public interface HelloService {

    String sayHello(String name);
}
@Service
public class HelloServiceImpl implements HelloService {
    @Override
    public String sayHello(String name) {
        return "hello world,"+name;
    }
}

Start service
tomcat7:run

Service consumer

The steps are the same as those of the service developer
pom.xml configuration and above service providers
Similarly, you only need to change the port number of Tomcat plug-in to 8082;

web.xml configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- Specify the configuration file to load through parameters contextConfigLocation load -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext-web.xml</param-value>
        </init-param> <load-on-startup>1</load-on-startup>
    </servlet> <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

1. Copy the HelloService interface in the service provider project to the current project
2. Write Controller

@Controller
@RequestMapping("/demo")
public class HelloController {
    @Reference
    private HelloService helloService;

    @RequestMapping("/hello")
    @ResponseBody
    public String getName(String name){
        //Remote call
         String result = helloService.sayHello(name);
         System.out.println(result); return result;
    }

}

Note: the @ Reference annotation provided by Dubbo is used to inject HelloService into the Controller

Create the applicationContext-web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://code.alibabatech.com/schema/dubbo
         http://code.alibabatech.com/schema/dubbo/dubbo.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- The current application name is used to calculate the dependency between applications in the registry. Note: the application names of consumers and providers are different -->
    <dubbo:application name="dubbo_consumer" />
    <!-- Connect service registry zookeeper ip by zookeeper Server ip address-->
    <dubbo:registry address="zookeeper://192.168.195.128:2181"/>
    <!-- Registration agreement and port Default 20880-->
<!--    <dubbo:protocol name="dubbo" port="20881"></dubbo:protocol>-->
    <!-- Scan the specified package and join@Service Annotated classes are published as services -->
    <dubbo:annotation package="com.qun.controller" />
</beans>

Test:
Enter in the browser http://localhost:8082/demo/hello.do?name=Jack , the results are shown in the figure:

Tags: Java Back-end

Posted on Sat, 23 Oct 2021 06:22:19 -0400 by Dragonfly