深入讲解Spring Boot和Spring Cloud,外加图书管理系统实战!
很抱歉,我的疏忽,说了这么久还没有给大家详细讲解过Spring Boot和Spring Cloud,那今天给大家详细讲解一下。
大家可以和下面这三篇博客一起看:
1、Spring Boot 和 Spring Cloud 微服务开发实践详解https://blog.csdn.net/speaking_me/article/details/143917383?spm=1001.2014.3001.5502
2、Spring Boot 和 Spring Cloud 构建一个完整的微服务架构——在线购物系统
https://blog.csdn.net/speaking_me/article/details/143918281?spm=1001.2014.3001.5502
3、当今最热门的微服务框架搭建讲解和微服务开发实战之点餐管理系统实战,内附源代码详细讲解!
https://blog.csdn.net/speaking_me/article/details/144005596?spm=1001.2014.3001.5502
Spring Boot
1. 核心概念
Spring Boot 是一个用于创建独立的、生产级的Spring应用程序的框架。它旨在简化新Spring应用的初始搭建以及开发过程。Spring Boot的主要目标是减少开发时间和配置工作,使开发者能够更快地构建应用。
2. 主要功能
- 自动配置: Spring Boot会根据添加的依赖自动配置Spring应用。例如,如果你添加了Spring MVC依赖,Spring Boot会自动配置一个嵌入式的Tomcat服务器。
 - 独立运行: Spring Boot应用可以被打包成一个可执行的JAR或WAR文件,不需要外部的Servlet容器。
 - 内嵌服务器: 默认支持内嵌的Tomcat、Jetty和Undertow服务器。
 - 简化配置: 提供了默认的配置选项,可以通过属性文件轻松覆盖这些默认值。
 - 健康检查和监控: 内置了Actuator模块,提供了应用的健康检查、监控等端点。
 - 起步依赖: 提供了一系列的“起步依赖”(Starter Dependencies),简化了依赖管理。
 
3. 示例项目
下面是一个简单的Spring Boot应用示例:
3.1 创建项目
使用Spring Initializr创建一个新的Spring Boot项目,选择以下依赖:
- Spring Web
 - Spring Data JPA
 - MySQL Driver
 - Spring Boot DevTools
 
3.2 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><groupId>com.example</groupId><artifactId>demo</artifactId><version>1.0.0-SNAPSHOT</version><packaging>jar</packaging><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.5</version><relativePath/> <!-- lookup parent from repository --></parent><properties><java.version>11</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build> </project>
3.3 application.properties
 
spring.datasource.url=jdbc:mysql://localhost:3306/demo?useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=root spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true
3.4 实体类 User.java
 
package com.example.demo.entity;import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id;@Entity public class User {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private String name;private String email;// Getters and Setters }
3.5 仓库接口 UserRepository.java
 
package com.example.demo.repository;import com.example.demo.entity.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository;@Repository public interface UserRepository extends JpaRepository<User, Long> { }
3.6 服务类 UserService.java
 
package com.example.demo.service;import com.example.demo.entity.User; import com.example.demo.repository.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;import java.util.List; import java.util.Optional;@Service public class UserService {@Autowiredprivate UserRepository userRepository;public List<User> getAllUsers() {return userRepository.findAll();}public Optional<User> getUserById(Long id) {return userRepository.findById(id);}public User saveUser(User user) {return userRepository.save(user);}public void deleteUser(Long id) {userRepository.deleteById(id);} }
3.7 控制器 UserController.java
 
package com.example.demo.controller;import com.example.demo.entity.User; import com.example.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*;import java.util.List; import java.util.Optional;@RestController @RequestMapping("/users") public class UserController {@Autowiredprivate UserService userService;@GetMappingpublic List<User> getAllUsers() {return userService.getAllUsers();}@GetMapping("/{id}")public Optional<User> getUserById(@PathVariable Long id) {return userService.getUserById(id);}@PostMappingpublic User saveUser(@RequestBody User user) {return userService.saveUser(user);}@DeleteMapping("/{id}")public void deleteUser(@PathVariable Long id) {userService.deleteUser(id);} }
3.8 启动类 DemoApplication.java
 
package com.example.demo;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);} }
Spring Cloud
1. 核心概念
Spring Cloud 是一个基于Spring Boot构建的微服务框架,提供了一整套微服务解决方案,包括服务注册与发现、配置管理、断路器、智能路由、微代理、控制总线、一次性令牌、全局锁、领导选举、分布式ID、集群状态管理等。
2. 主要功能
- 服务发现: 使用Eureka、Consul或Zookeeper等组件进行服务注册和发现。
 - 配置管理: 使用Config Server集中管理配置文件。
 - 断路器: 使用Hystrix实现断路器模式,防止服务雪崩。
 - API网关: 使用Zuul或Spring Cloud Gateway作为API网关,统一处理外部请求。
 - 消息总线: 使用Spring Cloud Bus实现消息驱动的应用程序。
 - 分布式跟踪: 使用Sleuth和Zipkin进行分布式跟踪。
 
