Dubbo+Zookeeper

本文最后更新于2023.03.28-21:33,某些文章具有时效性,若有错误或已失效,请在下方留言或联系涛哥

什么是Dubbo?

Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。简单的说,dubbo就是个服务框架,如果没有分布式的需求,其实是不需要用的,只有在分布式的时候,才有dubbo这样的分布式服务框架的需求,并且本质上是个服务调用的东东,说白了就是个远程服务调用的分布式框架(告别Web Service模式中的WSdl,以服务者与消费者的方式在dubbo上注册) 其核心部分包含: \1. 远程通讯: 提供对多种基于长连接的NIO框架抽象封装,包括多种线程模型,序列化,以及“请求-响应”模式的信息交换方式。 \2. 集群容错: 提供基于接口方法的透明远程过程调用,包括多协议支持,以及软负载均衡,失败容错,地址路由,动态配置等集群支持。 \3. 自动发现: 基于注册中心目录服务,使服务消费方能动态的查找服务提供方,使地址透明,使服务提供方可以平滑增加或减少机器。

Dubbo能做什么

1.透明化的远程方法调用,就像调用本地方法一样调用远程方法,只需简单配置,没有任何API侵入。

2.软负载均衡及容错机制,可在内网替代F5等硬件负载均衡器,降低成本,减少单点。

3. 服务自动注册与发现,不再需要写死服务提供方地址,注册中心基于接口名查询服务提供者的IP地址,并且能够平滑添加或删除服务提供者。

一,下载Zookeeper注册中心

1.1,什么是Zookeeper

Zookeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、名字服务、分布式同步、组服务等。 之所以把本文归类为Dubbo是因为,博主关于Dubbo的学习笔记都是以Zookeeper作为注册中心的。

Zookeeper作为Dubbo服务的注册中心,Dubbo原先基于数据库的注册中心,没采用Zookeeper,Zookeeper一个分布式的服务框架,是树型的目录服务的数据存储,能做到集群管理数据 ,这里能很好的作为Dubbo服务的注册中心,Dubbo能与Zookeeper做到集群部署,当提供者出现断电等异常停机时,Zookeeper注册中心能自动删除提供者信息,当提供者重启时,能自动恢复注册数据。

1.2,zookeeper安装部署

1.2.1,下载zookeeper

官网可下载:http://www.apache.org/dyn/closer.cgi/zookeeper/

1.2.2,下载完成后解压到自己需要放的位置并将cof目录下的zoo_sample.cfg改名为zoo.cfg;创建两个文件夹log和data并修改dataDir路径

dataDir=F:\开发环境\zookeeper\apache-zookeeper-3.8.0\data
dataDir=F:\开发环境\zookeeper\apache-zookeeper-3.8.0\log

1.2.3 配置环境变量

ZOOKEEPER_HOME = zookeeper安装路径
path = %ZOOKEEPER_HOME%/bin

1.2.4 运行

先启动Zookeeper目录下bin下的zkServer.cmd(不要关闭)再启动zkCli.cmd连接zookeeper

这样我们的zookeeper环境就搭建好了

二,下载dubbo-admin

安装部署

  1. 在需要放置的盘符输入cmd命令下载代码: git clone https://github.com/apache/dubbo-admin.git
  2. dubbo-admin-server/src/main/resources/application.properties中指定注册中心地址
  3. 构建 mvn clean package -Dmaven.test.skip=true 
  4. 启动
    • mvn --projects dubbo-admin-server spring-boot:run 或者

    • cd dubbo-admin-distribution/target; java -jar dubbo-admin-0.1.jar

  1. 访问 http://localhost:8080

  2. npm run serve 启动前端 

三,搭建dubbo环境

3.1,项目结构

3.2,新建一个dubbo-zookeeper springboot项目什么的不选,删除src,mvnw.cmd,mvnw文件

父pom文件

