当前位置: 首页 > news >正文

深入讲解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监控服务状态。

现在开始慢慢写出这个实战的代码

  1. 项目初始化
  2. 服务发现(Eureka)
  3. 图书服务(Book Service)
  4. 配置中心(Config Service)
  5. 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,外加图书管理系统实战!

很抱歉&#xff0c;我的疏忽&#xff0c;说了这么久还没有给大家详细讲解过Spring Boot和Spring Cloud,那今天给大家详细讲解一下。 大家可以和下面这三篇博客一起看&#xff1a; 1、Spring Boot 和 Spring Cloud 微服务开发实践详解https://blog.csdn.net/speaking_me/artic…...

【AIGC】2023-ICCV-用于高保真语音肖像合成的高效区域感知神经辐射场

2023-ICCV-Efficient Region-Aware Neural Radiance Fields for High-Fidelity Talking Portrait Synthesis 用于高保真语音肖像合成的高效区域感知神经辐射场摘要1. 引言2. 相关工作3. 方法3.1 准备工作和问题设置3.2 三平面哈希表示3.3. 区域注意模块3.4 训练细节 4. 实验4.1…...

如何写一份优质技术文档

作者简介&#xff1a; 本文作者拥有区块链创新专利30&#xff0c;是元宇宙标准化工作组成员、香港web3标准工作组成员&#xff0c;参与编写《数据资产确权与交易安全评价标准》、《链接元宇宙&#xff1a;应用与实践》、《香港Web3.0标准化白皮书》等标准&#xff0c;下面提供…...

ML 系列:第 35 节 - 机器学习中的数据可视化

ML 系列&#xff1a;第 35 天 - 机器学习中的数据可视化 文章目录 一、说明二、数据可视化2.1 直方图2.2 箱线图2.3 散点图2.4 条形图2.5 线图2.6 热图 三、结尾 一、说明 描述性统计和数据可视化是理解和解释机器学习数据的基础。它们有助于总结和直观地呈现数据&#xff0c…...

存储服务器一般做是做什么阵列?详细列举一下

存储服务器通常使用 RAID&#xff08;Redundant Array of Independent Disks&#xff09; 阵列技术来管理磁盘&#xff0c;以提高数据的性能、可靠性和可用性。所选择的 RAID 类型取决于存储服务器的具体用途和需求&#xff0c;比如性能要求、容量需求、容错能力等。 以下是存…...

uniapp使用扩展组件uni-data-select出现的问题汇总

前言 不知道大家有没有学习过我的这门课程那&#xff0c;《uniCloud云开发Vue3版本官方推荐用法》&#xff0c;这么课程已经得到了官方推荐&#xff0c;想要快速上手unicloud的小伙伴们&#xff0c;可以学习一下这么课程哦&#xff0c;不要忘了给一键三连呀。 在录制这门课程…...

pdf.js 预览pdf的时候发票数据缺失显示不全:字体加载出错(缺失)导致部分缺失

首先&#xff0c;排除后端返回的PDF文件流是没有问题的&#xff1a; 但是在vue项目中是这样的&#xff1a; 明显是显示不全&#xff0c;F12查看报错信息&#xff0c;有以下警告&#xff1a; pdf.js:2153 Warning: Error during font loading: The CMap “baseUrl” paramet…...

【设计模式】【结构型模式(Structural Patterns)】之外观模式(Facade Pattern)

1. 设计模式原理说明 外观模式&#xff08;Facade Pattern&#xff09; 是一种结构型设计模式&#xff0c;它提供了一个统一的接口&#xff0c;用来访问子系统中的一群接口。外观模式定义了一个高层接口&#xff0c;这个接口使得这一子系统更加容易使用。通过隐藏子系统的复杂…...

Redis使用场景-缓存-缓存穿透

前言 之前在针对实习面试的博文中讲到Redis在实际开发中的生产问题&#xff0c;其中缓存穿透、击穿、雪崩在面试中问的最频繁&#xff0c;本文加了图解&#xff0c;希望帮助你更直观的了解缓存穿透&#x1f600; &#xff08;放出之前写的针对实习面试的关于Redis生产问题的博…...

介绍 Apache Spark 的基本概念和在大数据分析中的应用

