跳转到主要内容
Chinese, Simplified
  • 第 1 部分:简介和架构
  • 第 2 部分:设置 Kubernetes 和 Kafka
  • 第 3 部分:设置保险柜
  • 第 4 部分:构建微服务
  • 第 5 部分:部署和测试 <--本文

部署微服务

 

构建镜像并部署到 Kubernetes

 

  • 将命令行控制台指向 $PROJECTS/k8s 文件夹。运行命令
> jhipster kubernetes

 

  • 提出的选择是:
  • which * type *- 选择微服务应用
  • 输入根目录 - 在我们的例子中,我们使用 (../)
  • 当被问及您想要包含哪个应用程序时 - 选择 GatewayKafka、Transaction 和 DepositAccount
  • 确保输入注册表管理员密码
  • 对于 Kubernetes 命名空间 - 选择默认值
  • 对于基础 Docker 存储库 - 使用您的 Docker Hub 用户名
  • 推送 docker 镜像 - 选择 docker push
  • 对于 istio - 设置为否
  • 对于边缘服务的 Kubernetes 服务类型 - 选择 LoadBalancer
  • 对于动态存储配置 - 是
  • 对于存储类,使用默认存储类 - 将答案留空
  • 成功后,您将看到以下屏幕
Kubernetes configuration successfully generated!
WARNING! You will need to push your image to a registry. If you have not done so, use the following commands to tag and push the images:
  docker image tag depositaccount azrulhasni/depositaccount
  docker push azrulhasni/depositaccount
  docker image tag gatewaykafka azrulhasni/gatewaykafka
  docker push azrulhasni/gatewaykafka
  docker image tag transaction azrulhasni/transaction
  docker push azrulhasni/transaction
INFO! Alternatively, you can use Jib to build and push image directly to a remote registry:
  ./mvnw -ntp -Pprod verify jib:build -Djib.to.image=azrulhasni/depositaccount in /Users/azrul/Documents/GitHub/Ebanking-JHipster-Kafka-Vault/DepositAccount
  ./mvnw -ntp -Pprod verify jib:build -Djib.to.image=azrulhasni/gatewaykafka in /Users/azrul/Documents/GitHub/Ebanking-JHipster-Kafka-Vault/GatewayKafka
  ./mvnw -ntp -Pprod verify jib:build -Djib.to.image=azrulhasni/transaction in /Users/azrul/Documents/GitHub/Ebanking-JHipster-Kafka-Vault/Transaction
You can deploy all your apps by running the following kubectl command:
  bash kubectl-apply.sh -f
[OR]
If you want to use kustomize configuration, then run the following command:
  bash kubectl-apply.sh -k
Use these commands to find your application's IP addresses:
  kubectl get svc gatewaykafka
INFO! Congratulations, JHipster execution is complete!

 

  • 我们将使用 Jib 版本。将命令行控制台指向 $PROJECTS/DepositAccount
  • 运行以下命令。这会将 DepositAccount 推送到 Docker Hub。
>./mvnw -ntp -Pprod verify jib:build -Djib.to.image=azrulhasni/depositaccount

 

  • 然后,转到 $PROJECTS/GatewayKafka 并运行以下命令。这会将 GatewayKafka 推送到 Docker Hub。
> ./mvnw -ntp -Pprod verify jib:build -Djib.to.image=azrulhasni/gatewaykafka

 

  • 最后,转到 $PROJECTS/Transaction 并运行以下命令。这会将事务推送到 Docker Hub。
>./mvnw -ntp -Pprod verify jib:build -Djib.to.image=azrulhasni/transaction

 

  • 然后,返回 $PROJECTS/k8s 并运行以下命令。这会将上面的所有三个图像拉到我们的 Kubernetes 集群中。
> bash kubectl-apply.sh -f

 

  • 要验证微服务是否已正确部署并运行,请运行以下命令:
>kubectl get  pods

 

  • 您将在下面看到结果。请注意,我们部署了每个微服务、其对应的数据库以及 JHipster 注册表。

No alt text provided for this image


