Cumulocity微服务部署访问被拒,Hello微服务教程无法运行求助
Hey there! Let's work through your Cumulocity Java microservice challenge step by step—first fixing that deployment access issue, then building out your data decoding and measurement upload functionality.
Access issues during deployment usually boil down to permissions, configuration, or connectivity. Here’s what to check:
- Verify User Permissions: Make sure the user you’re deploying with has the right roles. Head to Cumulocity’s
Administration > Rolesand confirm your role includesROLE_MICROSERVICE_MANAGEMENT_ADMIN(full microservice control) or at leastROLE_MICROSERVICE_MANAGEMENT_READ+ROLE_MICROSERVICE_MANAGEMENT_CREATE. - Double-Check Bootstrap Credentials: When you created your microservice app in Cumulocity, you generated bootstrap credentials. Ensure your
cumulocity.jsonfile has the exactapplicationNamematching the app you created, and that the tenant, username, and password in your deployment config are correct. - Test Network Connectivity: If deploying from your local machine, confirm you can reach your Cumulocity tenant without firewall/proxy blocks. Run a quick test like
curl https://myTenant.cumulocity.com/tenant/currentTenant—if this fails, your network is the culprit. - Dig Into Deployment Logs: Cumulocity logs tell you exactly what’s broken. After a failed deployment, go to
Administration > Microservices, select your app, and check the logs tab for specific errors (e.g., invalid credentials, missing permissions).
Assuming you’re using the Cumulocity Java SDK, here’s how to build out your desired functionality:
1. Set Up Your Project
Start with the Cumulocity Maven archetype to get a pre-configured microservice skeleton (or adapt the Hello tutorial project):
mvn archetype:generate -DarchetypeGroupId=com.cumulocity.sdk -DarchetypeArtifactId=c8y-microservice-archetype -DarchetypeVersion=10.18.0
Fill in your groupId, artifactId, and make sure the applicationName matches what you created in Cumulocity.
2. Add the Decode Endpoint
Create a REST controller to handle the /decode request, split the input data, and upload measurements:
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.cumulocity.sdk.client.measurement.MeasurementApi; import com.cumulocity.model.measurement.MeasurementValue; import com.cumulocity.model.measurement.Measurement; import java.util.ArrayList; import java.util.List; @RestController public class DecodeController { private final MeasurementApi measurementApi; // Inject Cumulocity's MeasurementApi (provided by the SDK) public DecodeController(MeasurementApi measurementApi) { this.measurementApi = measurementApi; } @GetMapping("/decode") public String decodeAndUpload(@RequestParam String data) { // Split input into 2-character chunks List<String> measurementChunks = splitDataIntoChunks(data, 2); if (measurementChunks.isEmpty()) { return "Error: Input data must be an even-length string (e.g., 112233)"; } // Create and upload each measurement to Cumulocity for (int i = 0; i < measurementChunks.size(); i++) { Measurement measurement = new Measurement(); // Replace with your target device ID (or add a deviceId param to the request for flexibility) measurement.setSource(new com.cumulocity.model.idtype.GId("YOUR_DEVICE_ID")); measurement.setType("CustomDecodedMeasurement"); measurement.setDateTime(new org.joda.time.DateTime()); // Add the decoded value (e.g., "measurement_1" for the first chunk) double value = Double.parseDouble(measurementChunks.get(i)); measurement.getValues().put("measurement_" + (i + 1), new MeasurementValue(value)); // Upload to Cumulocity measurementApi.create(measurement); } return "Success! Processed and uploaded " + measurementChunks.size() + " measurements."; } private List<String> splitDataIntoChunks(String data, int chunkSize) { List<String> chunks = new ArrayList<>(); for (int i = 0; i < data.length(); i += chunkSize) { chunks.add(data.substring(i, Math.min(i + chunkSize, data.length()))); } return chunks; } }
3. Configure Required Permissions
Update your cumulocity.json to include permission for creating measurements:
{ "apiVersion": "1", "applicationName": "my-application-name", "requiredRoles": [ "ROLE_MEASUREMENT_CREATE" ], "roles": [] }
4. Build and Deploy
- Package your project:
mvn clean package - Deploy using the Cumulocity CLI (first log in with your tenant credentials):
c8y login --tenant myTenant --username yourUsername --password yourPassword c8y microservices create --file target/your-artifact-id.jar
5. Test the Endpoint
Once deployed, test with:
curl "https://myTenant.cumulocity.com/service/my-application-name/decode?data=112233"
Then check Cumulocity’s Device Management > Measurements to confirm the new measurements show up under your target device.
If you’re stuck on the Hello tutorial, try these fixes:
- Check Maven Dependencies: Ensure your
pom.xmlincludes the Cumulocity public repository to pull SDK dependencies:<repositories> <repository> <id>cumulocity-public</id> <url>https://repo.cumulocity.com/public</url> </repository> </repositories> - Local Testing Configuration: When running locally, set bootstrap credentials via environment variables (
C8Y_BOOTSTRAP_TENANT,C8Y_BOOTSTRAP_USER,C8Y_BOOTSTRAP_PASSWORD) or add them toapplication.properties:c8y.bootstrap.tenant=myTenant c8y.bootstrap.user=yourBootstrapUser c8y.bootstrap.password=yourBootstrapPass - Clean and Rebuild: Sometimes stale builds cause issues—run
mvn clean installto start fresh.
内容的提问来源于stack exchange,提问作者Timothy Van Wichelen