3. 示例项目
下面是一个简单的Spring Cloud项目示例,包括服务发现、配置管理和API网关。
3.1 服务发现(Eureka)
3.1.1 创建 discovery-service 模块
 
在父项目的 modules 目录下创建 discovery-service 模块。
3.1.2 discovery-service 的 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>book-management-system</artifactId><version>1.0.0-SNAPSHOT</version></parent><artifactId>discovery-service</artifactId><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency></dependencies> </project>
3.1.3 discovery-service 的 application.yml
 
server:port: 8761eureka:instance:hostname: localhostclient:register-with-eureka: falsefetch-registry: falseservice-url:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
3.1.4 启动类 DiscoveryServiceApplication.java
 
package com.example.discoveryservice;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication @EnableEurekaServer public class DiscoveryServiceApplication {public static void main(String[] args) {SpringApplication.run(DiscoveryServiceApplication.class, args);} }
3.2 配置中心(Config Service)
3.2.1 创建 config-service 模块
 
在父项目的 modules 目录下创建 config-service 模块。
3.2.2 config-service 的 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>book-management-system</artifactId><version>1.0.0-SNAPSHOT</version></parent><artifactId>config-service</artifactId><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency></dependencies> </project>
3.2.3 config-service 的 application.yml
 
server:port: 8888spring:application:name: config-servicecloud:config:server:git:uri: https://github.com/your-repo/config-repo.gitclone-on-start: true
3.2.4 启动类 ConfigServiceApplication.java
 
package com.example.configservice;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer;@SpringBootApplication @EnableConfigServer public class ConfigServiceApplication {public static void main(String[] args) {SpringApplication.run(ConfigServiceApplication.class, args);} }
3.3 API网关(Gateway Service)
3.3.1 创建 gateway-service 模块
 
在父项目的 modules 目录下创建 gateway-service 模块。
3.3.2 gateway-service 的 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>book-management-system</artifactId><version>1.0.0-SNAPSHOT</version></parent><artifactId>gateway-service</artifactId><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency></dependencies> </project>
3.3.3 gateway-service 的 application.yml
 
server:port: 8080spring:application:name: gateway-servicecloud:gateway:routes:- id: book-serviceuri: lb://book-servicepredicates:- Path=/books/**eureka:client:service-url:defaultZone: http://localhost:8761/eureka/
3.3.4 启动类 GatewayServiceApplication.java
 
package com.example.gatewayservice;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication @EnableDiscoveryClient public class GatewayServiceApplication {public static void main(String[] args) {SpringApplication.run(GatewayServiceApplication.class, args);} }
通过上述示例,你可以看到Spring Boot和Spring Cloud的强大功能。Spring Boot简化了Spring应用的开发,而Spring Cloud则提供了一整套微服务解决方案。
结合使用这两个框架,可以快速构建健壮、可扩展的微服务应用。
图书管理系统实战
先简单分析这个实战的需求
创建一个基于Spring Boot和Spring Cloud的图书管理系统是一个很好的实践项目,可以帮助你深入理解微服务架构的设计和实现。
下面是一个详细的步骤指南分析,包括项目结构设计、技术栈选择、服务构建和集成等。
1. 技术栈选择
- Spring Boot: 快速开发微服务的基础框架。
 - Spring Cloud: 微服务治理工具集,包含服务发现、配置管理、API网关等功能。
 - 数据库: 可以选择MySQL、PostgreSQL等关系型数据库。
 - JPA/Hibernate: 持久层框架,用于操作数据库。
 - Eureka: 服务发现组件。
 - Hystrix: 断路器,用于处理分布式系统的延迟和容错。
 - Zuul/Gateway: API网关,用于路由请求。
 - Config Server: 配置中心,集中管理应用配置。
 - RabbitMQ/Kafka: 消息队列,用于异步通信。
 - Swagger: API文档工具。
 - Docker: 容器化部署。
 