Apache Spark 是一个开源的大数据处理框架&#xff0c;它提供了快速、通用、可扩展的数据处理能力。Spark可以处理大规模数据集&#xff0c;并且在内存中进行数据操作&#xff0c;从而实现高速的数据处理和分析。 Spark的核心概念是弹性分布式数据集&#xff08;Resilient Dis…...

OpenCPN-插件之Dashboard Tactics

1&#xff1a;相关链接Dashboard Tactics :: OpenCPN Dashboard Tactics Plugin rgleason/dashboard_tactics_pi: OpenCPN dashboard built-in plugin merger with external tactics_pi plugin NMEAconverter :: OpenCPN 2&#xff1a;显示样式 3&#xff1a;代码 这个插件…...

【LeetCode面试150】——20有效的括号

博客昵称&#xff1a;沈小农学编程 作者简介&#xff1a;一名在读硕士&#xff0c;定期更新相关算法面试题&#xff0c;欢迎关注小弟&#xff01; PS&#xff1a;哈喽&#xff01;各位CSDN的uu们&#xff0c;我是你的小弟沈小农&#xff0c;希望我的文章能帮助到你。欢迎大家在…...

JWT介绍和结合springboot项目实践(登录、注销授权认证管理)

目录 一、JWT介绍&#xff08;一&#xff09;基本介绍&#xff08;二&#xff09;jwt有哪些库1、jjwt&#xff08;Java JWT&#xff09;2、nimbus - jwt - jwt - api 和 nimbus - jwt - jwt - impl3、spring - security - jwt&#xff08;已弃用&#xff0c;但在旧项目中有参考…...

Linux 下安装 Golang环境

Linux 下安装 Golang 获取Golang下载地址 安装 进入终端&#xff0c;登入root来到应用安装目录使用 wget 下载解压文件配置环境变量查看golang版本&#xff0c;测试是否配置成功GO设置代理环境变量 本篇教程 以 centos7 为环境基础 不使用软件包管理器安装&#xff0c;原因&am…...

「Mac畅玩鸿蒙与硬件36」UI互动应用篇13 - 数字滚动抽奖器

本篇将带你实现一个简单的数字滚动抽奖器。用户点击按钮后&#xff0c;屏幕上的数字会以滚动动画的形式随机变动&#xff0c;最终显示一个抽奖数字。这个项目展示了如何结合定时器、状态管理和动画实现一个有趣的互动应用。 关键词 UI互动应用数字滚动动画效果状态管理用户交…...

安装使用Ubuntu18.04超级大全集最初版(anaconda,pycharm,代理,c/c++环境)

本文介绍ubuntu1804中我目前用到的环境的完整配置&#xff0c;包括ubuntu安装软件&#xff0c;更新环境变量等都有涉及。图片非常多&#xff0c;能给的连接和材料都给了。希望能帮助到新同学。 目录 目录 目录 环境及镜像文件 安装Ubuntu ​编辑 开机之后 ​编辑 更新…...

Redis设计与实现第16章 -- Sentinel 总结1(初始化、主从服务器获取信息、发送信息、接收信息)

Sentinel是Redis的高可用解决方案&#xff1a;由一个或多个Sentinel实例组成的Sentinel系统可以监视任意多个主服务器&#xff0c;以及这些主服务器属下的所有从服务器&#xff0c;被监视的主服务器进入下线状态时&#xff0c;自动将下线主服务器属下的某个从服务器升级为新的主…...

ChatGPT科研应用、论文写作、课题申报、数据分析与AI绘图

随着人工智能技术的飞速发展&#xff0c;ChatGPT等先进语言模型正深刻改变着科研工作的面貌。从科研灵感的激发、论文的高效撰写&#xff0c;到课题的成功申报&#xff0c;乃至复杂数据的深度分析与可视化呈现&#xff0c;AI技术均展现出前所未有的潜力。其实众多科研前沿工作者…...

OceanBase数据库系列之:基于docker快速安装OceanBase数据库,基于linux服务器快速部署OceanBase数据库

OceanBase数据库系列之&#xff1a;基于docker快速安装OceanBase数据库&#xff0c;基于linux服务器快速部署OceanBase数据库 一、docker快速安装OceanBase数据库下载OceanBase数据库镜像查看镜像启动OceanBase数据库查看OceanBase数据库是否启动成功 二、基于linux部署OceanBa…...

无星的微前端之旅(四)——qiankun线上服务代理到本地

