Introduction to Dubbo, how to write DubboDemo?

dubbo release version and new features

Mainly maintain two major versions, 2.6.x and 2.7.x.

Architecture

Insert picture description here
Node role description:

node Role description
Provider Service provider
Consumer Service consumer
Registry Registry
Monitor Monitoring center that counts the number of service calls and call time
Container Service running container

Calling relationship description:
0. The service container is responsible for starting, loading, and running services.
1. When the service provider starts, register the service provided by the registry
2. When the service consumer starts, subscribe to the registry for what they need the service
3. registration Center returns a list of addresses service providers to consumers, if there is a change, the registry will change based on the long connection push data to the consumer.
4. Service consumers, from the list of provided addresses, based on the soft load balancing algorithm, select one provider to call, and if the call fails, select another call.
5. Service consumers and providers accumulate the number of calls and call time in the memory, and send statistical data monitoring to the monitoring center every minute.
note:

注册中心和监控中心全部宕机,不影响已运行的提供者和消费者,消费者在本地缓存了提供者列表
虚线都是异步访问,实线都是同步访问,蓝色都是初始化。红色都是程序运行过程中执行的。
所有的角色都可以在单独的服务器上,遵守特定的协议。

Create a dubboDemo

1. Create a maven Project

Insert picture description here

2. Create 3 sub-modules respectively. API provides interfaces, service implements interfaces, provides services, and consumers call the interfaces from the registry.

Insert picture description here

3. Introduce the dubbo jar package in the parent project

<?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>org.example</groupId>
    <artifactId>dubbo-study</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.7.6</version>
        </dependency>
    </dependencies>
    <modules>
        <module>dubbo-service</module>
        <module>dubbo-consumer</module>
        <module>dubbo-api</module>
    </modules>


</project>

4. Write the demoService interface in the api project

Insert picture description here

5. Introduce the dependency of the api project in the pom of the service project

<?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>dubbo-study</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <dependencies>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
    <artifactId>dubbo-service</artifactId>

</project>

6. Implement the DemoService interface in the api

Insert picture description here

7. Create provider.xml under the resources of the service project and register the service.

<?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:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">


    <!--提供方应用名,用于在管理工具中区分是哪个提供方-->
    <dubbo:application name="hello-world-app"/>
    <!--使用multicast广播注册中心暴露服务地址    -->
    <dubbo:registry address="multicast://224.5.6.7:1234"/>
    <!--用dubbo协议在20880端口暴露服务    -->
    <dubbo:protocol name="dubbo" port="20880"/>
    <!--声明需要暴露的服务接口   只能注册接口,引用实现类 -->
    <dubbo:service interface="service.DemoService" ref="demoService"/>
    <!--和本地bean一样实现服务-->
    <bean id="demoService" class="service.impl.DemoServiceImpl"></bean>
</beans>

8. Introduce the dependency of the api project in the pom of the consumer project (screenshot omitted)

9. Create consumer.xml under the resources of the consumer project and call the service.

<?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:dubbo="http://dubbo.apache.org/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- 消费方应用名,在管理工具中区别是哪个consumer不要与提供方一样 -->
    <dubbo:application name="consumer-hellowold-app"/>
    <!-- 使用multicast广播注册中心暴露发现服务地址 -->
    <dubbo:registry address="multicast://224.5.6.7:1234"/>
    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="demoService" interface="service.DemoService"/> 
</beans>

10. Start testing, write test classes in service and consumer respectively, and start testing

This is the test in the service This is the test
Insert picture description here
in the consumer
Insert picture description here

Error resolution:

Starting service and consumer locally at the same time will cause port conflicts. Create dubbo.properties under resources to modify the port

dubbo.application.qos.enable=true
dubbo.application.qos.port=33333
dubbo.application.qos.accept.foreign.ip=false

Guess you like

Origin blog.csdn.net/weixin_44146509/article/details/109077418