Spring Cloud

发布时间 2023-11-29 15:00:25作者: 框框A

微服务

1.不同的微服务,不要重复开发相同的业务
2.微服务数据独立,不要访问其它微服务的数据库。
3.微服务可以将自己的业务暴露为接口,供其它微服务调用。
image

实现

开发环境

  • 开发工具:IntelliJ IDEA 2023.2.5 Ultimate
  • 开发框架:Spring boot 3.0.9
  • 语言:Java 21
  • 微服务框架:Spring cloud 2022.0.4
  • 数据库:mysql5.7.4
  • 持久层框架:mybatis3.0.3
  • Java增强工具:lombok 1.18.30

构建工程

1. 创建父工程

使用"Spring Initializr"构建Spring boot项目cloud-service。
删除其他不必要的文件,只留"pom.xml"文件。

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.9</version>
        <relativePath/>
    </parent>

    <packaging>pom</packaging>
    <modules>
        <module>user-service</module>
        <module>order-service</module>
        <module>eureka-server</module>
    </modules>

    <groupId>cn.zyz</groupId>
    <artifactId>cloud-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>cloud-service</name>
    <description>cloud-service</description>

    <properties>
        <java.version>21</java.version>
        <spring-cloud.version>2022.0.4</spring-cloud.version>
        <lombok.version>1.18.30</lombok.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>3.0.3</version>
        </dependency>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter-test</artifactId>
            <version>3.0.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

packaging:表示打包方式为pom
modules:表示包含的子模块
dependencies:父工程中此处的依赖都会被子模块继承
dependencyManagement:表示此处依赖不被子模块继承
groupId:子模块和父工程相同时,子模块中无需指定

2. 用户微服务

用户微服务:user-service

2.1 构建子模块

右击"父工程"->"New Module"->"New Module"

2.2 配置

application.yml
server:
  port: 8081

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/cloud_user?useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: root
  application:
    name: userservice

2.3 实体类

cn.zyz.user.pojo.User
@Data
public class User {
    private int id;
    private String username;
    private String address;
}

2.4 DAO

cn.zyz.user.mapper.UserMapper
@Mapper
public interface UserMapper {
    @Select("select * from tb_user where id=#{id}")
    public User getUser(int id);
}

2.5 服务类

cn.zyz.user.service.UserService
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    public User getUser(int id){
        return userMapper.getUser(id);
    }
}

2.6 控制器类

cn.zyz.user.web
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/{id}")
    public User getUser(@PathVariable int id) {
        User user = userService.getUser(id);
        System.out.println(user);
        return user;
    }
}

3.订单微服务

订单微服务:order-service