这个方式其实是我在上家公司的时候体验过&#xff0c;觉得确实很有意思。 所以这里来逆推一下实现方式。 解决了什么痛点 1.开发一个模块&#xff0c;需要启动2-3个项目 在微前端的开发过程中&#xff0c;如果我们要在主应用中看效果&#xff0c;我们至少需要启动一个主应用&am…...

鸿蒙进阶篇-Stage模型、UIAbility

“在科技的浪潮中&#xff0c;鸿蒙操作系统宛如一颗璀璨的新星&#xff0c;引领着创新的方向。作为鸿蒙开天组&#xff0c;今天我们将一同踏上鸿蒙基础的探索之旅&#xff0c;为您揭开这一神奇系统的神秘面纱。” 各位小伙伴们我们又见面了,我就是鸿蒙开天组,下面让我们进入今…...

快速上手:如何开发一个实用的 Edge 插件

在日常浏览网页时&#xff0c;背景图片能够显著提升网页的视觉体验。如果你也想为自己的浏览器页面添加个性化背景图片&#xff0c;并希望背景图片设置能够持久保存&#xff0c;本文将介绍如何通过开发一个自定义Edge插件来实现这一功能。我们将涵盖保存背景设置到插件选项页&a…...

java缓存技术点介绍

1. 缓存&#xff08;Cache&#xff09;&#xff1a; • 缓存是指用于存储数据的临时存储区域&#xff0c;以便快速访问。在Java中&#xff0c;缓存通常用于存储频繁访问的对象、结果集或其他数据。 2. 缓存命中率&#xff08;Cache Hit Ratio&#xff09;&#xff1a; • 缓存命…...

【单片机毕业设计12-基于stm32c8t6的智能称重系统设计】

【单片机毕业设计12-基于stm32c8t6的智能称重系统设计】 前言一、功能介绍二、硬件部分三、软件部分总结 前言 &#x1f525;这里是小殷学长&#xff0c;单片机毕业设计篇12-基于stm32c8t6的智能称重系统设计 &#x1f9ff;创作不易&#xff0c;拒绝白嫖可私 一、功能介绍 ----…...

音视频流媒体直播/点播系统EasyDSS互联网视频云平台介绍

随着互联网技术的飞速发展&#xff0c;音视频流媒体直播已成为现代社会信息传递与娱乐消费的重要组成部分。在这样的背景下&#xff0c;EasyDSS互联网视频云平台应运而生&#xff0c;它以高效、稳定、便捷的特性&#xff0c;为音视频流媒体直播领域带来了全新的解决方案。 1、产…...

3dtile平移子模型以及修改 3D Tiles 模型的模型矩阵z平移

第一段代码&#xff1a;应用平移变换到子模型 这段代码的目的是获取子模型的变换矩阵&#xff0c;并将其平移 10 个单位。 if (submodel) {// 获取当前子模型的变换矩阵let transform submodel.transform// 创建一个向上的平移矩阵&#xff0c;平移 10 个单位let translation…...

JavaScript中类数组对象及其与数组的关系

JavaScript中类数组对象及其与数组的关系 1. 什么是类数组对象&#xff1f; 类数组对象是指那些具有 length 属性且可以通过非负整数索引访问元素的对象。虽然这些对象看起来像数组&#xff0c;但它们并不具备真正数组的所有特性&#xff0c;例如没有继承 Array.prototype 上…...

【机器学习】机器学习学习笔记 - 监督学习 - 逻辑回归分类朴素贝叶斯分类支持向量机 SVM (可分类、可回归) - 04

逻辑回归分类 import numpy as np from sklearn import linear_modelX np.array([[4, 7], [3.5, 8], [3.1, 6.2], [0.5, 1], [1, 2], [1.2, 1.9], [6, 2], [5.7, 1.5], [5.4, 2.2]]) y np.array([0, 0, 0, 1, 1, 1, 2, 2, 2])# 逻辑回归分类器 # solver&#xff1a;求解器&a…...

Python脚本文件开头两行#!/usr/bin/python和# -*- coding: utf-8 -*-的作用

Linux环境下&#xff0c;Python脚本代码文件&#xff0c;比如test.py&#xff0c;文件的第一行一般要指定解释器&#xff0c;使用Linux的Shebang的形式指定&#xff0c;第二行一般指定编码格式。 首行指定解释器工具&#xff0c;使用方式如下&#xff1a; # 第1种方式&#x…...