2. 项目结构设计
2.1 微服务划分
- 图书服务 (Book Service): 处理与图书相关的业务逻辑。
 - 用户服务 (User Service): 管理用户信息和权限。
 - 订单服务 (Order Service): 处理借书和还书的订单。
 - 配置中心 (Config Service): 管理所有服务的配置文件。
 - 服务发现 (Discovery Service): 使用Eureka进行服务注册和发现。
 - API网关 (Gateway Service): 使用Zuul或Spring Cloud Gateway作为入口点,统一处理外部请求。
 
2.2 数据库设计
- 图书表: 包括图书ID、名称、作者、出版日期等字段。
 - 用户表: 包括用户ID、用户名、密码、邮箱等字段。
 - 订单表: 包括订单ID、用户ID、图书ID、借阅日期、归还日期等字段。
 
3. 创建Spring Boot项目
你可以使用Spring Initializr快速创建项目,选择相应的依赖项:
- Web
 - JPA
 - MySQL Driver (或其他数据库驱动)
 - Eureka Discovery Client (对于需要注册的服务)
 - Hystrix (可选)
 - Spring Cloud Config Client (对于需要读取配置的服务)
 
4. 实现各个服务
4.1 图书服务 (Book Service)
- 实体类: 
Book.java - 仓库接口: 
BookRepository.java继承JpaRepository - 服务类: 
BookService.java提供业务逻辑 - 控制器: 
BookController.java处理HTTP请求 
4.2 用户服务 (User Service)
- 实体类: 
User.java - 仓库接口: 
UserRepository.java - 服务类: 
UserService.java - 控制器: 
UserController.java 
4.3 订单服务 (Order Service)
- 实体类: 
Order.java - 仓库接口: 
OrderRepository.java - 服务类: 
OrderService.java - 控制器: 
OrderController.java 
5. 配置中心 (Config Service)
- 创建一个新的Spring Boot项目,添加 
spring-cloud-config-server依赖。 - 配置 
application.yml文件,指定Git仓库地址和其他配置。 - 启动类添加 
@EnableConfigServer注解。 
6. 服务发现 (Discovery Service)
- 创建一个新的Spring Boot项目,添加 
spring-cloud-starter-netflix-eureka-server依赖。 - 配置 
application.yml文件,设置端口和注册中心地址。 - 启动类添加 
@EnableEurekaServer注解。 
7. API网关 (Gateway Service)
- 创建一个新的Spring Boot项目,添加 
spring-cloud-starter-gateway和spring-cloud-starter-netflix-eureka-client依赖。 - 配置 
application.yml文件,设置路由规则和服务发现地址。 - 启动类无需额外注解。
 
8. 测试和部署
- 单元测试: 使用JUnit和Mockito编写单元测试。
 - 集成测试: 使用TestRestTemplate或RestAssured进行端到端测试。
 - 容器化: 使用Docker构建镜像,编写Dockerfile和docker-compose.yml文件。
 - 部署: 将服务部署到Kubernetes集群或Docker Swarm集群。
 
9. 文档和监控
- API文档: 使用Swagger生成API文档。
 - 监控: 使用Spring Boot Actuator和Prometheus/Grafana监控服务状态。
 
现在开始慢慢写出这个实战的代码
- 项目初始化
 - 服务发现(Eureka)
 - 图书服务(Book Service)
 - 配置中心(Config Service)
 - API网关(Gateway Service)
 
