深入讲解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的图书管理系统的基本构建过程。每个模块都有详细的代码和注释,希望可以帮助到你。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/62139.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

《气候与环境研究》

《气候与环境研究》是我国在气候和环境学科交叉领域的中文学术期刊&#xff0c;报道气候与环境研究领域的创新性研究成果&#xff0c;注重基础研究和应用研究相结合&#xff0c;为国民经济和社会的可持续发展、保护人类生存环境和防灾减灾服务。覆盖的学科范围有大气科学及海洋…

时间的礼物:如何珍视每一刻

《时间的礼物&#xff1a;如何珍视每一刻》 夫时间者&#xff0c;宇宙之精髓&#xff0c;生命之经纬&#xff0c;悄无声息而流转不息&#xff0c;如织锦之细线&#xff0c;串联古今&#xff0c;贯穿万物。 人生短暂&#xff0c;犹如白驹过隙&#xff0c;倏忽而逝&#xff0c;…

NVIDIA /CUDA 里面的clock rate详细介绍

本文主要介绍&#xff1a; cuda中的时钟频率具体有哪些&#xff1f;clock rate怎么调节&#xff1f; cuda中可以通过nvml 函数或者命令来调整时钟频率&#xff08;clock rate&#xff09; 介绍 命令行 nvdia-smi -q -i 0 可以查询device相关参数&#xff0c;可以用后面的命…

扫振牙刷设计思路以及技术解析

市面上目前常见的就两种&#xff1a;扫振牙刷和超声波牙刷 为了防水&#xff0c;表面还涂上了一层防水漆 一开始的电池管理芯片&#xff0c;可以让充电更加均衡。 如TP4056 第一阶段以恒流充电&#xff1b;当电压达到预定值时转入第二阶段进行恒压充电&#xff0c;此时电流逐…

电磁继电器

它的控制原理很简单&#xff0c;当我们给它的线圈接电&#xff0c;这个线圈就有了磁性&#xff0c;它上面的衔铁就会被吸引&#xff0c;这样小灯泡就会点亮 继电器于MOS管的差别在于&#xff0c;继电器可以很轻松的胜任高电压、大电流的场合 我们从外壳上可以看到 30VDC&#x…

【Jenkins】自动化部署 maven 项目笔记

文章目录 前言1. Jenkins 新增 Maven 项目2. Jenkins 配置 Github 信息3. Jenkins 清理 Workspace4. Jenkins 配置 后置Shell脚本后记 前言 目标&#xff1a;自动化部署自己的github项目 过程&#xff1a;jenkins 配置、 shell 脚本积累 相关连接 Jenkins 官方 docker 指导d…

LangGraph中的State管理

本教程将介绍如何使用LangGraph库构建和测试状态图。我们将通过一系列示例代码&#xff0c;逐步解释程序的运行逻辑。 1. 基本状态图构建 首先&#xff0c;我们定义一个状态图的基本结构和节点。 定义状态类 from langgraph.graph import StateGraph, START, END from typi…

Go-Web之TCP网络连接

函数学习 net.Listen:用于创建一个TCP监听器&#xff0c;在指定地址上监听传入的连接listrener.Accept:这个方法会阻塞&#xff0c;直到有新的客户端连接&#xff0c;当连接到来时&#xff0c;它会返回一个net.Conn类型的连接对象&#xff0c;表示客户端和服务器之间的TCP连接…

Excel的图表使用和导出准备

目的 导出Excel图表是很多软件要求的功能之一&#xff0c;那如何导出Excel图表呢&#xff1f;或者说如何使用Excel图表。 一种方法是软件生成图片&#xff0c;然后把图片写到Excel上&#xff0c;这种方式&#xff0c;因为格式种种原因&#xff0c;导出的图片不漂亮&#xff0c…

Spring WebFlux SSE(服务器发送事件)的正确用法

在SpringBoot2下SSE实现是返回一个SseEmitter,然后通过SseEmitter的send方法来发送事件. 在SpringBoot3的WebFlux 下SSE实现是返回一个Flux<ServerSentEvent<?>>,但是怎么手动向客户端发送SSE事件搜遍全网也没有看到一个讲清楚的.网上的例子一般都是这样的: GetM…