使用PyQt5开发一个GUI程序的实例演示

一、安装Python 下载安装到这个目录 G:\Python38-32 安装完成有这些工具&#xff0c;后面备用&#xff1a; G:\Python38-32\Scripts\pyrcc5.exe G:\Python38-32\Scripts\pyuic5.exe 二、PyQt环境配置 pip install PyQt5 pip install pyqt5-tools 建议使用国内源&#xff0c…...

3d扫描建模产品开发-三维扫描检测蓝光检测

现当下&#xff0c;汽车制造、航空航天&#xff0c;还是消费电子、医疗器械&#xff0c;三维扫描检测与蓝光技术正以前所未有的精度和效率&#xff0c;推动着产品从概念到实物的快速转化。 三维扫描技术&#xff0c;简而言之&#xff0c;就是通过激光、结构光&#xff08;如蓝…...

多线程——01

1. 进程/任务 process/task 进程是操作系统对一个正在运行的程序的一种抽象&#xff0c;换言之&#xff0c;可以把进程看做程序的一次运行过程 每个进程想要执行&#xff0c;都需要消耗一定的系统资源&#xff08;硬件资源&#xff09; 1&#xff09;进程在系统中的作用 a…...

Makefile中-D选项定义预编译处理宏

1.简单示例假设我们有一个简单的 C 程序main.c,内容如下: #include <stdio.h> #ifdef DEBUGvoid print_debug_info() {printf("This is debug information.\n");} #elsevoid print_debug_info() {} #endif int main() {print_debug_info();printf("Prog…...

NAPI杂记

NAPI和tasklet区别&#xff1a;NAPI是利用的软中断NET_RX_SOFTIRQ实现&#xff0c; tasklet是利用TASKLET_SOFTIRQ实现 另外文章说NAPI主要是采用轮询&#xff0c;可以减少中断触发次数。do_softirq里面是开中断执行的啊&#xff1f; 中断处理函数关闭中断开始处理。如果此时…...

006 MATLAB编程基础

01 M文件 MATLAB输入命令有两种方法&#xff1a; 一是在MATLAB主窗口逐行输入命令&#xff0c;每个命令之间用分号或逗号分隔&#xff0c;每行可包含多个命令。 二是将命令组织成一个命令语句文集&#xff0c;使用扩展名“.m”&#xff0c;称为M文件。它由一系列的命令和语句…...

使用Alpine镜像作为基础镜像的Dockerfile配置

配置阿里 apk源&#xff1a; /etc/apk/repositories&#xff1a; https://mirrors.aliyun.com/alpine/v3.13/main alpine-v3.13-community安装包下载_开源镜像站-阿里云 或者使用命令 sed -i sdl-cdn.alpinelinux.orgmirrors.aliyun.comg /etc/apk/repositories dockerfil…...

RAG (Retrieval Augmented Generation) 检索增强和生成

1 RAG技术简介 1.1 RAG技术概述 RAG&#xff08;Retrieval Augmented Generation&#xff09; 是一种结合了检索&#xff08;Retrieval&#xff09;和生成&#xff08;Generation&#xff09;的技术&#xff0c;旨在通过利用外部知识库来增强大型语言模型&#xff08;LLMs&am…...

Figma入门-约束与对齐

Figma入门-约束与对齐 前言 在之前的工作中&#xff0c;大家的原型图都是使用 Axure 制作的&#xff0c;印象中 Figma 一直是个专业设计软件。 最近&#xff0c;很多产品朋友告诉我&#xff0c;很多原型图都开始用Figma制作了&#xff0c;并且很多组件都是内置的&#xff0c…...