1. 项目初始化
首先,我们需要创建一个父项目来管理所有的子模块。使用Spring Initializr是一个不错的选择。
1.1 创建父项目
使用Spring Initializr创建一个父项目,选择以下依赖:
- Spring Web
 - Spring Data JPA
 - MySQL Driver
 - Spring Cloud Discovery (Eureka)
 - Spring Cloud Config
 
1.2 父项目的 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><groupId>com.example</groupId><artifactId>book-management-system</artifactId><version>1.0.0-SNAPSHOT</version><packaging>pom</packaging><modules><module>discovery-service</module><module>config-service</module><module>book-service</module><module>gateway-service</module></modules><properties><java.version>11</java.version><spring-cloud.version>2021.0.3</spring-cloud.version></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>2.7.5</version><type>pom</type><scope>import</scope></dependency><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> </project>
2. 服务发现(Eureka)
2.1 创建 discovery-service 模块
 
在父项目的 modules 目录下创建 discovery-service 模块。
2.2 discovery-service 的 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>book-management-system</artifactId><version>1.0.0-SNAPSHOT</version></parent><artifactId>discovery-service</artifactId><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency></dependencies> </project>
2.3 discovery-service 的 application.yml
 
server:port: 8761eureka:instance:hostname: localhostclient:register-with-eureka: falsefetch-registry: falseservice-url:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
2.4 discovery-service 的启动类
 
package com.example.discoveryservice;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication @EnableEurekaServer public class DiscoveryServiceApplication {public static void main(String[] args) {SpringApplication.run(DiscoveryServiceApplication.class, args);} }
3. 图书服务(Book Service)
3.1 创建 book-service 模块
 
在父项目的 modules 目录下创建 book-service 模块。
3.2 book-service 的 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>book-management-system</artifactId><version>1.0.0-SNAPSHOT</version></parent><artifactId>book-service</artifactId><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency></dependencies> </project>
3.3 book-service 的 application.yml
 
server:port: 8081spring:application:name: book-servicedatasource:url: jdbc:mysql://localhost:3306/book_db?useSSL=false&serverTimezone=UTCusername: rootpassword: rootjpa:hibernate:ddl-auto: updateshow-sql: trueeureka:client:service-url:defaultZone: http://localhost:8761/eureka/
3.4 实体类 Book.java
 
