1. Nacos 配置

在 Nacos 中创建配置(Data ID: node-red-apis.yaml):

# 第三方API配置
apis:
  # 规则引擎服务接口
  node-red-service:
    base-url: "http://127.0.0.1:8080/api"
    endpoints:
      alert: "/quality/alert"

2. 配置映射类

@Component
@ConfigurationProperties(prefix = "apis.node-red-service")
public class NodeRedConfig {
    private String baseUrl;
    private Map<String, String> endpoints;
    
    // 获取完整接口地址
    public String getEndpointUrl(String endpointName) {
        return baseUrl + endpoints.get(endpointName);
    }
    
    // Getter 和 Setter
    public String getBaseUrl() {
        return baseUrl;
    }
    
    public void setBaseUrl(String baseUrl) {
        this.baseUrl = baseUrl;
    }
    
    public Map<String, String> getEndpoints() {
        return endpoints;
    }
    
    public void setEndpoints(Map<String, String> endpoints) {
        this.endpoints = endpoints;
    }
}

3. 服务类实现

@Service
public class NodeRedService {
    
    @Autowired
    private NodeRedConfig nodeRedConfig;
    
    private final RestTemplate restTemplate = new RestTemplate();
    
    // 发送质量告警
    public String sendQualityAlert(Object alertData) {
        String url = nodeRedConfig.getEndpointUrl("alert");
        
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        
        HttpEntity<Object> request = new HttpEntity<>(alertData, headers);
        
        try {
            ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
            return response.getBody();
        } catch (Exception e) {
            // 处理异常
            return "Error calling Node-RED API: " + e.getMessage();
        }
    }
    
    // 直接获取配置的接口地址(用于调试或其他用途)
    public String getAlertEndpoint() {
        return nodeRedConfig.getEndpointUrl("alert");
    }
}

4. 控制器示例

@RestController
@RequestMapping("/api/quality")
public class QualityController {
    
    @Autowired
    private NodeRedService nodeRedService;
    
    @PostMapping("/alert")
    public String sendAlert(@RequestBody QualityAlert alert) {
        return nodeRedService.sendQualityAlert(alert);
    }
    
    @GetMapping("/endpoint")
    public String getEndpoint() {
        return nodeRedService.getAlertEndpoint();
    }
}

5. 启用配置属性

在启动类上添加注解:

@SpringBootApplication
@EnableConfigurationProperties(NodeRedConfig.class)
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

6. 使用示例

  1. 获取配置的接口地址​:

    GET /api/quality/endpoint

    返回:http://127.0.0.1:8080/api/quality/alert

  2. 发送质量告警​:

    POST /api/quality/alert
    Content-Type: application/json
    
    {
      "deviceId": "sensor-001",
      "qualityIssue": "temperature_high",
      "value": 45.6,
      "timestamp": "2025-08-15T10:30:00Z"
    }

7. 简化配置(可选)

如果您只需要一个接口,可以进一步简化配置:

在 Nacos 中创建配置(Data ID: node-red-alert-api.yaml):

# Node-RED 告警接口配置
node-red:
  alert:
    url: "http://192.168.11.1:1880/api/quality/alert"

对应的配置类:

@Component
@ConfigurationProperties(prefix = "node-red.alert")
public class NodeRedAlertConfig {
    private String url;
    
    public String getUrl() {
        return url;
    }
    
    public void setUrl(String url) {
        this.url = url;
    }
}

服务类:

@Service
public class NodeRedAlertService {
    
    @Autowired
    private NodeRedAlertConfig nodeRedAlertConfig;
    
    private final RestTemplate restTemplate = new RestTemplate();
    
    public String sendAlert(Object alertData) {
        String url = nodeRedAlertConfig.getUrl();
        
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        
        HttpEntity<Object> request = new HttpEntity<>(alertData, headers);
        
        try {
            ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
            return response.getBody();
        } catch (Exception e) {
            return "Error calling Node-RED alert API: " + e.getMessage();
        }
    }
}

实现方案:

  1. 从 Nacos 获取配置的接口地址

  2. 使用 RestTemplate 调用该接口

  3. 提供了简单的控制器端点来测试和使用

Logo

中国智能体开发者社区,聚焦智能体与大模型开发,提供前沿资讯、实用工具链、开源项目及行业案例。通过技术沙龙、开发者大赛等活动,促进经验交流与协作,助力开发者快速构建创新智能应用。

更多推荐