vue实现滚动条滑动到底部分页调取后端接口加载数据

一、案例效果 二、前提条件 接口返回数据 三、案例代码 子组件 const $emit defineEmits([cloneItem, updateList]);const props defineProps({rightList: {type: Array,},chartTableData: {type: Array as () > ChartListType[],},deleteChartInfo: {type: Object,}…

Ubuntu中使用多版本的GCC

我的系统中已经安装了GCC11.4&#xff0c;在安装cuda时出现以下错误提示&#xff1a; 意思是当前的GCC版本过高&#xff0c;要在保留GCC11.4的同时安装GCC9并可以切换&#xff0c;可以通过以下步骤实现&#xff1a; 步骤 1: 安装 GCC 9 sudo apt-get update sudo apt-get ins…

【Android】RecyclerView回收复用机制

概述 RecyclerView 是 Android 中用于高效显示大量数据的视图组件&#xff0c;它是 ListView 的升级版本&#xff0c;支持更灵活的布局和功能。 我们创建一个RecyclerView的Adapter&#xff1a; public class MyRecyclerView extends RecyclerView.Adapter<MyRecyclerVie…

Kotlin DSL Gradle 指南

本文是关于 Kotlin DSL Gradle 的指南&#xff08;上篇&#xff09;&#xff0c;介绍了 Gradle 作为 Android 开发构建工具的作用及优势&#xff0c;包括初始配置、生命周期、依赖管理、Task 相关内容。如 Task 的创建、自定义、各种方法和属性&#xff0c;以及文件操作等&…

数据库导论

data 数据是数据库中存储的基本数据&#xff0c;描述事物的符号称为数据。 DB 数据库是长期存储在计算机内&#xff0c;有组织&#xff0c;可共享的大量数据的集合。数据库中的数据按照一定的数据模型组织&#xff0c;描述和存储&#xff0c;具有较小的冗余度&#xff0c;较…

HTML实现 扫雷游戏

前言&#xff1a; 游戏起源与发展 扫雷游戏的雏形可追溯到 1973 年的 “方块&#xff08;cube&#xff09;” 游戏&#xff0c;后经改编出现了 “rlogic” 游戏&#xff0c;玩家需为指挥中心探出安全路线避开地雷。在此基础上&#xff0c;开发者汤姆・安德森编写出了扫雷游戏的…

浏览器漫谈HTML--2.2从表单标签看vue的响应式系统 理论+实战

表单标签的双向绑定是一个很有亮点的功能。在不同框架中他实现这个功能大同小异&#xff0c;这里我们介绍几个常见的框架中他是如何实现双向绑定的。 原生的input输入框是没有双向绑定的功能的。取而代之的&#xff0c;它的input上有一个event对象&#xff0c;这个对象中有一个…

Python 爬虫 (1)基础 | 目标网站

一、目标网站 1、加密网站 1.1、关键字比较明确 企名片&#xff1a;https://wx.qmpsee.com/articleDetail?idfeef62bfdac45a94b9cd89aed5c235be 1.2、关键字比较泛 烯牛数据&#xff1a;https://www.xiniudata.com/project/event/lib/invest

Spring Boot英语知识网站:开发策略

5系统详细实现 5.1 管理员模块的实现 5.1.1 用户信息管理 英语知识应用网站的系统管理员可以对用户信息添加修改删除以及查询操作。具体界面的展示如图5.1所示。 图5.1 用户信息管理界面 5.1.2 在线学习管理 系统管理员可以对在线学习信息进行添加&#xff0c;修改&#xff0…

堆优化版本的Prim

prim和dijkstra每轮找最小边的松弛操作其实是同源的&#xff0c;因而受dijkstra堆优化的启发&#xff0c;那么prim也可以采用小根堆进行优化。时间复杂度也由 O ( n 2 ) O(n^2) O(n2)降为 O ( n l o g n ) O(nlogn) O(nlogn)。 测试一下吧&#xff1a;原题链接 #include <i…