基于Dubbo的分布式小Demo

本文涉及的产品
注册配置 MSE Nacos/ZooKeeper,118元/月
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
云原生网关 MSE Higress,422元/月
简介: 基于Dubbo的分布式小Demo

整体架构:
image.png
下载链接:

Doubbo:https://github.com/apache/dubbo

Zookeeper:https://downloads.apache.org/zookeeper/

Doubbo-admin:https://github.com/apache/dubbo-admin

1 首先安装Zookpeer

[root@bogon tmp]# ls
apache-zookeeper-3.7.0-bin.tar.gz  jdk1.8.0_141  jdk8.tar.gz
[root@bogon tmp]# tar -zxvf apache-zookeeper-3.7.0-bin.tar.gz 
[root@bogon tmp]# tar -zxvf jdk8.tar.gz 
[root@bogon tmp]# mv jdk1.8.0_141/ /usr/local/
[root@bogon tmp]# mv apache-zookeeper-3.7.0-bin/ /usr/local/zookpeer
[root@bogon tmp]# cd /usr/local/
[root@bogon local]# ls
bin  etc  games  include  jdk1.8.0_141  lib  lib64  libexec  sbin  share  src  zookeeper
[root@bogon local]# vim /etc/profile
# --------写入文件------------
export JAVA_HOME=/usr/local/jdk1.8.0_141
export JER_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib:$CLASSPATH
export JAVA_PATH=${JAVA_HOME}/bin:${JRE_HOME}/bin
export PATH=$PATH:${JAVA_PATH}
# --------写入文件完成------------
[root@bogon local]# source /etc/profile
[root@bogon local]# java -version
java version "1.8.0_141"
Java(TM) SE Runtime Environment (build 1.8.0_141-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.141-b15, mixed mode)
[root@bogon local]# cd zookeeper/
[root@bogon conf]# cp zoo_sample.cfg zoo.cfg
[root@bogon conf]# cd ../bin
[root@bogon bin]# ./zkServer.sh start
[root@bogon bin]# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@bogon bin]# ./zkCli.sh 
Connecting to localhost:2181
......
[zk: localhost:2181(CONNECTED) 1] ls /
[zookeeper]

zookpeer的启动和重启命令:

[root@bogon bin]# ./zkServer.sh start   //启动
[root@bogon bin]# ./zkServer.sh stop    //停止
[root@bogon bin]# ./zkServer.sh status  //查看当前状态
[root@bogon bin]# ./zkServer.sh restart  //重启
[root@bogon bin]# ./zkServer.sh version   //查看当前版本

2 创建Spring Boot项目

2.1 服务提供方dubbo_provider

image.png

pom.xml依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba.boot</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>0.2.0</version>
    </dependency>
    <dependency>
        <groupId>org.ymx</groupId>
        <artifactId>doubbo_interface</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

application.properties配置文件:

server.port=8081
dubbo.application.name=my_provider
dubbo.registry.protocol=zookeeper
dubbo.registry.address=192.168.190.129:2181
dubbo.protocol.name=dubbo

UserServiceImpl接口实现类:

import com.alibaba.dubbo.config.annotation.Service;
import org.ymx.doubbo_interface.service.UserService;

/**
 * @desc: 接口实现类
 * @author: YanMingXin
 * @create: 2021/8/7-9:32
 **/
@Service
public class UserServiceImpl implements UserService {
    @Override
    public String sayHello(String name) {
        return "Hello " + name;
    }
}

主启动类:

@SpringBootApplication
@EnableDubbo(scanBasePackages = "org.ymx.*")
public class DoubboProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(DoubboProviderApplication.class, args);
    }
}

2.2 服务消费方dubbo_consumer

image.png

pom.xml依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba.boot</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>0.2.0</version>
    </dependency>
    <dependency>
        <groupId>org.ymx</groupId>
        <artifactId>doubbo_interface</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