测试微服务

 

  • 首先,我们需要安装 JQ。 JQ 是一个允许我们 grep json 数据的工具。 JQ 分布可以在这里找到 https://stedolan.github.io/jq/download/
  • 回想一下我们的架构。为了让我们调用事务微服务,我们必须通过我们的网关。还记得我们在创建网关时选择了 JWT 身份验证。运行以下命令为此类访问创建令牌。令牌将被导出到一个名为 TOKEN 的变量中。请注意,我们使用的是默认管理员用户和密码。我们应该为生产创建合适的用户。
> export TOKEN=`curl  -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{  "password": "admin",  "rememberMe": true,  "username": "admin"  }' 'http://localhost:8080/api/authenticate' | jq -r .id_token`

 

  • 要验证令牌,请运行:
> echo $TOKEN

 

  • 您应该得到如下响应
> echo $TOKEN
eyJhbGciOiJIUzUxMiJ9...AE2w

 

  • 首先,我们可能想要创建 2 个存款账户,我们也可以从中借记和贷记。使用下面的 curl 命令。我们将创建一个帐号为 1111 的帐户,余额为 10000。
> curl -X POST "http://localhost:8080/services/depositaccount/api/deposit-accounts" -H "accept: */*" -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" -d "{ \"accountNumber\": \"1111\", \"balance\": 10000, \"openingDate\": \"2020-10-17T11:55:02.749Z\", \"productId\": \"DEPOSIT\", \"status\": 0}"

{"id":1001,"accountNumber":"1111","productId":"DEPOSIT","openingDate":"2020-10-17T11:55:02.749Z","status":0,"balance":10000}
  • 然后创建第二个帐户。账号为2222,余额为0
> curl -X POST "http://localhost:8080/services/depositaccount/api/deposit-accounts" -H "accept: */*" -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" -d "{ \"accountNumber\": \"2222\", \"balance\": 0, \"openingDate\": \"2020-10-17T11:55:02.749Z\", \"productId\": \"DEPOSIT\", \"status\": 0}"

{"id":1002,"accountNumber":"2222","productId":"DEPOSIT","openingDate":"2020-10-17T11:55:02.749Z","status":0,"balance":0}
  • 现在是关键时刻。让我们将 10 从账户 1111 转移到账户 2222
> curl -X POST "http://localhost:8080/services/transaction/api/transaction-kafka/transfer" -H "accept: */*" -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" -d "{ \"amount\": \"10\", \"finalBalance\": \"\", \"fromAccountNumber\": \"1111\", \"toAccountNumber\": \"2222\"}"

{"fromAccountNumber":"1111","toAccountNumber":"2222","amount":"10","finalBalance":"9990.00"}
  • 请注意,finalBalance 字段现在是 9990。
  • 您还可以运行下面的 curl 命令来查看两个帐户的当前余额:
> curl -X GET "http://localhost:8080/services/depositaccount/api/deposit-accounts" -H "accept: */*" -H "Authorization: Bearer $TOKEN"


您将收到以下回复:

[
  {
    "id": 1001,
    "accountNumber": "1111",
    "productId": "DEPOSIT",
    "openingDate": "2020-10-17T12:22:57.494Z",
    "status": 0,
    "balance": 9990
  },
  {
    "id": 1002,
    "accountNumber": "2222",
    "productId": "DEPOSIT",
    "openingDate": "2020-10-17T12:22:57.494Z",
    "status": 0,
    "balance": 10
  }
]


结论


我们从一个简单的架构开始,我们希望从一个微服务向另一个微服务发送加密消息(并接收响应)。

我们探索了 Kafka,也安装了 Kubernetes。我们还探索了 Vault 并尝试了它的功能。

最后,我们创建了 2 个微服务,并将加密消息从一个发送到另一个并接收回复。我们的教程到此结束

完整的应用程序可以在这里访问:https://github.com/azrulhasni/Ebanking-JHipster-Kafka-Vault

原文地址
https://www.linkedin.com/pulse/secure-inter-micro-service-communication-spring-boot-kafka-madisa-4c/
Article
知识星球
 
微信公众号
 
视频号