实现两个spring boot项目之间的通讯

希望通过ucp(通讯平台)实现两个spring boot服务(暂时就叫A和B)之间的通讯,A发送消息,B收到以后处理并返回

1、在ucp标准组件库UCP_STD_Library的通讯渠道ZjCloudRabbitMqInput中加入http的jar包,然后编译

2、配置A项目发往ucp的渠道id:ZjCloudRabbitMqInput,编写往ucp渠道发送消息的接口"boot2ucp/send",返回类型为JSONObject

3、ucp平台lot_edge_platform的application.yaml配置rabbltmq

spring:
  application:
    name: UCP
  #配置rabbitMq 服务器
  rabbitmq:
    addresses: 10.34.12.130:5672,10.34.12.130:5673,10.34.12.130:5674
    username: admin
    password: admin
    #虚拟host 可以不设置,使用server默认host
    virtual-host: /test

    listener:
      simple:
        #确认消息模式
        acknowledge-mode: manual
        #负载均衡
        prefetch: 1

server:
  port: 8081

zj:
  rabbitmq:
    exchange: topicExchange
    queueName:
      boot2ucp: topic.boot2ucp
      ucp2boot: topic.ucp2boot

4、可视化平台画通讯渠道ZjCloudRabbitMqInput。http端口为C服务的ip和端口,url为C服务被访问的接口,然后编译UCP_STD_Library

5、C服务被访问的接口"/hello"

6、启动A服务、C服务、ucp的lot_edge_platform,然后浏览器输入http://localhost:8021/swagger-ui.html,A服务发送请求

收到了来自C的返回信息

A服务的执行和接收返回数据过程

lot_edge_platform打印了ZjCloudRabbitMqInput渠道执行的详细信息

结束

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Springboot中,服务接口之间相互调用可以使用RestTemplate或FeignClient。 1. RestTemplate RestTemplate是Spring提供的用于访问Rest服务的客户端。通过RestTemplate,我们可以直接访问其他服务的Rest接口,获取返回结果。 使用RestTemplate,我们需要进行如下步骤: - 创建RestTemplate实例 - 构造请求参数 - 发送请求 - 解析返回结果 示例代码: ``` RestTemplate restTemplate = new RestTemplate(); String url = "http://localhost:8080/user/{id}"; Map<String, String> params = new HashMap<>(); params.put("id", "1"); User user = restTemplate.getForObject(url, User.class, params); ``` 2. FeignClient FeignClient是Spring Cloud提供的一种声明式的HTTP客户端,它简化了服务接口之间的调用。我们只需要定义一个接口,然后在接口上添加注解即可实现调用。 使用FeignClient,我们需要进行如下步骤: - 定义FeignClient接口 - 添加注解 - 调用接口 示例代码: ``` @FeignClient(name = "user-service") public interface UserFeignClient { @GetMapping("/user/{id}") User getUserById(@PathVariable("id") Long id); } @RestController public class UserController { @Autowired private UserFeignClient userFeignClient; @GetMapping("/user/{id}") public User getUserById(@PathVariable("id") Long id) { return userFeignClient.getUserById(id); } } ``` 以上是Springboot服务接口之间相互调用的两种方式,具体使用哪种方式,需要根据项目需求和实际情况来选择。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值