当前位置: 首页 > 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…...

【三维生成】Edify 3D:可扩展的高质量的3D资产生成(英伟达)

标题&#xff1a;Edify 3D: Scalable High-Quality 3D Asset Generation 项目&#xff1a;https://research.nvidia.com/labs/dir/edify-3d demo&#xff1a;https://build.nvidia.com/Shutterstock/edify-3d 文章目录 摘要一、前言二、多视图扩散模型2.1.消融研究 三、重建模型…...

Java求职招聘网站开发实践

一、项目介绍 本文将介绍如何使用Java技术栈开发一个求职招聘网站。该网站主要实现求职者和招聘方的双向选择功能&#xff0c;包含用户管理、职位发布、简历投递等核心功能。 二、技术选型 后端框架&#xff1a;Spring Boot 2.7.0数据库&#xff1a;MySQL 8.0前端框架&#…...

一文详细了解websocket应用以及连接断开的解决方案

文章目录 websocketvite 热启动探索websocket -心跳websocket 事件监听应用过程中问题总结 websocket Websocket简介 定义和工作原理 Websocket是一种在单个TCP连接上进行全双工通信的协议。与传统的HTTP请求 - 响应模式不同&#xff0c;它允许服务器主动向客户端推送数据。例…...

如何做含有identify抓信号的fpga版本(image或者Bit)

在数字的FPGA debug中除了ila就是identify了&#xff0c;identify是synopsys公司的RTL级的调试工具。要用起来idetify&#xff0c;第一步就是要做出含有identify的信号的FPGA版本&#xff0c;quartus的是image&#xff0c;Ximlinx的是Bit或者Bin文件。具体有以下几步&#xff1…...

AIGC实践-使用Amazon Bedrock的SDXL模型进行文生图

一、Bedrock 简介 Amazon Bedrock 是 Amazon Web Services (AWS) 提供的一种生成式 AI 服务。通过 Bedrock&#xff0c;用户可以方便地使用多种基础模型&#xff08;Foundation Models&#xff09;&#xff0c;包括 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&#xff08;jdk11&#xff09; 1&#xff09;配置环境变量 新增&#xff1a;JAVA_HOMEC:\Program Files\Java\jdk-11 //你的jdk目录 在path新增&#xff1a;%JAVA_HOME%\bin2&#xff09;验证是否配置成功&#xff08;cmd运行命令&#xff09; java java -version …...

蓝牙 AVRCP 协议详解

前言 随着无线音频设备的普及&#xff0c;蓝牙已经成为智能设备间通信的主流方式之一。除了传输音频流的 A2DP 协议外&#xff0c;AVRCP&#xff08;Audio/Video Remote Control Profile&#xff0c;音频/视频远程控制协议&#xff09;为用户提供了对蓝牙音频设备的控制能力&am…...

在 Ubuntu 18.04 上安装 MySQL 5.7和MySQL 8

1.Ubuntu安装MySQL 5.72.Ubuntu安装MySQL 8 在 Ubuntu 18.04 上安装 MySQL 5.7&#xff0c;可以按照以下步骤操作&#xff1a; 1. 更新系统包列表 运行以下命令以确保系统包列表是最新的&#xff1a; sudo apt update2. 检查默认 MySQL 版本 Ubuntu 18.04 默认提供 MySQL 5.…...

第4章 Spring Boot自动配置

自动配置概述 SpringBoot的两大核心 Spring Boot 框架的两大核心特性可以概括为“启动器”&#xff08;Starter&#xff09;和“自动配置”&#xff08;Auto-configuration&#xff09;。 启动器&#xff08;Starter&#xff09;&#xff1a; Spring Boot 提供了一系列的 Star…...

显存:存储,GPU:计算;Pipeline Parallelism(管道并行)

目录 显存:存储,GPU:计算 流水线切分策略:(数据并并,多头并行,单头MLP切片) 存储(显存)和计算(GPU)负载不均衡的问题 1,2,3,4,5指的计算任务(数据切分) 大方块代表GPU计算 黄色代表显存 解决办法:重计算和流水线切分策略 重计算策略: 流水线切分策略:…...

费曼路径积分简单示例

费曼路径积分简单示例 费曼路径积分是量子力学中的一种计算方法&#xff0c;它通过对所有可能路径的贡献进行积分&#xff0c;来计算粒子从一个点到另一个点的概率幅。与经典力学不同&#xff0c;经典力学中粒子沿着使作用量最小的路径运动&#xff0c;而在量子力学中&#xf…...

40分钟学 Go 语言高并发:【实战】并发安全的配置管理器(功能扩展)

【实战】并发安全的配置管理器&#xff08;功能扩展&#xff09; 一、扩展思考 分布式配置中心 实现配置的集中管理支持多节点配置同步实现配置的版本一致性 配置加密 敏感配置的加密存储配置的安全传输访问权限控制 配置格式支持 支持YAML、TOML等多种格式配置格式自动…...

麒麟安全增强-kysec

DAC: 自主访问控制是linux下默认的接入控制机制,通过对资源读、写、执行操作,保证系统安全 MAC:安全接入控制机制,由操作系统约束的访问控制,默认情况下,MAC不允许任何访问,用户可以自定义策略规则制定允许什么 ,从而避免很多攻击。 MAC强制访问控制常见的实现方式:…...

shell编程(8)

目录 一、until循环 示例 until 和 while 的区别 二、case语句 基本语法 示例 1. 简单的 case 语句 2. 使用通配符 3. 处理多个匹配 case 和 if 的比较 case 语句&#xff1a; if 语句&#xff1a; 三、基本函数 基本函数定义和调用 1. 定义一个简单的函数 2. …...

高级java每日一道面试题-2024年11月24日-JVM篇-说说对象分配规则?

