我在自己家里用近10台树莓派搭建了一个集群,平时在这个集群上学习一些技术知识。虽然这些机器性能不太好,但用起来比虚拟机和Docker要方便一些,应为虚拟机要装在自己电脑上,每次都要重启。

这个源代码是基于这个树莓派集群搭建的微服务的框架,采用Spring Cloud框架,包括了服务网关(Spring Cloud Gateway),服务器注册与发现(Spring Cloud Eureka),配置中心(Spring Cloud Config),服务调用(Spring Cloud OpenFeign),Redis数据库集群,MySQL数据库集群,RabbitMQ消息队列集群。

希望对想学习Spring Cloud微服务的同学有所帮助。

整体架构

image-20210912084915867

基于这套集群,正好这段时间离职在家,搭建了下图所示的一套基于Spring Cloud的微服务框架。整个系统由大概10台服务器组成,其中5台为树莓派3B的板子,内存均为1G。另外有三台方糖的cubieboard1,是很早之前买的,一直放着没用(cubieboard1的质量还真不错,8年了还能稳定运行)。

SpringCloud

上图中,蓝色的nginx为外网服务器,其他的均为内网服务器。nginx将请求转发给Spring Cloud Gateway, Gateway再根据请求地址分发到不同的微服务上。由于Gateway没用公网地址,它通过 frp 内网穿透的方式让外网的nginx服务器能够访问。整体的服务器清单大致如下表所示。

服务器 应用 类型 备注
liwenbo.net nginx 腾讯云 外网服务器
192.168.3.200 Gateway[0]
Config[0]
树莓派3B+ 树莓派3B+,内网入口,通过frp连接外网
192.168.3.201 mysql[0] 树莓派3B+
192.168.3.202 Eureka[0]
Gateway[1]
Config[1]
pcDuino
192.168.3.203 member-api[0]
order-api[0]
product-api[0]
树莓派3B+
192.168.3.204 redis-master[0]
redis-sentinel[0]
order-api[1]
cubieboard1
192.168.3.205 redis-slave[0]
redis-slave[1]
redis-slave[2]
redis-sentinel[1]
cubieboard1
192.168.3.206 RabbitMQ[0]
member-api[1]
product-api[1]
树莓派3B+
192.168.3.207 RabbitMQ[1] 树莓派3B+
192.168.3.208 空闲 虚拟机 4核/4G
192.168.3.209 gitlab, Jenkins 虚拟机 4核/4G

树莓派的安装

先准备一张至少8GB的microsd卡,最好16G或32G。到树莓派官方下载镜像制作工具,也可以使用Win32 Disk Imager。网上有很多教程,直接将镜像烧录到sd卡中。

image-20210911210312396

建议选择不带桌面的最小版本,内存占用少。

image-20210911210515222

将sd卡插入树莓派卡槽,然后上电,进入系统,默认用户名密码是 pi / raspberry。该系统应该是一个基于Debian的定制系统,我们大致按照Debian的方式操作即可。

默认系统是禁用了ssh的,如果要远程连接服务器,可以使用 raspi-config 命令先开启ssh。

1
sudo raspi-config

默认官方的源速度比较慢,我们可以根据自己的网络状况,换成清华的源试试。

换清华的源,参考: https://mirrors.tuna.tsinghua.edu.cn/help/raspbian/

1
2
3
4
5
6
# 编辑 `/etc/apt/sources.list` 文件,删除原文件所有内容,用以下内容取代:
deb http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ buster main non-free contrib rpi
deb-src http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ buster main non-free contrib rpi

# 编辑 `/etc/apt/sources.list.d/raspi.list` 文件,删除原文件所有内容,用以下内容取代:
deb http://mirrors.tuna.tsinghua.edu.cn/raspberrypi/ buster main ui

对于需要Java的环境,我们可以安装openjdk-11的版本。

1
2
sudo apt-get update
sudo apt-get install openjdk-11-jdk

image-20210910103850700

至此,树莓派就可以当成一个性能较差的Linux服务器使用了。