SpringCloud(一)微服务项目搭建

news/2024/12/22 20:48:24/

一、简介

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.Driverurl: jdbc:mysql://localhost:3306/cloudstudyusername: rootpassword: root

User


@Data
public class User {private Integer uid;private String name;private String sex;
}

UserController


@RequestMapping("/user")
@RestController
public class UserController {@Resourceprivate 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 {@Resourceprivate UserMapper userMapper;@Overridepublic 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 {@Resourceprivate 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 {@Resourceprivate BorrowMapper borrowMapper;@Overridepublic 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);}


http://www.ppmy.cn/news/924074.html

相关文章

Appium 全新 2.0 全新跨平台生态,版本特性抢鲜体验!

关于Appium V2 Appium V2 beta版本在2021年发布&#xff0c;从2022年1月1号开始&#xff0c;Appium核心团队不会再维护Appium 1.x版本了&#xff0c;所有近期官方发布的平台驱动&#xff08;如Android平台的UIAutomator&#xff0c;IOS平台的XCUITest&#xff09;不再兼容Appi…

求先序排列

题目描述 给出一棵二叉树的中序与后序排列。求出它的先序排列。&#xff08;约定树结点用不同的大写字母表示&#xff0c;且二叉树的节点个数≤8&#xff09;。 输入格式 共两行&#xff0c;均为大写字母组成的字符串&#xff0c;表示一棵二叉树的中序与后序排列。 输出格式…

消息队列——spring和springboot整合rabbitmq

目录 spring整合rabbitmq——生产者 rabbitmq配置文件信息 倒入生产者工程的相关代码 简单工作模式 spring整合rabbitmq——消费者 spring整合rabbitmq——配置详解 SpringBoot整合RabbitMQ——生产者 SpringBoot整合RabbitMQ——消费者 spring整合rabbitmq——生产者 使…

自助柜员机属于微型计算机吗,计算机800道题.docx

计算机800道题 计算机题库总线是连接计算机内部多个功能部件的一组私有的信息通路。( 2 ) 1.正确 2.错误总线是连接计算机内部多个功能部件的一组公共信息通路。( 1)1.正确 2.错误自动柜员机属于微型计算机的一种 。( 2 )1.正确 2.错误主页是指网站上显示主要内容的页面。…

彻悟人生555句金玉良言

1、成功一定有方法&#xff0c;失败一定有原因。 2、待人三要素&#xff1a;团结能人做大事&#xff0c;团结好人做实事&#xff0c;团结坏人不坏事。 3、进步三要素&#xff1a;你自己行&#xff0c;有人说你行&#xff0c;说你行的人行。 4、说话三要素&#xff1a;该说时…

让你彻悟人生的555句金玉良言

1、成功一定有方法&#xff0c;失败一定有原因。   2、待人三要素&#xff1a;团结能人做大事&#xff0c;团结好人做实事&#xff0c;团结坏人不坏事。   3、进步三要素&#xff1a;你自己行&#xff0c;有人说你行&#xff0c;说你行的人行。   4、说话三要素&#xff…

Maven项目之一号店——注册与登录

Maven项目之一号店——注册与登录 声明&#xff1a;该项目为大型项目&#xff0c;所有代码非原创&#xff0c;用于自学以及与网友探讨学习所用&#xff0c;以下文章为自学时整理&#xff0c;请勿用于商业用途。 前端制作为个人私有&#xff0c;仅提供包结构和前端框架作为参考…

民谣与2017年终总结

东风摇众木&#xff0c;即有看花期。 之所以写这个标题&#xff0c;是因为我最近觉得&#xff0c;在生活中&#xff0c;我们一定要积极向上的面向阳光面向正能量的姿态对待明天。 作为一个伪文青&#xff0c;其实很早就知道《米店》&#xff0c;真正被感染是老狼在《歌手》上的…