【2】Spring Cloud 工程搭建

  • 🎥 个人主页:Dikz12
  • 🔥个人专栏:Spring Cloud实战
  • 📕格言:吾愚多不敏,而愿加学
  • 欢迎大家👍点赞✍评论⭐收藏

目录

1.声明项目依赖和项目构建插件

2.完善子项目订单服务

2.1完善启动类和配置文件

[2.2 业务代码](#2.2 业务代码)

3.远程调用

3.1需求

[​3.2 实现](#3.2 实现)


1.声明项目依赖和项目构建插件

把下面代码分别引入到两个子项目的pom.xml中.

复制代码
<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.完善子项目订单服务

2.1完善启动类和配置文件

启动类

复制代码
@SpringBootApplication
public class OrderApplication {
    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class,args);
    }
}

配置文件

在resource文件夹中建立,application.yml 文件.

复制代码
server:
  port: 8080
spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/cloud_order?characterEncoding=utf8&useSSL=false
    username:
    password:
    driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
  configuration: # 配置打印 MyBatis 执行的 SQL
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: true  #自动驼峰转换

2.2 业务代码

订单服务:根据订单id,获取订单详情..

1. 先搭架子

2. 实体类.

子项目pom并没有引入lombok,但依然可以使用。@Date

复制代码
@Data
public class OrderInfo {
    private Integer orderId;
    private Integer userId;
    private Integer productId;
    private Integer num;
    private Integer price; //随便了
    private Date createTime;
    private Date updateTime;
}

3.Controller

复制代码
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);
    }
}

4.Service

复制代码
import com.dome.order.mapper.OrderMapper;
import com.dome.order.model.OrderInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderService {
    @Autowired
    private OrderMapper orderMapper;

    public OrderInfo selectOrderById(Integer orderId) {
        return orderMapper.selectOrderById(orderId);
    }
}

5.Mapper

复制代码
import com.dome.order.model.OrderInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface OrderMapper {
    @Select("select  * from order_detail where id = #{orderId}")
    OrderInfo selectOrderById(Integer orderId);
}

测试

启动OrderApplication类.

访问url:http://127.0.0.1:8080/order/1.

完善子项目商品服务,跟上诉过程一样,只需要修改端口号就可以了,因为后⾯需要多个服务⼀起启动,所以要设置为不同的端⼝号。(这里就不在展示了)

3.远程调用

3.1需求

根据订单查询订单信息时,根据订单⾥产品ID,获取产品的详细信息.

3.2 实现

实现思路: order-service服务向product-service服务发送⼀个http请求,把得到的返回结果,和订单结果融合在⼀起,返回给调⽤⽅.
实现⽅式: 采⽤ Spring 提供的 RestTemplate.

实现http请求的⽅式,有很多,可参考:https://zhuanlan.zhihu.com/p/670101467

准备工作

把product实体类添加到order-service中的mode中并在OrderInfo中添加product属性.

1. 定义RestTemplate. (第三方对象需要使用@Bean注解)

复制代码
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class BeanConfig {
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}


2.修改order-service中的OrderService

复制代码
@Service
public 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;
    }
}
  1. 测试.

例 url: http://127.0.0.1:8080/order/1

相关推荐
Code额1 小时前
Python 连接 DeepSeek API,OpenAI 对话方式总结
后端·python·ai·ai编程
星火10241 小时前
【LangChain4j系列10】Guardrails 安全护栏
人工智能·后端
四千岁1 小时前
稀疏向量BM25Retriever不支持中文怎么办?jieba来帮忙
前端·javascript·后端
用户6919026813391 小时前
Docker基本概念
后端·docker·容器
颜进强1 小时前
14 - OpenSpec 老页面改造骨架:定位 + 增量 + 回归三件套
前端·后端·ai编程
唐青枫1 小时前
别只把 switch 当成多路 if:Zig 模式匹配、状态机与 Tagged Union 实战
后端
步行cgn1 小时前
MyBatis 错误 Result Maps collection does not contain value for ... 详解与解决方案
后端
用户250694921611 小时前
Cordis 从入门到实战:插件卸载后,别留下一地鸡毛
后端
SomeB1oody1 小时前
【RustyML入门】5.3. 聚类指标
开发语言·后端·机器学习·rust·教程
IT_陈寒2 小时前
Vite打包给我挖的这个坑,差点搞崩我的项目
前端·人工智能·后端