package com.example.bookservice.entity;import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id;@Entity public class Book {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private String title;private String author;private String publicationDate;// Getters and Setters }
3.5 仓库接口 BookRepository.java
 
package com.example.bookservice.repository;import com.example.bookservice.entity.Book; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository;@Repository public interface BookRepository extends JpaRepository<Book, Long> { }
3.6 服务类 BookService.java
 
package com.example.bookservice.service;import com.example.bookservice.entity.Book; import com.example.bookservice.repository.BookRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;import java.util.List; import java.util.Optional;@Service public class BookService {@Autowiredprivate BookRepository bookRepository;public List<Book> getAllBooks() {return bookRepository.findAll();}public Optional<Book> getBookById(Long id) {return bookRepository.findById(id);}public Book saveBook(Book book) {return bookRepository.save(book);}public void deleteBook(Long id) {bookRepository.deleteById(id);} }
3.7 控制器 BookController.java
 
package com.example.bookservice.controller;import com.example.bookservice.entity.Book; import com.example.bookservice.service.BookService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*;import java.util.List; import java.util.Optional;@RestController @RequestMapping("/books") public class BookController {@Autowiredprivate BookService bookService;@GetMappingpublic List<Book> getAllBooks() {return bookService.getAllBooks();}@GetMapping("/{id}")public Optional<Book> getBookById(@PathVariable Long id) {return bookService.getBookById(id);}@PostMappingpublic Book saveBook(@RequestBody Book book) {return bookService.saveBook(book);}@DeleteMapping("/{id}")public void deleteBook(@PathVariable Long id) {bookService.deleteBook(id);} }
3.8 启动类 BookServiceApplication.java
 
package com.example.bookservice;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication @EnableDiscoveryClient public class BookServiceApplication {public static void main(String[] args) {SpringApplication.run(BookServiceApplication.class, args);} }
4. 配置中心(Config Service)
4.1 创建 config-service 模块
 
在父项目的 modules 目录下创建 config-service 模块。
4.2 config-service 的 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>book-management-system</artifactId><version>1.0.0-SNAPSHOT</version></parent><artifactId>config-service</artifactId><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency></dependencies> </project>
4.3 config-service 的 application.yml
 
server:port: 8888spring:application:name: config-servicecloud:config:server:git:uri: https://github.com/your-repo/config-repo.gitclone-on-start: true
4.4 启动类 ConfigServiceApplication.java
 
package com.example.configservice;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer;@SpringBootApplication @EnableConfigServer public class ConfigServiceApplication {public static void main(String[] args) {SpringApplication.run(ConfigServiceApplication.class, args);} }
5. API网关(Gateway Service)
5.1 创建 gateway-service 模块
 
在父项目的 modules 目录下创建 gateway-service 模块。
5.2 gateway-service 的 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>book-management-system</artifactId><version>1.0.0-SNAPSHOT</version></parent><artifactId>gateway-service</artifactId><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency></dependencies> </project>
5.3 gateway-service 的 application.yml
 
server:port: 8080spring:application:name: gateway-servicecloud:gateway:routes:- id: book-serviceuri: lb://book-servicepredicates:- Path=/books/**eureka:client:service-url:defaultZone: http://localhost:8761/eureka/
5.4 启动类 GatewayServiceApplication.java
 
package com.example.gatewayservice;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication @EnableDiscoveryClient public class GatewayServiceApplication {public static void main(String[] args) {SpringApplication.run(GatewayServiceApplication.class, args);} }
总结
以上是一个完整的基于Spring Boot和Spring Cloud的图书管理系统的基本构建过程。每个模块都有详细的代码和注释,希望可以帮助到你。
相关文章:
深入讲解Spring Boot和Spring Cloud,外加图书管理系统实战!
很抱歉,我的疏忽,说了这么久还没有给大家详细讲解过Spring Boot和Spring Cloud,那今天给大家详细讲解一下。 大家可以和下面这三篇博客一起看: 1、Spring Boot 和 Spring Cloud 微服务开发实践详解https://blog.csdn.net/speaking_me/artic…...
【三维生成】Edify 3D:可扩展的高质量的3D资产生成(英伟达)
标题:Edify 3D: Scalable High-Quality 3D Asset Generation 项目:https://research.nvidia.com/labs/dir/edify-3d demo:https://build.nvidia.com/Shutterstock/edify-3d 文章目录 摘要一、前言二、多视图扩散模型2.1.消融研究 三、重建模型…...
Java求职招聘网站开发实践
一、项目介绍 本文将介绍如何使用Java技术栈开发一个求职招聘网站。该网站主要实现求职者和招聘方的双向选择功能,包含用户管理、职位发布、简历投递等核心功能。 二、技术选型 后端框架:Spring Boot 2.7.0数据库:MySQL 8.0前端框架&#…...
一文详细了解websocket应用以及连接断开的解决方案
文章目录 websocketvite 热启动探索websocket -心跳websocket 事件监听应用过程中问题总结 websocket Websocket简介 定义和工作原理 Websocket是一种在单个TCP连接上进行全双工通信的协议。与传统的HTTP请求 - 响应模式不同,它允许服务器主动向客户端推送数据。例…...
如何做含有identify抓信号的fpga版本(image或者Bit)
在数字的FPGA debug中除了ila就是identify了,identify是synopsys公司的RTL级的调试工具。要用起来idetify,第一步就是要做出含有identify的信号的FPGA版本,quartus的是image,Ximlinx的是Bit或者Bin文件。具体有以下几步࿱…...
AIGC实践-使用Amazon Bedrock的SDXL模型进行文生图
一、Bedrock 简介 Amazon Bedrock 是 Amazon Web Services (AWS) 提供的一种生成式 AI 服务。通过 Bedrock,用户可以方便地使用多种基础模型(Foundation Models),包括 OpenAI 的 GPT、Anthropic 的 Claude 等。这些模型可以用于各…...
【源码】Sharding-JDBC源码分析之SQL中分片键路由ShardingSQLRouter的原理
Sharding-JDBC系列 1、Sharding-JDBC分库分表的基本使用 2、Sharding-JDBC分库分表之SpringBoot分片策略 3、Sharding-JDBC分库分表之SpringBoot主从配置 4、SpringBoot集成Sharding-JDBC-5.3.0分库分表 5、SpringBoot集成Sharding-JDBC-5.3.0实现按月动态建表分表 6、【…...
初学 flutter 环境变量配置
一、jdk(jdk11) 1)配置环境变量 新增:JAVA_HOMEC:\Program Files\Java\jdk-11 //你的jdk目录 在path新增:%JAVA_HOME%\bin2)验证是否配置成功(cmd运行命令) java java -version …...
蓝牙 AVRCP 协议详解
前言 随着无线音频设备的普及,蓝牙已经成为智能设备间通信的主流方式之一。除了传输音频流的 A2DP 协议外,AVRCP(Audio/Video Remote Control Profile,音频/视频远程控制协议)为用户提供了对蓝牙音频设备的控制能力&am…...
在 Ubuntu 18.04 上安装 MySQL 5.7和MySQL 8
1.Ubuntu安装MySQL 5.72.Ubuntu安装MySQL 8 在 Ubuntu 18.04 上安装 MySQL 5.7,可以按照以下步骤操作: 1. 更新系统包列表 运行以下命令以确保系统包列表是最新的: sudo apt update2. 检查默认 MySQL 版本 Ubuntu 18.04 默认提供 MySQL 5.…...
第4章 Spring Boot自动配置
自动配置概述 SpringBoot的两大核心 Spring Boot 框架的两大核心特性可以概括为“启动器”(Starter)和“自动配置”(Auto-configuration)。 启动器(Starter): Spring Boot 提供了一系列的 Star…...
显存:存储,GPU:计算;Pipeline Parallelism(管道并行)
目录 显存:存储,GPU:计算 流水线切分策略:(数据并并,多头并行,单头MLP切片) 存储(显存)和计算(GPU)负载不均衡的问题 1,2,3,4,5指的计算任务(数据切分) 大方块代表GPU计算 黄色代表显存 解决办法:重计算和流水线切分策略 重计算策略: 流水线切分策略:…...
费曼路径积分简单示例
费曼路径积分简单示例 费曼路径积分是量子力学中的一种计算方法,它通过对所有可能路径的贡献进行积分,来计算粒子从一个点到另一个点的概率幅。与经典力学不同,经典力学中粒子沿着使作用量最小的路径运动,而在量子力学中…...
40分钟学 Go 语言高并发:【实战】并发安全的配置管理器(功能扩展)
【实战】并发安全的配置管理器(功能扩展) 一、扩展思考 分布式配置中心 实现配置的集中管理支持多节点配置同步实现配置的版本一致性 配置加密 敏感配置的加密存储配置的安全传输访问权限控制 配置格式支持 支持YAML、TOML等多种格式配置格式自动…...
麒麟安全增强-kysec
DAC: 自主访问控制是linux下默认的接入控制机制,通过对资源读、写、执行操作,保证系统安全 MAC:安全接入控制机制,由操作系统约束的访问控制,默认情况下,MAC不允许任何访问,用户可以自定义策略规则制定允许什么 ,从而避免很多攻击。 MAC强制访问控制常见的实现方式:…...
shell编程(8)
目录 一、until循环 示例 until 和 while 的区别 二、case语句 基本语法 示例 1. 简单的 case 语句 2. 使用通配符 3. 处理多个匹配 case 和 if 的比较 case 语句: if 语句: 三、基本函数 基本函数定义和调用 1. 定义一个简单的函数 2. …...
高级java每日一道面试题-2024年11月24日-JVM篇-说说对象分配规则?
如果有遗漏,评论区告诉我进行补充 面试官: 说说对象分配规则? 我回答: 在Java高级面试中,对象分配规则是一个核心考点,它涉及到JVM的内存管理、对象的创建和初始化等多个方面。以下是对Java对象分配规则的详细解释: 一、内存分配区域 J…...
进程间通信5:信号
引入 我们之前学习了信号量,信号量和信号可不是一个东西,不能混淆。 信号是什么以及一些基础概念 信号是一种让进程给其他进程发送异步消息的方式 信号是随时产生的,无法预测信号可以临时保存下来,之后再处理信号是异步发送的…...
性能测试及调优
一、性能测试介绍 1、什么叫做性能测试? (1)通过某些工具或手段来检测软件的某些指标是否达到了要求,这就是性能测试 (2)指通过自动化的测试工具模拟多种正常、峰值以及异常负载条件来对系统的各项性能指…...
实战基于LangChain和ChatGLM私有化部署聊天机器人
本文主要阐述了如何使用第二代6B模型进行对话训练,以及如何通过微调来提高大模型的性能。文中提到了在8501端口上启动第二代6B模型,并使用极简模板进行请求。与第一代模型相比,第二代6B模型具有更强的对话能力,并且可以通过微调来…...
KubeSphere 容器平台高可用:环境搭建与可视化操作指南
Linux_k8s篇 欢迎来到Linux的世界,看笔记好好学多敲多打,每个人都是大神! 题目:KubeSphere 容器平台高可用:环境搭建与可视化操作指南 版本号: 1.0,0 作者: 老王要学习 日期: 2025.06.05 适用环境: Ubuntu22 文档说…...
Qt Widget类解析与代码注释
#include "widget.h" #include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget) {ui->setupUi(this); }Widget::~Widget() {delete ui; }//解释这串代码,写上注释 当然可以!这段代码是 Qt …...
376. Wiggle Subsequence
376. Wiggle Subsequence 代码 class Solution { public:int wiggleMaxLength(vector<int>& nums) {int n nums.size();int res 1;int prediff 0;int curdiff 0;for(int i 0;i < n-1;i){curdiff nums[i1] - nums[i];if( (prediff > 0 && curdif…...
Linux-07 ubuntu 的 chrome 启动不了
文章目录 问题原因解决步骤一、卸载旧版chrome二、重新安装chorme三、启动不了,报错如下四、启动不了,解决如下 总结 问题原因 在应用中可以看到chrome,但是打不开(说明:原来的ubuntu系统出问题了,这个是备用的硬盘&a…...
云原生安全实战:API网关Kong的鉴权与限流详解
🔥「炎码工坊」技术弹药已装填! 点击关注 → 解锁工业级干货【工具实测|项目避坑|源码燃烧指南】 一、基础概念 1. API网关(API Gateway) API网关是微服务架构中的核心组件,负责统一管理所有API的流量入口。它像一座…...
【p2p、分布式,区块链笔记 MESH】Bluetooth蓝牙通信 BLE Mesh协议的拓扑结构 定向转发机制
目录 节点的功能承载层(GATT/Adv)局限性: 拓扑关系定向转发机制定向转发意义 CG 节点的功能 节点的功能由节点支持的特性和功能决定。所有节点都能够发送和接收网格消息。节点还可以选择支持一个或多个附加功能,如 Configuration …...
华为OD最新机试真题-数组组成的最小数字-OD统一考试(B卷)
题目描述 给定一个整型数组,请从该数组中选择3个元素 组成最小数字并输出 (如果数组长度小于3,则选择数组中所有元素来组成最小数字)。 输入描述 行用半角逗号分割的字符串记录的整型数组,0<数组长度<= 100,0<整数的取值范围<= 10000。 输出描述 由3个元素组成…...
MySQL的pymysql操作
本章是MySQL的最后一章,MySQL到此完结,下一站Hadoop!!! 这章很简单,完整代码在最后,详细讲解之前python课程里面也有,感兴趣的可以往前找一下 一、查询操作 我们需要打开pycharm …...
鸿蒙(HarmonyOS5)实现跳一跳小游戏
下面我将介绍如何使用鸿蒙的ArkUI框架,实现一个简单的跳一跳小游戏。 1. 项目结构 src/main/ets/ ├── MainAbility │ ├── pages │ │ ├── Index.ets // 主页面 │ │ └── GamePage.ets // 游戏页面 │ └── model │ …...
数据结构第5章:树和二叉树完全指南(自整理详细图文笔记)
名人说:莫道桑榆晚,为霞尚满天。——刘禹锡(刘梦得,诗豪) 原创笔记:Code_流苏(CSDN)(一个喜欢古诗词和编程的Coder😊) 上一篇:《数据结构第4章 数组和广义表》…...