<?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>com.dubbo</groupId>
    <artifactId>dubbo-zookeeper</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dubbo-zookeeper</name>
    <description>dubbo-zookeeper</description>
​
    <packaging>pom</packaging>
​
    <modules>
        <module>dubbo-consumer</module>
        <module>dubbo-interface</module>
        <module>dubbo-provider</module>
    </modules>
​
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
    </properties>
​
​
​
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
​
        </dependencies>
    </dependencyManagement>
​
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
​
</project>

3.3,新建一个moudle dubbo-interface

子pom文件

<?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>com.example</groupId>
    <artifactId>dubbo-interface</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dubbo-interface</name>
    <description>dubbo-interface</description>
​
    <parent>
        <groupId>com.dubbo</groupId>
        <artifactId>dubbo-zookeeper</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
​
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
​
​
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
​
</project>

新建DubboService接口

package com.example.dubbointerface.service;
​
public interface DubboService {
    String hello();
}​

 3.4,新建一个moudle dubbo-provider服务提供者

子pom文件

<?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>com.example</groupId>
    <artifactId>dubbo-provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dubbo-provider</name>
    <description>dubbo-provider</description>
    <!--子类继承父类-->
    <parent>
        <groupId>com.dubbo</groupId>
        <artifactId>dubbo-zookeeper</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
​
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>
​
    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>dubbo-interface</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
​
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>
    </dependencies>
​
​
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
​

核心配置

server:
  port: 8001
  #应用名
dubbo:
  application:
    name: dubbo-producer
  #zookeeper地址
  registry:
    address: 127.0.0.1:2181
  #指定注册中心类型
    protocol: zookeeper
  #传入协议的名称
  protocol:
    name: dubbo
  #dubbo服务暴露的端口,可自定义
    port: 20888

创建DubboService的实现类DubboServiceImpl

ps:注意是使用com.alibab的sercie注解

package com.example.dubboprovider.sercvice.impl;
​
import com.alibaba.dubbo.config.annotation.Service;
import com.example.dubbointerface.service.DubboService;
​
​
@Service
public class DubboServiceImpl implements DubboService{
    @Override
    public String hello() {
        return "hello dubbo";
    }
}​

 启动类添加@EnableDubbo​注解

package com.example.dubboprovider;
​
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
​
@SpringBootApplication
@EnableDubbo
public class DubboProviderApplication {
​
    public static void main(String[] args) {
        SpringApplication.run(DubboProviderApplication.class, args);
    }
​
}

3.5,新建一个moudle dubbo-consumer消费者

子pom文件

<?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>com.dubbo</groupId>
        <artifactId>dubbo-zookeeper</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
​
    <groupId>com.example</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>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>
​
    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>dubbo-provider</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>
    </dependencies>
​
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
​
</project>

核心配置

dubbo:
  application:
    name:  dubbo-consumer
  registry:
    address: 127.0.0.1:2181
    protocol: zookeeper
    check: false
  monitor:
    protocol: register
  consumer:
    check:  false
    timeout: 3000
server:
  port: 8085​

 新建controller访问层

ps:注意使用com.alibaba的reference注解来远程调用DubboService接口

package com.example.dubboconsumer.controller;
​
​
import com.alibaba.dubbo.config.annotation.Reference;
​
​
import com.example.dubbointerface.service.DubboService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
​
​
@RestController
public class DubboController {
    @Reference
    private DubboService dubboService;
​
    @RequestMapping("/hello")
    public String hello(){
        return dubboService.hello();
    }
}

 启动类

package com.example.dubboconsumer;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
​
@SpringBootApplication
@EnableDubbo
public class DubboConsumerApplication {
​
    public static void main(String[] args) {
        SpringApplication.run(DubboConsumerApplication.class, args);
    }
​
}

四,启动流程

1,启动zookeeper服务和dubbo-admin管理中心

2,启动dubbo-provider

3,启动dubbo-consumer

4,访问:http://localhst:8085/hello

5,效果图

阅读剩余
THE END