SpringCloud(一)微服务项目搭建

本文介绍了如何使用SpringCloud构建微服务项目,从创建父项目和子项目开始,详细展示了SpringBoot应用的配置,包括依赖管理、数据库连接以及MyBatis的集成。接着,文章提到了通过RESTfulAPI实现跨服务的数据查询,展示了Borrow服务如何关联查询User和Book服务的数据。
摘要由CSDN通过智能技术生成

一、简介

SpringCloud是Spring提供的一套分布式解决方案,集合了一些大型互联网公司的开源产品,包括诸多组件,共同组成SpringCloud框架。并且,它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置中心、消息总线、负载均衡、熔断机制、数据监控等,都可以用Spring Boot的开发风格做到一键启动和部署。


二、微服务项目搭建

官方文档:https://docs.spring.io/spring-cloud-netflix/docs/current/reference/html/

1.创建父项目

 2.创建子项目

3.在子项目中创建SpringBoot的启动主类

父类:

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>2.6.7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>SpringCloudStudy</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>SpringCloudStudy</name>
    <description>SpringCloudStudy</description>
    <modules>
        <module>user-service</module>
        <module>book-service</module>
        <module>borrow-service</module>
    </modules>
    <properties>
        <java.version>8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.28</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.28</version>
        </dependency>
    </dependencies>

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

</project>

Book:


@SpringBootApplication
public class BookApplication {
    public static void main(String[] args) {
        SpringApplication.run(BookApplication.class, args);
    }
}

pom.xml

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.example</groupId>
        <artifactId>SpringCloudStudy</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>book-service</artifactId>
    <packaging>jar</packaging>

    <name>book-service</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>
</project>

 Borrow


@SpringBootApplication
public class BorrowApplication {
    public static void main(String[] args) {
        SpringApplication.run(BorrowApplication.class, args);
    }
}

pom.xml

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.example</groupId>
        <artifactId>SpringCloudStudy</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>borrow-service</artifactId>
    <packaging>jar</packaging>

    <name>borrow-service</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.target>8</maven.compiler.target>
        <maven.compiler.source>8</maven.compiler.source>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>



    </dependencies>
</project>

 User

@SpringBootApplication
public class UserApplication{
    public static void main(String[] args) {
        SpringApplication.run(UserApplication.class,args);
    }
}

pom.xml

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.example</groupId>
        <artifactId>SpringCloudStudy</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>user-service</artifactId>
    <packaging>jar</packaging>

    <name>user-service</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>
</project>

4.构建数据库

添加外键 

 添加索引

 父级pom.xml添加依赖

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.2.0</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

子类导入

<dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>

子类application.yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/cloudstudy
    username: root
    password: root

User


@Data
public class User {

    private Integer uid;

    private String name;

    private String sex;
}

UserController


@RequestMapping("/user")
@RestController
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("/getUserById/{uid}")
    public User getUserById(@PathVariable("uid") Integer uid){
        return userService.getUserById(uid);
    }
}

UserService


public interface UserService {

    User getUserById(Integer uid);
}

UserServiceImpl


@Service
public class UserServiceImpl implements UserService {


    @Resource
    private UserMapper userMapper;

    @Override
    public User getUserById(Integer uid) {
        return userMapper.getUserById(uid);
    }
}

UserMapper


@Mapper
public interface UserMapper {
    @Select("select * from db_user where uid = #{uid}")
    User getUserById(Integer uid);
}

Book同上

Borrow(关联查询)

新建通用模块

 子类添加依赖

pom.xml

 <dependency>
            <groupId>com.example</groupId>
            <artifactId>commons</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

Borrow

import lombok.Data;

@Data
public class Borrow {

    private Integer id;

    private Integer uid;

    private Integer bid;
}

UserBorrowDto

import com.example.entity.Book;
import com.example.entity.User;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.List;


@Data
@AllArgsConstructor
public class UserBorrowDto {

    private User user;

    private List<Book> bookList;
}

BorrowController


@RestController
@RequestMapping("/borrow")
public class BorrowController {

    @Resource
    private BorrowService borrowService;

    @GetMapping("/getBorrowById/{uid}")
    public UserBorrowDto getBorrowById(@PathVariable("uid") Integer uid){
        return borrowService.getUserBorrowDtoByUid(uid);
    }
}

BorrowService


public interface BorrowService {

    UserBorrowDto getUserBorrowDtoByUid(Integer uid);
}

BorrowServiceImpl

@Service
public class BorrowServiceImpl implements BorrowService {

    @Resource
    private BorrowMapper borrowMapper;

    @Override
    public UserBorrowDto getUserBorrowDtoByUid(Integer uid) {
        List<Borrow> borrow = borrowMapper.getBorrowByUid(uid);
        //RestTemplate支持多种方式远程调用
        RestTemplate template = new RestTemplate();
        //这里通过调用getForObject来请求其他服务,并将结果自动进行封装
        //获取User信息
        User user = template.getForObject("http://localhost:8101/user/getUserById/" + uid, User.class);
        //获取每一本书的详情信息
        List<Book> bookList = borrow
                .stream()
                .map(b -> template.getForObject("http://localhost:8201/book/getBookById/" + b.getId(), Book.class))
                .collect(Collectors.toList());
        return new UserBorrowDto(user,bookList);
    }
}

BorrowMapper


@Mapper
public interface BorrowMapper {

    @Select("select * from db_borrow where bid = #{bid} and uid = #{uid}")
    Borrow getBorrow(Integer uid, Integer bid);

    @Select("select * from db_borrow where uid = #{uid}")
    List<Borrow> getBorrowByUid(Integer uid);

    @Select("select * from db_borrow where bid = #{bid}")
    List<Borrow> getBorrowByBid(Integer bid);

}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值