深入讲解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模型具有更强的对话能力,并且可以通过微调来…...
零门槛NAS搭建:WinNAS如何让普通电脑秒变私有云?
一、核心优势:专为Windows用户设计的极简NAS WinNAS由深圳耘想存储科技开发,是一款收费低廉但功能全面的Windows NAS工具,主打“无学习成本部署” 。与其他NAS软件相比,其优势在于: 无需硬件改造:将任意W…...
数据库分批入库
今天在工作中,遇到一个问题,就是分批查询的时候,由于批次过大导致出现了一些问题,一下是问题描述和解决方案: 示例: // 假设已有数据列表 dataList 和 PreparedStatement pstmt int batchSize 1000; // …...

图表类系列各种样式PPT模版分享
图标图表系列PPT模版,柱状图PPT模版,线状图PPT模版,折线图PPT模版,饼状图PPT模版,雷达图PPT模版,树状图PPT模版 图表类系列各种样式PPT模版分享:图表系列PPT模板https://pan.quark.cn/s/20d40aa…...
Spring AI与Spring Modulith核心技术解析
Spring AI核心架构解析 Spring AI(https://spring.io/projects/spring-ai)作为Spring生态中的AI集成框架,其核心设计理念是通过模块化架构降低AI应用的开发复杂度。与Python生态中的LangChain/LlamaIndex等工具类似,但特别为多语…...
大数据学习(132)-HIve数据分析
🍋🍋大数据学习🍋🍋 🔥系列专栏: 👑哲学语录: 用力所能及,改变世界。 💖如果觉得博主的文章还不错的话,请点赞👍收藏⭐️留言Ǵ…...
今日学习:Spring线程池|并发修改异常|链路丢失|登录续期|VIP过期策略|数值类缓存
文章目录 优雅版线程池ThreadPoolTaskExecutor和ThreadPoolTaskExecutor的装饰器并发修改异常并发修改异常简介实现机制设计原因及意义 使用线程池造成的链路丢失问题线程池导致的链路丢失问题发生原因 常见解决方法更好的解决方法设计精妙之处 登录续期登录续期常见实现方式特…...
docker 部署发现spring.profiles.active 问题
报错: org.springframework.boot.context.config.InvalidConfigDataPropertyException: Property spring.profiles.active imported from location class path resource [application-test.yml] is invalid in a profile specific resource [origin: class path re…...
代码随想录刷题day30
1、零钱兑换II 给你一个整数数组 coins 表示不同面额的硬币,另给一个整数 amount 表示总金额。 请你计算并返回可以凑成总金额的硬币组合数。如果任何硬币组合都无法凑出总金额,返回 0 。 假设每一种面额的硬币有无限个。 题目数据保证结果符合 32 位带…...

AI+无人机如何守护濒危物种?YOLOv8实现95%精准识别
【导读】 野生动物监测在理解和保护生态系统中发挥着至关重要的作用。然而,传统的野生动物观察方法往往耗时耗力、成本高昂且范围有限。无人机的出现为野生动物监测提供了有前景的替代方案,能够实现大范围覆盖并远程采集数据。尽管具备这些优势…...

搭建DNS域名解析服务器(正向解析资源文件)
正向解析资源文件 1)准备工作 服务端及客户端都关闭安全软件 [rootlocalhost ~]# systemctl stop firewalld [rootlocalhost ~]# setenforce 0 2)服务端安装软件:bind 1.配置yum源 [rootlocalhost ~]# cat /etc/yum.repos.d/base.repo [Base…...