• 4.SpringCloud基础项目搭建利用RestTemplate实现远程调用


    简单的微服务架构

    父pom.xml

    1. "1.0" encoding="UTF-8"?>
    2. "http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. org.springframework.boot
    6. spring-boot-starter-parent
    7. 2.3.9.RELEASE
    8. 4.0.0
    9. com.xkj.org
    10. cloud-demo
    11. pom
    12. 1.0-SNAPSHOT
    13. <module>order-servicemodule>
    14. <module>user-servicemodule>
    15. <module>eureka-servermodule>
    16. UTF-8
    17. UTF-8
    18. 1.8
    19. Hoxton.SR10
    20. 5.1.47
    21. 2.1.1
    22. org.springframework.cloud
    23. spring-cloud-dependencies
    24. ${spring-cloud.version}
    25. pom
    26. import
    27. mysql
    28. mysql-connector-java
    29. ${mysql.version}
    30. org.mybatis.spring.boot
    31. mybatis-spring-boot-starter
    32. ${mybatis.version}
    33. org.projectlombok
    34. lombok

    user-service的pom.xml文件

    1. "1.0" encoding="UTF-8"?>
    2. "http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. cloud-demo
    6. com.xkj.org
    7. 1.0-SNAPSHOT
    8. 4.0.0
    9. user-service
    10. pom
    11. org.springframework.boot
    12. spring-boot-starter-web
    13. mysql
    14. mysql-connector-java
    15. org.mybatis.spring.boot
    16. mybatis-spring-boot-starter

    application.yml

    1. server:
    2. port: 8081
    3. spring:
    4. datasource:
    5. url: jdbc:mysql://localhost:3306/cloud_user?useSSL=false
    6. username: root
    7. password: 123456
    8. driver-class-name: com.mysql.jdbc.Driver
    9. application:
    10. name: user-service
    11. mybatis:
    12. type-aliases-package: com.xkj.org.pojo
    13. configuration:
    14. map-underscore-to-camel-case: true
    15. logging:
    16. level:
    17. com.xkj.org: debug
    18. pattern:
    19. dateformat: MM-dd HH:mm:ss:SSS

     order-service的关键配置

    1. "1.0" encoding="UTF-8"?>
    2. "http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. cloud-demo
    6. com.xkj.org
    7. 1.0-SNAPSHOT
    8. 4.0.0
    9. order-service
    10. org.springframework.boot
    11. spring-boot-starter-web
    12. mysql
    13. mysql-connector-java
    14. org.mybatis.spring.boot
    15. mybatis-spring-boot-starter

    order-service的application.yml文件

    1. server:
    2. port: 8080
    3. spring:
    4. datasource:
    5. url: jdbc:mysql://localhost:3306/cloud_order?useSSL=false
    6. username: root
    7. password: 123456
    8. driver-class-name: com.mysql.jdbc.Driver
    9. application:
    10. name: order-service
    11. mybatis:
    12. type-aliases-package: com.xkj.org.pojo
    13. configuration:
    14. map-underscore-to-camel-case: true
    15. logging:
    16. level:
    17. com.xkj.org: debug
    18. pattern:
    19. dateformat: MM-dd HH:mm:ss:SSS

     配置RestTemplate Bean,交给spring容器:

    1. @SpringBootApplication
    2. @MapperScan("com.xkj.org.mapper")
    3. public class OrderApplication {
    4. public static void main(String[] args) {
    5. SpringApplication.run(OrderApplication.class, args);
    6. }
    7. @Bean
    8. public RestTemplate restTemplate() {
    9. return new RestTemplate();
    10. }
    11. }

    远程调用user-service的接口:

    1. @Service
    2. public class OrderServiceImpl implements OrderService {
    3. @Autowired
    4. private OrderMapper orderMapper;
    5. @Autowired
    6. private RestTemplate restTemplate;
    7. @Override
    8. public Order queryOrderById(Long orderId) {
    9. Order order = orderMapper.findById(orderId);
    10. String url = "http://localhost:8081/user/" + order.getUserId();
    11. User user = restTemplate.getForObject(url, User.class);
    12. order.setUser(user);
    13. return order;
    14. }
    15. }
    1. DROP TABLE IF EXISTS `tb_order`;
    2. CREATE TABLE `tb_order` (
    3. `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '订单id',
    4. `user_id` bigint(20) NOT NULL COMMENT '用户id',
    5. `name` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '商品名称',
    6. `price` bigint(20) NOT NULL COMMENT '商品价格',
    7. `num` int(10) NULL DEFAULT 0 COMMENT '商品数量',
    8. PRIMARY KEY (`id`) USING BTREE,
    9. UNIQUE INDEX `username`(`name`) USING BTREE
    10. ) ENGINE = InnoDB AUTO_INCREMENT = 109 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
    11. INSERT INTO `tb_order` VALUES (101, 1, 'Apple 苹果 iPhone 12 ', 699900, 1);
    12. INSERT INTO `tb_order` VALUES (102, 2, '雅迪 yadea 新国标电动车', 209900, 1);
    13. INSERT INTO `tb_order` VALUES (103, 3, '骆驼(CAMEL)休闲运动鞋女', 43900, 1);
    14. INSERT INTO `tb_order` VALUES (104, 4, '小米10 双模5G 骁龙865', 359900, 1);
    15. INSERT INTO `tb_order` VALUES (105, 5, 'OPPO Reno3 Pro 双模5G 视频双防抖', 299900, 1);
    16. INSERT INTO `tb_order` VALUES (106, 6, '美的(Midea) 新能效 冷静星II ', 544900, 1);
    17. INSERT INTO `tb_order` VALUES (107, 2, '西昊/SIHOO 人体工学电脑椅子', 79900, 1);
    18. INSERT INTO `tb_order` VALUES (108, 3, '梵班(FAMDBANN)休闲男鞋', 31900, 1);
    1. DROP TABLE IF EXISTS `tb_user`;
    2. CREATE TABLE `tb_user` (
    3. `id` bigint(20) NOT NULL AUTO_INCREMENT,
    4. `username` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '收件人',
    5. `address` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '地址',
    6. PRIMARY KEY (`id`) USING BTREE,
    7. UNIQUE INDEX `username`(`username`) USING BTREE
    8. ) ENGINE = InnoDB AUTO_INCREMENT = 109 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
    9. INSERT INTO `tb_user` VALUES (1, '柳岩', '湖南省衡阳市');
    10. INSERT INTO `tb_user` VALUES (2, '文二狗', '陕西省西安市');
    11. INSERT INTO `tb_user` VALUES (3, '华沉鱼', '湖北省十堰市');
    12. INSERT INTO `tb_user` VALUES (4, '张必沉', '天津市');
    13. INSERT INTO `tb_user` VALUES (5, '郑爽爽', '辽宁省沈阳市大东区');
    14. INSERT INTO `tb_user` VALUES (6, '范兵兵', '山东省青岛市');
  • 相关阅读:
    猿创征文|vue3+ts封装table组件并注册发布
    必须使用串口擦除Flash后才能继续连接,另外使用串口3进行通讯时,整合后串口负责的ES8266-01S连接阿里云平台的相关代码也处于崩溃状态,请问这两者之间有没有直接联系,如何解决?
    数据库——知识1
    微软真是活菩萨,面向初学者的机器学习、数据科学、AI、LLM课程统统免费
    pink老师 JavaScript基础以及进阶笔记
    NLP自然语言处理学习笔记(九)(转自咕泡AI)
    平凯星辰合伙人余梦杰:全球化和云计算趋势下的开源生态构建
    STEAM上的一款电路模拟神器 — CRUMB Circuit Simulator
    汽车RNC主动降噪算法DSP C程序实现
    工程命令行编译设置方法
  • 原文地址:https://blog.csdn.net/qq_36352889/article/details/138004621