[dubbo学习] 简单的dubbo功能

1.Dubbo结构图

[dubbo学习] 简单的dubbo功能

生产者-消费者模型,生产者和消费者都需要在注册中心进行注册,生产者生产接口,通过api暴露给消费者使用

注册中心管理服务提供方的url

监控模块负责监控管理整个流程

生产者(Provider)整个发布,订阅流程:

  • 启动容器,加载,运行服务提供者
  • 服务提供者在启动时,在注册中心发布注册自己提供的服务
  • 服务消费者在启动时,在注册中心订阅自己所需的服务。

如果存在服务失败或者变更的情况,Dubbo就进行如下的操作:

  • 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
  • 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
  • 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

2.Dubbo实例

2.1 zookeeper

2.1.1 zookeeper 下载

zookeeper下载:https://zookeeper.apache.org/releases.html

[dubbo学习] 简单的dubbo功能

2.1.2 zookeeper安装

windows 安装

1.解压下载的压缩包

2.修改 zoo_sample.cfg 为 zoo.cfg

[dubbo学习] 简单的dubbo功能

 3.bin目录下启动 zkServer.cmd

Linux 安装

1.解压下载的压缩包

2.修改 zoo_sample.cfg 为 zoo.cfg

3.修改zoo.cfg

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
dataDir=/home/tjtl/dubbo/apache-zookeeper-3.6.1-bin/data
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the 
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
admin.serverPort=8888
## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true


server.1=zoo1:2888:3888

4.在 /home/tjtl/dubbo/apache-zookeeper-3.6.1-bin/ 目录下建立文件夹data

mkdir /home/tjtl/dubbo/apache-zookeeper-3.6.1-bin/data

5.在第4步建立的文件夹下建立文件myid

6.执行命令

echo “1” > myid

"1" 对应 zoo.cfg 文件中 “server.1” 中的 “1"

7.在bin目录下启动

./zkServer.sh start-foreground

Linux 下安装遇到问题处理

1.报错 java.net.SocketException:Unresolved address

   处理: 在/etc/hosts中配置主机映射

127.0.0.1 master

   [dubbo学习] 简单的dubbo功能

2.2 dubbo-admin安装

下载Url https://github.com/apache/dubbo

修改/dubbo-admin-2.5.4/WEB-INF/dubbo.properties

dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest

 使用maven自己手动打包mvn package -Dmaven.skip.test=true

 将war包放入tomcat下,执行tomcat

 访问url:http://127.0.0.1:8090/dubbo-admin-2.5.4

 访问用户名:root

 访问密码:root

[dubbo学习] 简单的dubbo功能

2.3 dubbo 生产者 消费者 API

项目目录

[dubbo学习] 简单的dubbo功能

API提供生产者的接口暴露,生产者在service中实现暴露接口,消费者通过controller进行消费

 springboot-dubbo.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>springboot-dubbo</groupId>
    <artifactId>springboot-dubbo</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <modules>
        <module>dubbo-api</module>
        <module>dubbo-provider</module>
        <module>dubbo-consumer</module>
    </modules>

</project>

 2.3.1 创建生产者

 [dubbo学习] 简单的dubbo功能

生产者在service中进行接口实现

 pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.soft</groupId>
    <artifactId>dubbo-provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dubbo-provider</name>
    <description>dubbo-provider</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

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

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

        <!--服务提供者的接口API-->
        <dependency>
            <groupId>com.soft</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>v1.0</version>
        </dependency>

        <!-- dubbo依赖 -->
        <dependency>
            <groupId>com.alibaba.spring.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>

        <!-- 引入zookeeper的依赖 -->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application.properties

############### dubbo 配置 ###############################
# 端口
server.port=9011
spring.dubbo.application.name=dubbo-provider
spring.dubbo.application.registry=zookeeper://IP:2181

DubboProviderApplication.java

package com.soft.provider;

import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableDubboConfiguration //开启Dubbo的自动配置
@SpringBootApplication
public class DubboProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(DubboProviderApplication.class, args);
    }

}

ProviderDemoImpl.java

package com.soft.provider.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.soft.api.service.IProviderDemo;
import org.springframework.stereotype.Component;

/**
 * dubbo 服务层测试
 * @author suphowe
 */
//使用com.alibaba.dubbo.config.annotation.Service,作用是暴露服务,不要使用Spring中的@Service
@Service
@Component
public class ProviderDemoImpl implements IProviderDemo {

    @Override
    public String providerReturnString(String name) {
        return "Provider Return:".concat(name);
    }

}

2.3.2 创建消费者

[dubbo学习] 简单的dubbo功能

 消费者在controller中进行消费

pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.soft</groupId>
    <artifactId>dubbo-consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dubbo-consumer</name>
    <description>dubbo-consumer</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

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

        <!--服务提供者的接口API-->
        <dependency>
            <groupId>com.soft</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>v1.0</version>
        </dependency>

        <!-- dubbo依赖 -->
        <dependency>
            <groupId>com.alibaba.spring.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>

        <!-- 引入zookeeper的依赖 -->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application.properties

############### dubbo 配置 ###############################
# 端口
server.port=9021
spring.dubbo.application.name=dubbo-consumer
spring.dubbo.application.registry=zookeeper://Ip:2181

DubboConsumerApplication.java

package com.soft.consumer;

import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableDubboConfiguration //开启Dubbo的自动配置
@SpringBootApplication
public class DubboConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(DubboConsumerApplication.class, args);
    }

}

ConsumerController.java

package com.soft.consumer.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.soft.api.service.IProviderDemo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConsumerController {

    @Reference
    private IProviderDemo providerDemo;

    @RequestMapping(value = "/callInterfase")
    public String callInterfase() {
        String hello = providerDemo.providerReturnString("consumer test ");
        System.out.println(providerDemo.providerReturnString("consumer print "));
        return hello;
    }
}

2.3.3 创建API

[dubbo学习] 简单的dubbo功能

 API提供生产者暴露的接口

pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>springboot-dubbo</groupId>
        <artifactId>springboot-dubbo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <groupId>com.soft</groupId>
    <artifactId>dubbo-api</artifactId>
    <version>v1.0</version>


</project>

IProviderDemo.java

package com.soft.api.service;

public interface IProviderDemo {

    String providerReturnString(String name);
}

2.4 测试

依次启动生产者和消费者

[dubbo学习] 简单的dubbo功能[dubbo学习] 简单的dubbo功能

 范围http://127.0.0.1:9021/callInterfase

[dubbo学习] 简单的dubbo功能

 查看http://127.0.0.1:8090/dubbo-admin-2.5.4/

[dubbo学习] 简单的dubbo功能

Success!

相关推荐