dubbo-in-depth-basic use

In-depth study on the basic use of dubbo (1)

Process Diagram of Internet Architecture Upgrading

Single Application Architecture (Deploying All Applications on a Single Machine) - > Vertical Application Architecture (Deploying Applications on a Single Machine to Relevant Separate Servers, Each Server is Independent) - > Distributed Service Architecture (Interactive Processes in Previous Parts) - > Flow Computing Architecture SOA (When the system becomes complex, With all kinds of interaction confusion, Yushu has an integrated configuration mechanism based on central scheduling - > micro-services

dubbo's role

  • As a service registry, dynamic registration and discovery services make the location of services transparent and achieve software load balancing and Failover (failure backup) by obtaining the address list of service providers from consumers, thus reducing the dependence on F5 hardware load balancer.
  • Automatically draw dependency diagrams between applications to help architects clean up relationships.
  • As a reference index of capacity planning, it can dynamically adjust the weight. On-line, the weight of a machine has been increased. In the process of increasing, the change of response time is recorded until the response time reaches the threshold and the visits at this time are recorded. Then the total capacity is estimated by multiplying the number of machines with this amount of access.

Basic architecture of dubbo

node Role Description
Provider Exposure service provider
Consumer Service consumers invoking remote services
Registry Registry of Service Registration and Discovery
Monitor Monitoring Center for Call Subharmonization and Call Time of Statistical Services
Container Service Running Container

Basic usage method

In other words, dubbo is not that difficult. First, it needs a set of public interfaces. As an entry point to the services provided outside, the dubbo framework handles the process of finding service providers. Users can use dubbo just like spring framework. See the following examples.

  1. Public interface

Note: This common interface must have a package name, otherwise the reflection processing method inside dubbo will report null error.

package service;
public interface DemoService {
    public String say();
}

Provide maven Gong for other components to use

<?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>demo</artifactId>
        <groupId>org.fen</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>demoAPI</artifactId>
</project>
  1. provider Service provider

xml configuration file, pay attention to the external services must be interface

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <dubbo:application name="provider"></dubbo:application>
    <dubbo:registry address="multicast://224.5.6.10:1253"/>
    <dubbo:protocol name="dubbo" port="20880"/>
    <bean id="myfirstdemo" class="DemoSayImp"></bean>
    <dubbo:service interface="service.DemoService" ref="myfirstdemo"></dubbo:service>
</beans>

Interface Implementation Class

import service.DemoService;
 
//public class DemoSayImp implements DemoSay{
public class DemoSayImp implements DemoService {
    public String say() {
        System.out.println("this is my say");
        return "sdfsdf";
    }
}

Service startup entry

import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import java.io.IOException;
 
public class Main {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("bean.xml");
        applicationContext.start();
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Note: The linux system needs to modify the / etc/hosts file to change the ip address corresponding to the hostname to the ip address of the computer's corresponding network card.

  1. Consumer consumer

xml configuration file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <dubbo:application name="consumer"></dubbo:application>
    <dubbo:registry address="multicast://224.5.6.10:1253"/>
    <dubbo:reference interface="service.DemoService" id="demoService"></dubbo:reference>
</beans>

Consumer Procedure Entry

import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.DemoService;
 
public class Main {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"bean.xml"});
        //context.start();
        DemoService demoService = (DemoService) context.getBean("demoService"); // Getting a remote service agent
        String hello = demoService.say(); // Executing remote methods
        System.out.println(hello); // Display the result of the call
    }
}

Tags: Java Dubbo xml Maven Spring

Posted on Mon, 26 Aug 2019 22:02:57 -0700 by Jezthomp