如果有遗漏,评论区告诉我进行补充 面试官: 说说对象分配规则? 我回答: 在Java高级面试中&#xff0c;对象分配规则是一个核心考点&#xff0c;它涉及到JVM的内存管理、对象的创建和初始化等多个方面。以下是对Java对象分配规则的详细解释&#xff1a; 一、内存分配区域 J…...

进程间通信5:信号

引入 我们之前学习了信号量&#xff0c;信号量和信号可不是一个东西&#xff0c;不能混淆。 信号是什么以及一些基础概念 信号是一种让进程给其他进程发送异步消息的方式 信号是随时产生的&#xff0c;无法预测信号可以临时保存下来&#xff0c;之后再处理信号是异步发送的…...

性能测试及调优

一、性能测试介绍 1、什么叫做性能测试&#xff1f; &#xff08;1&#xff09;通过某些工具或手段来检测软件的某些指标是否达到了要求&#xff0c;这就是性能测试 &#xff08;2&#xff09;指通过自动化的测试工具模拟多种正常、峰值以及异常负载条件来对系统的各项性能指…...

实战基于LangChain和ChatGLM私有化部署聊天机器人

本文主要阐述了如何使用第二代6B模型进行对话训练&#xff0c;以及如何通过微调来提高大模型的性能。文中提到了在8501端口上启动第二代6B模型&#xff0c;并使用极简模板进行请求。与第一代模型相比&#xff0c;第二代6B模型具有更强的对话能力&#xff0c;并且可以通过微调来…...

利用adb工具安装卸载安卓平板(手机)软件

参考链接&#xff1a; 1、ADB 操作命令详解及用法大全 2、全面掌握Android调试工具箱&#xff1a;ADB与实用程序实战 平时使用小米手机没有感觉&#xff0c;miui系统做的确实好。最近买了个水货学习系统平板&#xff08;主要看重硬件配置&#xff0c;性价比很高&#xff0c;但…...

基于docker进行任意项目灵活发布

引言 不管是java还是python程序等&#xff0c;使用docker发布的优势有以下几点&#xff1a; 易于维护。直接docker命令进行管理&#xff0c;如docker stop、docker start等&#xff0c;快速方便无需各种进程查询关闭。环境隔离。项目代码任何依赖或设置都可以基本独立&#x…...

Datatables:监听行内文本框,进行行内数据修改;计算行总和

一、监听行内文本框&#xff0c;进行行内数据修改 效果 修改数量、单价会自动计算金额&#xff08;金额数量*单价&#xff09; 实现 1、增加行的class 2、数据监听、修改数值 "initComplete": function() {// 监听数量和单价输入框的变化$(document).on(input, .…...

对于某些原型或UI软件的个人看法(2024/11)

由于我这几天&#xff0c;一边敲代码&#xff0c;一边进行页面布局设计与编码&#xff0c;发现可能就一个卡片&#xff0c;我都得调很久样式&#xff0c;觉得这样改很累也没效率&#xff0c;页面也不是很美观。所以我想到了ui设计&#xff0c;我可以先进行ui设计&#xff0c;然…...

嵌入式硬件实战提升篇(二)PCB高速板设计 FPGA核心板带DDR3 PCB设计DDR全面解析

引言&#xff1a;设计一款高速板&#xff0c;供读者学习&#xff0c;FPGA核心板&#xff0c;带一颗DDR3内存&#xff0c;FPGA型号&#xff1a;XC6SLX16-2FTG256C。 随着嵌入式硬件技术的快速发展&#xff0c;高速板设计逐渐成为嵌入式系统设计中的核心技术之一。高速板的设计要…...

亚信安全携手飞书“走近先进” 与保隆科技探索制造业数字化转型

亚信安全携手飞书组织举办“走近先进”活动。近日活动“走近”了中国汽车供应链百强、上海市制造业五十强企业——上海保隆汽车科技股份有限公司&#xff08;以下简称“保隆科技”&#xff09;。活动围绕“突破桎梏 加速升级”的主题&#xff0c;聚焦企业数字化转型的核心议题&…...

【C++篇】排队的艺术:用生活场景讲解优先级队列的实现

文章目录 须知 &#x1f4ac; 欢迎讨论&#xff1a;如果你在学习过程中有任何问题或想法&#xff0c;欢迎在评论区留言&#xff0c;我们一起交流学习。你的支持是我继续创作的动力&#xff01; &#x1f44d; 点赞、收藏与分享&#xff1a;觉得这篇文章对你有帮助吗&#xff1…...

VTK的基本概念(一)

文章目录 三维场景的基本要素1.灯光2.相机3.颜色4.纹理映射 三维场景的基本要素 1.灯光 在三维渲染场景中&#xff0c;可以有多个灯光的存在&#xff0c;灯光和相机是三维渲染场景的必备要素&#xff0c;如果没有指定的话&#xff0c;vtkRenderer会自动创建默认的灯光和相机。…...

error LNK2001: 无法解析的外部符号 memcpy strcmp strlen

0>LIBMY_static.lib(pixdesc.obj) : error LNK2001: 无法解析的外部符号 __imp_abort 10>LIBMY_static.lib(random_seed.obj) : error LNK2001: 无法解析的外部符号 __imp_abort 10>postprocess.obj : error LNK2001: 无法解析的外部符号 __imp_abort 10>LIBMY_sta…...

打造智能扩容新纪元:Kubernetes Custom Metrics深度解析

自定义指标:Kubernetes Auto Scaling的革命 1. 引言 1.1 Kubernetes与Auto Scaling Kubernetes作为当今容器编排的事实标准,提供了强大的自动化能力,其中Auto Scaling(自动扩缩容)是其核心特性之一。Auto Scaling允许Kubernetes集群根据当前负载动态调整资源,以应对不…...