基于Eventuate的微服务应用Money Transfer分析

来源:互联网 发布:看得见风景的房间 知乎 编辑:程序博客网 时间:2024/05/29 18:09

Money Transfer是Chris Richardson在自己的官方博客提供的一个账户转账的应用示例,其核心是基于Eventuate实现的。Eventuate提供了分布式数据的最终一致性实现。

1. 在Money Transfer应用中,集中展现了微服务的如下特性:

  • microservices
  • polyglot persistence
  • event sourcing (ES)
  • command query responsibility segregation (CQRS)

2. 在Money Transfer应用中,组件的部署分为如下几个独立的服务:

  • API gateway Service
  • Account Service
  • Account View Service
  • Customer Service
  • Customer View Service
  • Transaction Service

其中API gateway Service否则整个应用的请求分发。其他几个是业务相关的服务,其中关键的是Transaction Service。

每个服务都有自己私有的数据库,可能是SQL,也可能是NOSQL。

跨服务的数据操作(即分布式事务)是通过event的订阅与发布而实现最终一致性。这又是借助于第三方的事件架构软件Eventuate实现的。


3. Money Transfer应用的源代码组成

  • java-spring
  • scala-spring
  • js-frontend
  • prebuilt-web-client
其中js-frontend和prebuilt-web-client是应用的两个客户端实现。prebuilt-web-client是基于js的网页,而js-frontend是基于NodeJS的应用。

java-spring是应用的服务端Java实现,scala-spring是应用的服务端Scala实现。下面以java-spring为例进行分析。

1) java-spring源代码的构建与使用

  • 安装Gradle(可选)
  • 编译
cd java-spring
./gradlew assemble -P eventuateDriver=local
  • 启动服务
export DOCKER_HOST_IP=...#不能是localhost
docker-compose -f docker-compose-eventuate-local.yml up -d
  • 浏览器访问
http://$DOCKER_HOST_IP:8080


2) Transaction Service的分析



3) API gateway Service的分析



在API gateway Service中,配置属性文件如下:

accounts.commandside.service.host=localhostaccounts.queryside.service.host=localhostcustomers.commandside.service.host=localhostcustomers.queryside.service.host=localhosttransfers.commandside.service.host=localhostapi.gateway.endpoints[0].path=[/]*api/accounts.*api.gateway.endpoints[0].method=GETapi.gateway.endpoints[0].location=http://${accounts.queryside.service.host}:8080api.gateway.endpoints[1].path=[/]*api/customers.*/accountsapi.gateway.endpoints[1].method=GETapi.gateway.endpoints[1].location=http://${accounts.queryside.service.host}:8080api.gateway.endpoints[2].path=[/]*api/accounts.*api.gateway.endpoints[2].method=POSTapi.gateway.endpoints[2].location=http://${accounts.commandside.service.host}:8080api.gateway.endpoints[3].path=[/]*api/customers.*api.gateway.endpoints[3].method=GETapi.gateway.endpoints[3].location=http://${customers.queryside.service.host}:8080api.gateway.endpoints[4].path=[/]*api/customers.*api.gateway.endpoints[4].method=POSTapi.gateway.endpoints[4].location=http://${customers.commandside.service.host}:8080api.gateway.endpoints[5].path=[/]*api/transfers.*api.gateway.endpoints[5].method=POSTapi.gateway.endpoints[5].location=http://${transfers.commandside.service.host}:8080api.gateway.endpoints[6].path=[/]*api/customers.*api.gateway.endpoints[6].method=DELETEapi.gateway.endpoints[6].location=http://${customers.commandside.service.host}:8080api.gateway.endpoints[7].path=[/]*api/accounts.*api.gateway.endpoints[7].method=DELETEapi.gateway.endpoints[7].location=http://${accounts.commandside.service.host}:8080
可见,通过配置实现了对不同服务的聚合和调度。


参考链接:

http://eventuate.io/
http://eventuate.io/exampleapps.html
https://github.com/cer/event-sourcing-examples

原创粉丝点击