简体   繁体   English

如何发布/订阅 AWS IoT - 服务器端使用 Java SDK

[英]How do I publish/Subscribe AWS IoT - server side using Java SDK

I'm really new with AWS and IoT, and my goal is to:我是 AWS 和 IoT 的新手,我的目标是:

  1. Use the Java SDK v.2 from my serverless application to create/get/update/attach/... certificates and things.使用我的无服务器应用程序中的 Java SDK v.2 创建/获取/更新/附加/...证书和其他东西。

  2. Create client side MQTT demo application to connect publish and subscribe to messages used by my new certificates and thing created in phase 1.创建客户端 MQTT 演示应用程序以连接发布和订阅由我的新证书和在第 1 阶段创建的事物使用的消息。

  3. Publish/subscribe messages in the server side in order to talk to my things/clients.在服务器端发布/订阅消息以便与我的东西/客户交谈。

1 & 2 I've managed to do perfectly. 1 & 2 我做得很完美。 But I don't understand how should I do the 3rd one.但是我不明白我应该怎么做第三个。

  • Should I use the IoT device SDK as well in the server side?我是否也应该在服务器端使用 IoT 设备 SDK? If so with what credentials do I connect?如果是这样,我连接什么凭据?
  • Is there some objects in the SDK that I've missed?我错过了 SDK 中的某些对象吗?

In order to connect to IoT Core from the server I first configure my SSO connection using the AWSCLI and in the code I simply use my profile name and region to connect.为了从服务器连接到 IoT Core,我首先使用 AWSCLI 配置我的 SSO 连接,在代码中我只使用我的配置文件名称和区域进行连接。

Your serverless Java application needs to be configured as a "Thing" in the same account/region as your IoT devices.您的无服务器 Java 应用程序需要在与您的 IoT 设备相同的帐户/区域中配置为“事物”。 In the console, go to在控制台中,go 到

AWS IoT -> Manage -> Things AWS IoT -> 管理 -> 事物

and create a thing for your app.并为您的应用创建一个东西。 In this case you shouldn't need a "Device Shadow", and you can select "Auto Generate Certificates".在这种情况下,您不需要“设备影子”,您可以 select “自动生成证书”。

For the IoT Policy, you will need the following:对于物联网政策,您将需要以下内容:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "iot:Connect",
      "Resource": "arn:aws:iot:us-east-1:YOUR_AWS_ACCOUNT_ID:client/*"
    },
    {
      "Effect": "Allow",
      "Action": "iot:Subscribe",
      "Resource": "arn:aws:iot:us-east-1:YOUR_AWS_ACCOUNT_ID:topicfilter/*"
    },
    {
      "Effect": "Allow",
      "Action": "iot:Receive",
      "Resource": "arn:aws:iot:us-east-1:YOUR_AWS_ACCOUNT_ID:topic/*"
    },
    {
      "Effect": "Allow",
      "Action": "iot:Publish",
      "Resource": "arn:aws:iot:us-east-1:YOUR_AWS_ACCOUNT_ID:topic/*"
    }
  ]
}

Your application will communicate with IoTCore using the endpoint shown in the Settings screen in IoTCore for the region where you have created your thing.您的应用程序将使用您创建事物所在区域的 IoTCore 设置屏幕中显示的端点与 IoTCore 通信。 Your application will authenticate using the key/cert you downloaded when creating the thing (username/password auth is not allowed).您的应用程序将使用您在创建事物时下载的密钥/证书进行身份验证(不允许使用用户名/密码身份验证)。

Once your application connects to the endpoint, you will want to "subscribe" to the same topic your devices use to send messages.一旦您的应用程序连接到端点,您将希望“订阅”您的设备用于发送消息的同一主题。 You can also publish to one or more topics.您还可以发布到一个或多个主题。

In order to debug communications, you can use the MQTT client in the AWS IoTCore console, just note the console needs to be refreshed periodically when communication times out.为了调试通信,您可以在 AWS IoTCore 控制台中使用 MQTT 客户端,请注意当通信超时时需要定期刷新控制台。 I recommend marking your topics as favorites so they are easy to re-subscribe to on a refresh.我建议将您的主题标记为收藏夹,以便在刷新时轻松重新订阅。

As for coding in Java, you should be able to leverage examples from the AWS IoT Device SDK here:至于 Java 中的编码,您应该能够在此处利用 AWS IoT 设备 SDK 中的示例:

https://github.com/aws/aws-iot-device-sdk-java-v2/tree/main/samples https://github.com/aws/aws-iot-device-sdk-java-v2/tree/main/samples

Here's a link to the MQTT client class:下面是 MQTT 客户端 class 的链接:

http://aws-iot-device-sdk-java-docs.s3-website-us-east-1.amazonaws.com/com/amazonaws/services/iot/client/AWSIotMqttClient.html http://aws-iot-device-sdk-java-docs.s3-website-us-east-1.amazonaws.com/com/amazonaws/services/iot/client/AWSIotMqttClient.html

Please note that your app will not have access to messages when not in use.请注意,您的应用在不使用时将无法访问消息。 There are a few strategies to deal with message persistence, but that's outside the scope of your question, so I won't cover it here.有一些策略可以处理消息持久性,但这不在您问题的 scope 范围内,因此我不会在这里介绍。

Hopefully this gets you pointed in the right direction.希望这能让你指出正确的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AWS IoT 使用 SDK 发布保留消息 - AWS IoT Publish a retained message using SDK Eclipse Paho MQTT Golang 发布订阅 AWS IOT 在同一台机器上作为单独进程运行时返回 EOF 错误 - Eclipse Paho MQTT Golang publish subscribe to AWS IOT returns EOF error when running as separate process on same machine 如何使用 Dataflow 更新 IoT 设备配置(在 Cloud IoT Core 中)? - How do I update IoT device config (in Cloud IoT Core), using Dataflow? AWS JS SDK:如何使用根 IAM 账户从子账户/其他角色访问 GameLift 数据? - AWS JS SDK: How do I access GameLift data from a subaccount / another role using the root IAM account? 如何构建和运行 aws-iot-device-sdk-embedded-C 示例? - How build and run aws-iot-device-sdk-embedded-C sample? 如何在 Java AWS SDK 中设置基本 url? - How to set base url in Java AWS SDK? AWS JAVA SDK - 我可以使用在同一 aws 帐户内运行的 aws sdk 以编程方式访问其他服务而无需硬编码凭据吗? - AWS JAVA SDK - Can i programmatically access other services using aws sdk running inside same aws account without hard coding credentials? 如何使用aws-java-sdk从S3逐块读取文件 - How to read file chunk by chunk from S3 using aws-java-sdk AWS GoLang SDK 版本 2 和物联网核心队列配置 - AWS GoLang SDK Version 2 and iot core Fleet Provisioning 如何使用 AWS SDK 部署特定堆栈? - How to deploy a specific stack using AWS SDK?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM