所以设置为不同的端⼝号
发布时间:2025-06-24 20:01:52 作者:北方职教升学中心 阅读量:150
RestTemplate。
采⽤ Spring 提供的。3.2 实现。所以设置为不同的端⼝号。
2.2 业务代码。
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> </dependency> <!--mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency></dependencies><build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>**/**</include> </includes> </resource> </resources></build>
2.完善子项目订单服务 。
测试。并在orderinfo中添加product属性.。
欢迎大家点赞✍评论⭐收藏。 2.1完善启动和配置文件。
在resource文件夹中建立, 实现思路: 。
🔥个人专栏:Spring 实战Cloud。 3.2 实现。
2.2 业务代码
订单服务:根据订单id获取订单详细信息.. 。
,与上诉过程相同只需修改端口号,因为后⾯需要多种服务⼀启动,返回给调⽤⽅.。1.声明项目依赖性和项目构建插件。
子项目pom没有引入lombok,但还是可以用的。(这里就不展示)了;
3.远程调用。
.。3. 测试.。
3.1需求 。3. 测试.。
1. 先搭架子。
目录。可以参考:https://zhuanlan.zhihu.com/p/670101467。
📕格言:吾愚多不敏感而愿加学。 2.1完善启动和配置文件。根据订单⾥产品ID,
将以下代码引入两个子项目的pom.xml中. 。
2.2 业务代码。
访问url:http://127.0.0.1:8080/order/1.。
准备工作。application.yml 文件.。
2.1完善启动类和配置文件
启动类 。
完善子项目商品服务。
1.声明项目依赖性和项目构建插件。
将product实体类添加到orderservice中的mode中,配置文件。
。
2.完善子项目订单服务 。3.1需求 。
4.Service。
server: port: 8080spring: datasource: url: jdbc:mysql://127.0.0.1:3306/cloud_order?characterEncoding=utf8&useSSL=false username: password: driver-class-name: com.mysql.cj.jdbc.Drivermybatis: configuration: # 配置打印 MyBatis 执行的 SQL log-impl: org.apache.ibatis.logging.stdout.StdOutImpl map-underscore-to-camel-case: true #驼峰自动转换。@Servicepublic class OrderService { @Autowired private OrderMapper orderMapper; @Autowired private RestTemplate restTemplate; public OrderInfo selectOrderById(Integer orderId) { OrderInfo orderInfo = orderMapper.selectOrderById(orderId); String url = "http://127.0.0.1:9090/product/" + orderInfo.getProductId(); ProductInfo productInfo = restTemplate.getForObject(url, ProductInfo.class); orderInfo.setProductInfo(productInfo); return orderInfo; }}。
2. 实体类. 。
实现⽅式: 。
实现http请求⽅有很多类型,5.Mapper。例 url: http://127.0.0.1:8080/order/1。
import com.dome.order.mapper.OrderMapper;import com.dome.order.model.OrderInfo;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;@Servicepublic class OrderService { @Autowired private OrderMapper orderMapper; public OrderInfo selectOrderById(Integer orderId) { return orderMapper.selectOrderById(orderId); }}。order-service服务向product-service服务发送⼀http请求将返回结果与订单结果相结合⼀起,
- 🎥 个人主页:Dikz12。
测试。@Date。
@SpringBootApplicationpublic class OrderApplication { public static void main(String[] args) { SpringApplication.run(OrderApplication.class,args); }}。
2.修改order-service中的orderservice。
根据订单查询订单信息时,import com.dome.order.model.OrderInfo;import com.dome.order.service.OrderService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/order")public class OrderController { @Autowired private OrderService orderService; @RequestMapping("/{orderId}") public OrderInfo getOrderById(@PathVariable("orderId") Integer orderId) { ////从url中获取参数 return orderService.selectOrderById(orderId); }}。
配置文件。
Orderaplication启动. 。
1. Resttemplatet定义. (第三方需要使用ʄBean注释)
import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.client.RestTemplate;@Configurationpublic class BeanConfig { @Bean public RestTemplate restTemplate() { return new RestTemplate(); }}。获取产品详细信息.。 。
3.Controller。3.远程调用。
@Datapublic class OrderInfo { private Integer orderId; private Integer userId; private Integer productId; private Integer num; private Integer price; //随便了 private Date createTime; private Date updateTime;}。
import com.dome.order.model.OrderInfo;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Select;@Mapperpublic interface OrderMapper { @Select("select * from order_detail where id = #{orderId}") OrderInfo selectOrderById(Integer orderId);}。