这个模块是一个简单的 Java 客户端,它是为了测试我们的场景而创建的。
您可以指定两个参数——您要购买的足球运动员的 ID,以及您的报价的价值。如果不指定这些参数,默认值将分别为 1 (Gianlugi Buffon) 和 2200 万。
现在,是时候测试它了。运行以下命令:
$ java -jar target/Football_Player_Market_client-1.0.0.jar 18 1
我出价 18 百万尝试购买 Gianluigi Buffon;我的报价将被视为有效,因为 1800 万是玩家价值的 80% 以上。因此,Narayana LRA 协调员将执行完整阶段。
以下是 football-player-market-microservice-lra 的摘录,显示了 LRA 协调员针对微服务执行的工作流:
2018-08-24 17:32:47,851 INFO [com.packtpub.thorntail.footballplayermicroservice.rest.service.FootballPlayerMarketREST] (default task-1) Start method sendOffer
2018-08-24 17:32:47,851 FINE [com.packtpub.thorntail.footballplayermicroservice.rest.service.FootballPlayerMarketREST] (default task-1) Retrieving football player with id 3
2018-08-24 17:32:47,853 FINE [com.packtpub.thorntail.footballplayermicroservice.rest.service.FootballPlayerMarketREST] (default task-1) Value of LRA_ID 0_ffff7f000001_-595eb3c1_5b80245b_17
2018-08-24 17:32:47,853 FINE [com.packtpub.thorntail.footballplayermicroservice.rest.service.FootballPlayerMarketREST] (default task-1) Creating offer ...
2018-08-24 17:32:48,452 FINE [com.packtpub.thorntail.footballplayermicroservice.rest.service.FootballPlayerMarketREST] (default task-1) Offer created with response code 204
2018-08-24 17:32:48,874 FINE [com.packtpub.thorntail.footballplayermicroservice.rest.service.FootballPlayerMarketREST] (default task-1) Got football player FootballPlayer{id=3, name=Keylor, surname=Navas, age=31, team=Real Madrid, position=goalkeeper, price=18, status=null, lraId=null}
2018-08-24 17:32:48,874 FINE [com.packtpub.thorntail.footballplayermicroservice.rest.service.FootballPlayerMarketREST] (default task-1) Changing football player status ...
2018-08-24 17:32:49,018 INFO [stdout] (default task-1) Value of offer price: 18
2018-08-24 17:32:49,018 INFO [stdout] (default task-1) Value of football player price: 18
2018-08-24 17:32:49,018 FINE [com.packtpub.thorntail.footballplayermicroservice.rest.service.FootballPlayerMarketREST] (default task-1) Value of offer price 18
2018-08-24 17:32:49,018 FINE [com.packtpub.thorntail.footballplayermicroservice.rest.service.FootballPlayerMarketREST] (default task-1) Value of football player price 18
2018-08-24 17:32:49,018 INFO [com.packtpub.thorntail.footballplayermicroservice.rest.service.FootballPlayerMarketREST] (default task-1) End method sendOffer
2018-08-24 17:32:49,131 INFO [com.packtpub.thorntail.footballplayermicroservice.rest.service.FootballPlayerMarketREST] (default task-2) Start method confirmOffer: I'm in LRA complete phase
2018-08-24 17:32:49,132 FINE [com.packtpub.thorntail.footballplayermicroservice.rest.service.FootballPlayerMarketREST] (default task-2) Value of header lraId http://localhost:8580/lra-coordinator/0_ffff7f000001_-595eb3c1_5b80245b_17
2018-08-24 17:32:49,132 FINE [com.packtpub.thorntail.footballplayermicroservice.rest.service.FootballPlayerMarketREST] (default task-2) Value of lraIdParameter 0_ffff7f000001_-595eb3c1_5b80245b_17
2018-08-24 17:32:49,132 FINE [com.packtpub.thorntail.footballplayermicroservice.rest.service.FootballPlayerMarketREST] (default task-2) Setting the offer as ACCEPTED ...
2018-08-24 17:32:49,306 FINE [com.packtpub.thorntail.footballplayermicroservice.rest.service.FootballPlayerMarketREST] (default task-2) Set the offer as ACCEPTED ...
2018-08-24 17:32:49,335 FINE [com.packtpub.thorntail.footballplayermicroservice.rest.service.FootballPlayerMarketREST] (default task-2) Got football player FootballPlayer{id=3, name=Keylor, surname=Navas, age=31, team=Real Madrid, position=goalkeeper, price=18, status=Reserved, lraId=0_ffff7f000001_-595eb3c1_5b80245b_17}
2018-08-24 17:32:49,335 FINE [com.packtpub.thorntail.footballplayermicroservice.rest.service.FootballPlayerMarketREST] (default task-2) Changing football player status ...
2018-08-24 17:32:49,353 INFO [com.packtpub.thorntail.footballplayermicroservice.rest.service.FootballPlayerMarketREST] (default task-2) End method confirmOffer: LRA complete phase terminated
我已经突出显示了显示业务方法的开始和结束以及由 LRA 协调员组织的完整阶段的日志消息。
如果你在数据库端执行检查,你会注意到 Gianluigi Buffon 的状态是 "purchased":
$ SELECT * FROM football_player where id = 1
上述命令的输出如下:
id name surname age team position price
1 Gianluigi Buffon 40 Paris Saint Germain goalkeeper 2
status lraid
Purchased ""
如果您检查足球运动员的报价,如下所示,您将能够验证状态是 ACCEPTED:
$ SELECT * FROM FOOTBALL_PLAYER_OFFER WHERE ID_FOOTBALL_PLAYER = 1
上述命令的输出如下:
ID ID_FOOTBALL_PLAYER ID_FOOTBALL_MANAGER PRICE STATUS
1 1 1 18 ACCEPTED
现在,我们将模拟一个故障场景。
我将出价5百万尝试购买曼努埃尔·诺伊尔;我的报价将被视为无效,因为 5 百万还不到玩家价值的 80%。因此,Narayana LRA 协调员将执行补偿阶段。
让我们运行以下命令:
$ java -jar target/Football_Player_Market_client-1.0.0.jar 5 2
以下是 football-player-market-microservice-lra 的摘录,显示了 LRA 协调员针对微服务执行的工作流:
2018-08-25 00:18:20,007 INFO [com.packtpub.thorntail.footballplayermicroservice.rest.service.FootballPlayerMarketREST] (default task-4) Start method sendOffer
2018-08-25 00:18:20,008 FINE [com.packtpub.thorntail.footballplayermicroservice.rest.service.FootballPlayerMarketREST] (default task-4) Retrieving football player with id 2
2018-08-25 00:18:20,008 FINE [com.packtpub.thorntail.footballplayermicroservice.rest.service.FootballPlayerMarketREST] (default task-4) Value of LRA_ID 0_ffff7f000001_205e9dee_5b808159_53
2018-08-25 00:18:20,008 FINE [com.packtpub.thorntail.footballplayermicroservice.rest.service.FootballPlayerMarketREST] (default task-4) Creating offer ...
2018-08-25 00:18:20,104 FINE [com.packtpub.thorntail.footballplayermicroservice.rest.service.FootballPlayerMarketREST] (default task-4) Offer created with response code 204
2018-08-25 00:18:20,105 INFO [org.apache.http.impl.execchain.RetryExec] (default task-4) I/O exception (org.apache.http.NoHttpResponseException) caught when processing request to {}->http://localhost:8080: The target server failed to respond
2018-08-25 00:18:20,106 INFO [org.apache.http.impl.execchain.RetryExec] (default task-4) Retrying request to {}->http://localhost:8080
2018-08-25 00:18:20,113 FINE [com.packtpub.thorntail.footballplayermicroservice.rest.service.FootballPlayerMarketREST] (default task-4) Got football player FootballPlayer{id=2, name=Manuel, surname=Neuer, age=32, team=Bayern Munchen, position=goalkeeper, price=35, status=null, lraId=null}
2018-08-25 00:18:20,113 FINE [com.packtpub.thorntail.footballplayermicroservice.rest.service.FootballPlayerMarketREST] (default task-4) Changing football player status ...
2018-08-25 00:18:20,129 INFO [stdout] (default task-4) Value of offer price: 5
2018-08-25 00:18:20,129 INFO [stdout] (default task-4) Value of football player price: 35
2018-08-25 00:18:20,129 FINE [com.packtpub.thorntail.footballplayermicroservice.rest.service.FootballPlayerMarketREST] (default task-4) Value of offer price 5
2018-08-25 00:18:20,129 FINE [com.packtpub.thorntail.footballplayermicroservice.rest.service.FootballPlayerMarketREST] (default task-4) Value of football player price 35
2018-08-25 00:18:20,130 ERROR [org.jboss.resteasy.resteasy_jaxrs.i18n] (default task-4) RESTEASY002010: Failed to execute: javax.ws.rs.WebApplicationException: The offer is unacceptable!
at com.packtpub.thorntail.footballplayermicroservice.rest.service.FootballPlayerMarketREST.sendOffer(FootballPlayerMarketREST.java:142)
at com.packtpub.thorntail.footballplayermicroservice.rest.service.FootballPlayerMarketREST$Proxy$_$$_WeldClientProxy.sendOffer(Unknown Source)
...
2018-08-25 00:18:20,152 INFO [com.packtpub.thorntail.footballplayermicroservice.rest.service.FootballPlayerMarketREST] (default task-5) Start method compensateWork: I'm in LRA compensate phase
2018-08-25 00:18:20,152 FINE [com.packtpub.thorntail.footballplayermicroservice.rest.service.FootballPlayerMarketREST] (default task-5) Value of lraIdParameter 0_ffff7f000001_205e9dee_5b808159_53
2018-08-25 00:18:20,153 FINE [com.packtpub.thorntail.footballplayermicroservice.rest.service.FootballPlayerMarketREST] (default task-5) Setting the offer as REFUSED ...
2018-08-25 00:18:20,196 FINE [com.packtpub.thorntail.footballplayermicroservice.rest.service.FootballPlayerMarketREST] (default task-5) Set the offer as REFUSED ...
2018-08-25 00:18:20,203 FINE [com.packtpub.thorntail.footballplayermicroservice.rest.service.FootballPlayerMarketREST] (default task-5) Got football player FootballPlayer{id=2, name=Manuel, surname=Neuer, age=32, team=Bayern Munchen, position=goalkeeper, price=35, status=Reserved, lraId=0_ffff7f000001_205e9dee_5b808159_53}
2018-08-25 00:18:20,203 FINE [com.packtpub.thorntail.footballplayermicroservice.rest.service.FootballPlayerMarketREST] (default task-5) Changing football player status ...
2018-08-25 00:18:20,227 INFO [com.packtpub.thorntail.footballplayermicroservice.rest.service.FootballPlayerMarketREST] (default task-5) End method compensateWork: LRA compensate phase terminated
我已经突出显示了显示业务方法的开始和结束以及由 LRA 协调员组织的补偿阶段的日志消息。
如果您在数据库端执行检查,您会注意到 Manuel Neuer 的 status 为 Free:
$ java -jar target/Football_Player_Market_client-1.0.0.jar 5 2
上述命令的输出如下:
id name surname age team position price status lraid
2 Manuel Neuer 32 Bayern Munchen goalkeeper 35 Free ""
如果您查看足球运动员的报价,您可以验证状态是否为 REFUSED:
$ SELECT * FROM FOOTBALL_PLAYER_OFFER WHERE ID_FOOTBALL_PLAYER = 2
上述命令的输出如下:
2 2 1 5 REFUSEDID ID_FOOTBALL_PLAYER ID_FOOTBALL_MANAGER PRICE STATUS