application.properties配置文件

dubbo.application.name=my_consumer
dubbo.registry.protocol=zookeeper
dubbo.registry.address=192.168.190.129:2181
dubbo.protocol.name=dubbo

UserController控制器类

/**
 * @desc: Controller控制类
 * @author: YanMingXin
 * @create: 2021/8/7-10:08
 **/
@RestController
public class UserController {

    @Reference
    private UserService userService;

    @RequestMapping("/hello/{name}")
    public String hello(@PathVariable("name") String name) {
        return userService.sayHello(name);
    }

}

主启动类:

@SpringBootApplication
@EnableDubbo(scanBasePackages = "org.ymx.*")
public class DoubboConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(DoubboConsumerApplication.class, args);
    }
}

2.3 公共接口dubbo_interface

image.png

pom.xml依赖

<dependencies>
    <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>
</dependencies>

application.properties配置文件

UserService接口类

/**
 * @desc: 公共接口
 * @author: YanMingXin
 * @create: 2021/8/7-10:07
 **/
public interface UserService {

    String sayHello(String name);

}

主启动类:

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

2.4 启动后观察Zookpeer并测试

doubbo_interface不需要启动,只需要install就可以了

[zk: localhost:2181(CONNECTED) 14] ls /
[dubbo, zookeeper]
[zk: localhost:2181(CONNECTED) 15] ls /dubbo
[org.ymx.doubbo_consumer.service.UserService, org.ymx.doubbo_provider.service.UserService]

测试:

image.png

3 dubbo管理界面

放到tomcat中
image.png
修改配置文件,在
image.png

修改内容

dubbo.registry.address=zookeeper://192.168.190.129:2181  # 注册中心地址
dubbo.admin.root.password=root   # root账户的密码
dubbo.admin.guest.password=guest  # guest账户的密码

启动

访问:http://localhost:8080/dubbo-admin/
image.png

相关实践学习
基于MSE实现微服务的全链路灰度
通过本场景的实验操作,您将了解并实现在线业务的微服务全链路灰度能力。
相关文章
|
7月前
|
监控 Dubbo 前端开发
快速入门分布式系统与Dubbo+zookeeper Demo
快速入门分布式系统与Dubbo+zookeeper Demo
488 0
|
7月前
|
监控 负载均衡 Dubbo
Dubbo 框架揭秘:分布式架构的精髓与魔法【一】
Dubbo 框架揭秘:分布式架构的精髓与魔法【一】
241 0
|
3月前
|
安全 应用服务中间件 API
微服务分布式系统架构之zookeeper与dubbo-2
微服务分布式系统架构之zookeeper与dubbo-2
|
3月前
|
负载均衡 监控 Dubbo
分布式框架-dubbo
分布式框架-dubbo
|
3月前
|
Dubbo Java 应用服务中间件
分布式-dubbo的入门
分布式-dubbo的入门
|
3月前
|
存储 Dubbo 应用服务中间件
分布式-Dubbo-Dubbo的由来
分布式-Dubbo-Dubbo的由来
|
3月前
|
XML 负载均衡 监控
分布式-dubbo-简易版的RPC框架
分布式-dubbo-简易版的RPC框架
|
3月前
|
负载均衡 监控 Dubbo
分布式-Dubbo-dubbo能解决什么问题
分布式-Dubbo-dubbo能解决什么问题
|
6月前
|
缓存 NoSQL 数据库
分布式系统面试全集通第一篇(dubbo+redis+zookeeper----分布式+CAP+BASE+分布式事务+分布式锁)
分布式系统面试全集通第一篇(dubbo+redis+zookeeper----分布式+CAP+BASE+分布式事务+分布式锁)
113 0
|
6月前
|
Dubbo Java 应用服务中间件
DUBBO--基础篇(一)--简介(示意Demo)
DUBBO--基础篇(一)--简介(示意Demo)
102 0

热门文章

最新文章