039集——渐变色之:CAD中画彩虹()(CAD—C#二次开发入门)

&#xff08;来左边儿 跟我一起画个龙&#xff0c;在你右边儿 画一道彩虹 ~~~~~~~~~~~ &#xff09; 效果如下&#xff1a; namespace AcTools {public class Class1{public Wform.Timer timer;//定时器需建在类下面public static DateTime startTime;[CommandM…...

MTK主板_小型联发科安卓主板_行业智能终端主板基于联发科方案

MTK安卓主板是一款小巧而高效的科技产品&#xff0c;其尺寸仅为43.4mm x 57.6mm。采用了先进的联发科12nm制程工艺&#xff0c;这款主板搭载四核或八核64位A53架构的CPU&#xff0c;主频高达2.0GHz&#xff0c;不但保证了出色的计算能力&#xff0c;还实现了超低功耗的特点。系…...

Arrays.copyOfRange(),System.arraycopy() 数组复制,数组扩容

Arrays.copyOfRange() 当需要将数组中的 长度扩容时, 数组复制 使用 需要用到Arrays 类提供的的 参数解析 * param original the array from which a range is to be copied * param from the initial index of the range to be copied, inclusive * param to the final ind…...

Docker for Everyone Plus——No Enough Privilege

直接告诉我们flag在/flag中&#xff0c;访问第一小题&#xff1a; sudo -l查看允许提权执行的命令&#xff1a; 发现有image load命令 题目指明了有rz命令&#xff0c;可以用ZMODEM接收文件&#xff0c;看到一些write up说可以用XShell、MobaXterm、Tabby Terminal等软件连接上…...

ElasticSearch学习笔记把:Springboot整合ES(二)

一、前言 上一篇文章中我们学习了ES中的Term级别的查询&#xff0c;包括 term、terms、terms_set、rang等&#xff0c;今天我们使用Java代码实现一遍上述的查询。 二、项目依赖 POM <?xml version"1.0" encoding"UTF-8"?> <project xmlns&q…...

Linux系统之iotop命令的基本使用

Linux系统之iotop命令的基本使用 一、iotop命令介绍二、iotop命令的使用帮助2.1 安装iotop2.2 iotop命令help帮助信息2.3 iotop命令选项解释 三、 iotop命令的基本使用四、iotop使用注意事项 一、iotop命令介绍 iotop 是一个类似于 top 的命令行工具&#xff0c;但它专注于显示…...

根据合约地址判断合约协议的方法

判断合约协议之前&#xff0c;需要了解一下什么是ERC165协议&#xff1a; ERC165 是以太坊中用于标准化接口检测的协议&#xff0c;由 Fabian Vogelsteller 在 2018 年创建 &#xff0c;其核心内容主要包括以下方面&#xff1a; 接口定义 单一函数接口&#xff1a;ERC165 协议…...

什么是sfp,onu,​为什么PON(​俗称“光猫”​)模块使用SC光纤接口

在现代网络设备中&#xff0c;我们经常会看到SFP或SFP接口的身影&#xff0c;这些接口有时被简称为光口&#xff0c;但这个称呼并不严谨。有些厂商则称之为多功能口或多用途口&#xff0c;然而这对于不了解的人来说可能还是一头雾水。SFP&#xff0c;即Small Form-Factor Plugg…...

链表?->?(以尾插法说明,附头插法)

这篇文章做一些关于初学链表的一些问题的解答。 我知道有些朋友只是需要一份完整的关于链表的代码&#xff0c;我会附在后面&#xff0c;大家直接划到最后就好。 一、创建链表 (1 相信所有搜索过链表创建的朋友都看过这样一行&#xff1a; struct Line* head (struct Line…...

11.29周五F34-Day10打卡

文章目录 1. 问问他能不能来。解析答案:【解析答案分析】【对比分析】【拓展内容】2. 问题是他能不能做。解析答案:【解析答案分析】3. 问题是我们能否联系得上她。(什么关系?动作 or 描述?)解析答案:【解析答案分析】【对比分析】4. 我们在讨论是否要开一个会。解析答案:…...

【项目日记】仿mudou的高并发服务器 --- 实现HTTP服务器

对于生命&#xff0c;你不妨大胆一点&#xff0c; 因为我们始终要失去它。 --- 尼采 --- ✨✨✨项目地址在这里 ✨✨✨ ✨✨✨https://gitee.com/penggli_2_0/TcpServer✨✨✨ 仿mudou的高并发服务器 1 前言2 Util工具类3 HTTP协议3.1 HTTP请求3.2 HTTP应答 4 上下文解析模块…...

【SpringBoot问题】IDEA中用Service窗口展示所有服务及端口的办法

1、调出Service窗口 打开View→Tool Windows→Service&#xff0c;即可显示。 2、正常情况应该已经出现SpringBoot&#xff0c;如下图请继续第三步 3、配置Service窗口的项目启动类型。微服务一般是Springboot类型。所以这里需要选择一下。 点击最后一个号&#xff0c;点击Ru…...