案例驱动体验
案例驱动体验
重要
通过 sqshq/piggymetrics
学习和了解微服务架构
Microservice Architecture with Spring Boot, Spring Cloud and Docker
基于 piggymetrics 搭建微服务,以 sqshq/piggymetrics
项目为载体,达成2个目的:
- 本地启动项目
- 容器化部署项目
参考资料:
版本说明
版本适配是项目首要关注点,关乎环境搭建。
版本会随着语言、组件、框架等的升级,造成项目与版本不相适配的潜在问题,进而导致项目工程无法启动、部署。因此,对于任何一个工程项目,应优先关注版本适配问题。
组件/语言/框架/工具 | 版本 |
---|---|
Java | 8 |
SpringBoot | 2.0.3.RELEASE |
SpringCloud | Finchley.RELEASE |
MongoDB | 5.0.5 |
RabbitMQ | 3.9.11 |
IDEA | 2022.3.1 |
- https://docs.spring.io/spring-data/mongodb/docs/3.2.4/reference/html/#preface mongodb版本适配
- https://docs.spring.io/spring-data/mongodb/docs/3.2.4/reference/html/#compatibility.matrix 兼容性适配
本地启动项目
环境准备
部署 MongDB
# 安装
docker pull mongodb:latest
# 运行
docker run -itd --name mongodb -p 27017:27017 mongo:latest --auth
# 进容器
docker ps
docker exec -it 镜像ID /bin/bash
# 登录
mongo
# 查看当前数据库,确认是否为 piggymetrics 库
db
# 切换到 piggymetrics 库
use piggymetrics
# 添加数据库用户
db.createUser(
{
user: "user",
pwd: "password",
roles: [ { role: "root", db: "admin" } ]
}
)
# 使用 db.getUser 或 db.auth 验证
db.getUser('user');
db.auth('user','password');
执行数据初始化脚本:
// 初始化数据 account-service-dump.js
db.accounts.update(
{ "_id": "demo" },
{
"_id": "demo",
"lastSeen": new Date(),
"note": "demo note",
"expenses": [
{
"amount": 1300,
"currency": "USD",
"icon": "home",
"period": "MONTH",
"title": "Rent"
},
{
"amount": 120,
"currency": "USD",
"icon": "utilities",
"period": "MONTH",
"title": "Utilities"
},
{
"amount": 20,
"currency": "USD",
"icon": "meal",
"period": "DAY",
"title": "Meal"
},
{
"amount": 240,
"currency": "USD",
"icon": "gas",
"period": "MONTH",
"title": "Gas"
},
{
"amount": 3500,
"currency": "EUR",
"icon": "island",
"period": "YEAR",
"title": "Vacation"
},
{
"amount": 30,
"currency": "EUR",
"icon": "phone",
"period": "MONTH",
"title": "Phone"
},
{
"amount": 700,
"currency": "USD",
"icon": "sport",
"period": "YEAR",
"title": "Gym"
}
],
"incomes": [
{
"amount": 42000,
"currency": "USD",
"icon": "wallet",
"period": "YEAR",
"title": "Salary"
},
{
"amount": 500,
"currency": "USD",
"icon": "edu",
"period": "MONTH",
"title": "Scholarship"
}
],
"saving": {
"amount": 5900,
"capitalization": false,
"currency": "USD",
"deposit": true,
"interest": 3.32
}
},
{ upsert: true }
);
部署成功:

数据初始化成功:

部署 RabbitMQ
#安装
docker pull rabbitmq:latest
#运行
docker run -d --hostname rabbitmq --name rabbit -p 15672:15672 -p 5672:5672 rabbitmq
#进容器内
docker ps
docker exec -it 镜像ID /bin/bash
#安装控制台
rabbitmq-plugins enable rabbitmq_management
访问:http://localhost:15672 , 账户/密码:guest/guest

安装 IDEA插件
- 安装 EnvFile 插件
调整配置文件
调整config项目下 shard/*.yml
的配置以及其他微服务中 bootstrap.yml
的配置。

调整内容(未全列出,参照【图:需调整的配置文件】):
- 实例名改为
IP
或localhost
,如:
# shared/account-service.yml
## Security配置中,auth-service 改为 localhost
accessTokenUri: http://auth-service:5000/uaa/oauth/token
accessTokenUri: http://localhost:5000/uaa/oauth/token
# shared/application.yml
## Eureka配置中,registry 改为 localhost
defaultZone: http://registry:8761/eureka/
defaultZone: http://localhost:8761/eureka/
## Security配置中,auth-service 改为 localhost
user-info-uri: http://auth-service:5000/uaa/users/current
user-info-uri: http://localhost:5000/uaa/users/current
## RabbitMQ rabbitmq 改为 localhost
host: rabbitmq
host: localhost
启动服务
按照以下顺序依次启动服务,其余服务无顺序要求:

registry
服务启动成功后,访问 http://127.0.0.1:8761 :

gateway
服务启动成功后,访问 http://127.0.0.1:4000

问题记录
连接 MongoDB
失败,报 MongoSecurityException
,参考这篇。

在 shared/account-service.yml
指定 authentication-database
,且需明文指定 password
,默认的 password: ${MONGODB_PASSWORD}
在本地启动时,存在无法读取 .env
文件问题:
spring:
data:
mongodb:
# 本地环境
host: localhost
username: user
password: password
database: piggymetrics
# 指定鉴权数据库
authentication-database: admin
port: 27017
其余配置文件中,如果也有 MongoDB
的配置,那么也需一并改造,如以下文件均需指定 authentication-database
配置:
shared/auth-service.yml
shared/statistics-service.yml