【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

相关推荐
有梦想的攻城狮13 分钟前
spring中的@MapperScan注解详解
java·后端·spring·mapperscan
柚个朵朵1 小时前
Spring的Validation,这是一套基于注解的权限校验框架
java·后端·spring
程序员小杰@1 小时前
【MCP教程系列】SpringBoot 搭建基于 Spring AI 的 SSE 模式 MCP 服务
人工智能·spring boot·spring
程序员buddha2 小时前
Spring & Spring Boot 常用注解整理
java·spring boot·spring
Asus.Blogs2 小时前
为什么go语言中返回的指针类型,不需要用*取值(解引用),就可以直接赋值呢?
开发语言·后端·golang
C_V_Better2 小时前
Java Spring Boot 控制器中处理用户数据详解
java·开发语言·spring boot·后端·spring
胡子洲2 小时前
Spring Boot 应用中实现基本的 SSE 功能
java·spring boot·后端
贰拾wan3 小时前
【Java-EE进阶】SpringBoot针对某个IP限流问题
java·spring boot·后端·idea
Paran-ia3 小时前
【2025版】Spring Boot面试题
java·spring boot·后端
LUCIAZZZ3 小时前
JVM之虚拟机运行
java·jvm·spring·操作系统·springboot