Spring Boot 微服务架构设计与实现

Spring Boot 微服务架构设计与实现

Spring Boot 微服务架构设计与实现

在这里插入图片描述
25.1 学习目标与重点提示

学习目标:掌握Spring Boot微服务架构设计与实现的核心概念与使用方法,包括微服务架构的定义与特点、Spring Boot与微服务的集成、Spring Boot与微服务的配置、Spring Boot与微服务的基本方法、Spring Boot的实际应用场景,学会在实际开发中处理微服务架构设计与实现问题。
重点:微服务架构的定义与特点Spring Boot与微服务的集成Spring Boot与微服务的配置Spring Boot与微服务的基本方法Spring Boot的实际应用场景

25.2 微服务架构概述

微服务架构是Java开发中的重要组件。

25.2.1 微服务架构的定义

定义:微服务架构是一种软件架构风格,将应用程序拆分为一组独立的服务,每个服务运行在自己的进程中,通过网络进行通信。
作用

  • 提高应用程序的可扩展性。
  • 提高应用程序的可维护性。
  • 提高应用程序的可靠性。

常见的微服务架构

  • Spring Cloud:Spring Cloud是Spring Boot提供的微服务框架。
  • Netflix OSS:Netflix OSS是Netflix提供的微服务框架。
  • Docker:Docker是一种容器化技术,用于打包和部署应用程序。
  • Kubernetes:Kubernetes是一种容器编排工具,用于管理和调度应用程序。

✅ 结论:微服务架构是一种软件架构风格,作用是提高应用程序的可扩展性、可维护性、可靠性。

25.2.2 微服务架构的特点

定义:微服务架构的特点是指微服务架构的特性。
特点

  • 独立部署:每个服务可以独立部署。
  • 独立开发:每个服务可以独立开发。
  • 独立运行:每个服务运行在自己的进程中。
  • 网络通信:每个服务通过网络进行通信。

✅ 结论:微服务架构的特点包括独立部署、独立开发、独立运行、网络通信。

25.3 Spring Boot与微服务的集成

Spring Boot与微服务的集成是Java开发中的重要内容。

25.3.1 集成Spring Cloud Eureka的步骤

定义:集成Spring Cloud Eureka的步骤是指使用Spring Boot与Spring Cloud Eureka集成的方法。
步骤

  1. 创建Spring Boot项目。
  2. 添加所需的依赖。
  3. 配置Spring Cloud Eureka。
  4. 创建服务提供者。
  5. 创建服务消费者。
  6. 测试应用。

示例
服务注册中心(Eureka Server)的pom.xml文件中的依赖:

<dependencies><!-- Eureka Server依赖 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><!-- 测试依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Hoxton.SR12</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>

服务注册中心(Eureka Server)的application.properties文件中的配置:

# 服务器端口 server.port=8761 # Eureka Server配置 eureka.client.register-with-eureka=false eureka.client.fetch-registry=false eureka.instance.hostname=localhost 

服务注册中心(Eureka Server)的启动类:

importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication@EnableEurekaServerpublicclassEurekaServerApplication{publicstaticvoidmain(String[] args){SpringApplication.run(EurekaServerApplication.class, args);}}

服务提供者(Product Service)的pom.xml文件中的依赖:

<dependencies><!-- Web依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Eureka Client依赖 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><!-- 测试依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Hoxton.SR12</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>

服务提供者(Product Service)的application.properties文件中的配置:

# 服务器端口 server.port=8081 # 应用名称 spring.application.name=product-service # Eureka Client配置 eureka.client.service-url.defaultZone=http://localhost:8761/eureka/ eureka.instance.prefer-ip-address=true 

服务提供者(Product Service)的实体类:

publicclassProduct{privateLong id;privateString productId;privateString productName;privatedouble price;privateint sales;publicProduct(){}publicProduct(Long id,String productId,String productName,double price,int sales){this.id = id;this.productId = productId;this.productName = productName;this.price = price;this.sales = sales;}// Getter和Setter方法publicLonggetId(){return id;}publicvoidsetId(Long id){this.id = id;}publicStringgetProductId(){return productId;}publicvoidsetProductId(String productId){this.productId = productId;}publicStringgetProductName(){return productName;}publicvoidsetProductName(String productName){this.productName = productName;}publicdoublegetPrice(){return price;}publicvoidsetPrice(double price){this.price = price;}publicintgetSales(){return sales;}publicvoidsetSales(int sales){this.sales = sales;}@OverridepublicStringtoString(){return"Product{"+"id="+ id +",+ productId +'\''+",+ productName +'\''+", price="+ price +", sales="+ sales +'}';}}

服务提供者(Product Service)的控制器类:

importorg.springframework.web.bind.annotation.*;importjava.util.ArrayList;importjava.util.List;importjava.util.stream.Collectors;@RestController@RequestMapping("/api/products")publicclassProductController{privateList<Product> products =newArrayList<>();publicProductController(){ products.add(newProduct(1L,"P001","手机",1000.0,100)); products.add(newProduct(2L,"P002","电脑",5000.0,50)); products.add(newProduct(3L,"P003","电视",3000.0,80)); products.add(newProduct(4L,"P004","手表",500.0,200)); products.add(newProduct(5L,"P005","耳机",300.0,150));}@GetMapping("/")publicList<Product>getAllProducts(){return products;}@GetMapping("/{id}")publicProductgetProductById(@PathVariableLong id){return products.stream().filter(product -> product.getId().equals(id)).findFirst().orElse(null);}@PostMapping("/")publicProductaddProduct(@RequestBodyProduct product){ product.setId((long)(products.size()+1)); products.add(product);return product;}@PutMapping("/{id}")publicProductupdateProduct(@PathVariableLong id,@RequestBodyProduct product){Product existingProduct =getProductById(id);if(existingProduct !=null){ existingProduct.setProductId(product.getProductId()); existingProduct.setProductName(product.getProductName()); existingProduct.setPrice(product.getPrice()); existingProduct.setSales(product.getSales());}return existingProduct;}@DeleteMapping("/{id}")publicvoiddeleteProduct(@PathVariableLong id){ products.removeIf(product -> product.getId().equals(id));}}

服务提供者(Product Service)的启动类:

importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication@EnableEurekaClientpublicclassProductServiceApplication{publicstaticvoidmain(String[] args){SpringApplication.run(ProductServiceApplication.class, args);}}

服务消费者(Order Service)的pom.xml文件中的依赖:

<dependencies><!-- Web依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Eureka Client依赖 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><!-- Ribbon依赖 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-ribbon</artifactId></dependency><!-- 测试依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Hoxton.SR12</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>

服务消费者(Order Service)的application.properties文件中的配置:

# 服务器端口 server.port=8082 # 应用名称 spring.application.name=order-service # Eureka Client配置 eureka.client.service-url.defaultZone=http://localhost:8761/eureka/ eureka.instance.prefer-ip-address=true 

服务消费者(Order Service)的实体类:

publicclassProduct{privateLong id;privateString productId;privateString productName;privatedouble price;privateint sales;publicProduct(){}publicProduct(Long id,String productId,String productName,double price,int sales){this.id = id;this.productId = productId;this.productName = productName;this.price = price;this.sales = sales;}// Getter和Setter方法publicLonggetId(){return id;}publicvoidsetId(Long id){this.id = id;}publicStringgetProductId(){return productId;}publicvoidsetProductId(String productId){this.productId = productId;}publicStringgetProductName(){return productName;}publicvoidsetProductName(String productName){this.productName = productName;}publicdoublegetPrice(){return price;}publicvoidsetPrice(double price){this.price = price;}publicintgetSales(){return sales;}publicvoidsetSales(int sales){this.sales = sales;}@OverridepublicStringtoString(){return"Product{"+"id="+ id +",+ productId +'\''+",+ productName +'\''+", price="+ price +", sales="+ sales +'}';}}publicclassOrder{privateLong id;privateString orderId;privateList<Product> products;publicOrder(){}publicOrder(Long id,String orderId,List<Product> products){this.id = id;this.orderId = orderId;this.products = products;}// Getter和Setter方法publicLonggetId(){return id;}publicvoidsetId(Long id){this.id = id;}publicStringgetOrderId(){return orderId;}publicvoidsetOrderId(String orderId){this.orderId = orderId;}publicList<Product>getProducts(){return products;}publicvoidsetProducts(List<Product> products){this.products = products;}@OverridepublicStringtoString(){return"Order{"+"id="+ id +",+ orderId +'\''+", products="+ products +'}';}}

服务消费者(Order Service)的控制器类:

importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.cloud.client.loadbalancer.LoadBalanced;importorg.springframework.context.annotation.Bean;importorg.springframework.http.ResponseEntity;importorg.springframework.web.bind.annotation.*;importorg.springframework.web.client.RestTemplate;importjava.util.ArrayList;importjava.util.List;importjava.util.stream.Collectors;@RestController@RequestMapping("/api/orders")publicclassOrderController{@AutowiredprivateRestTemplate restTemplate;privateList<Order> orders =newArrayList<>();publicOrderController(){ orders.add(newOrder(1L,"O001",newArrayList<>())); orders.add(newOrder(2L,"O002",newArrayList<>())); orders.add(newOrder(3L,"O003",newArrayList<>()));}@Bean@LoadBalancedpublicRestTemplaterestTemplate(){returnnewRestTemplate();}@GetMapping("/")publicList<Order>getAllOrders(){return orders;}@GetMapping("/{id}")publicOrdergetOrderById(@PathVariableLong id){return orders.stream().filter(order -> order.getId().equals(id)).findFirst().orElse(null);}@PostMapping("/")publicOrderaddOrder(@RequestBodyOrder order){ order.setId((long)(orders.size()+1)); orders.add(order);return order;}@PutMapping("/{id}")publicOrderupdateOrder(@PathVariableLong id,@RequestBodyOrder order){Order existingOrder =getOrderById(id);if(existingOrder !=null){ existingOrder.setOrderId(order.getOrderId()); existingOrder.setProducts(order.getProducts());}return existingOrder;}@DeleteMapping("/{id}")publicvoiddeleteOrder(@PathVariableLong id){ orders.removeIf(order -> order.getId().equals(id));}@GetMapping("/{id}/products")publicList<Product>getOrderProducts(@PathVariableLong id){Order order =getOrderById(id);if(order !=null){List<Product> products = order.getProducts();return products;}returnnewArrayList<>();}@PostMapping("/{id}/products")publicOrderaddOrderProduct(@PathVariableLong id,@RequestBodyProduct product){Order order =getOrderById(id);if(order !=null){ order.getProducts().add(product);}return order;}@DeleteMapping("/{id}/products/{productId}")publicOrderdeleteOrderProduct(@PathVariableLong id,@PathVariableLong productId){Order order =getOrderById(id);if(order !=null){ order.getProducts().removeIf(product -> product.getId().equals(productId));}return order;}@GetMapping("/{id}/product/{productId}")publicProductgetProductById(@PathVariableLong id,@PathVariableLong productId){Order order =getOrderById(id);if(order !=null){List<Product> products = order.getProducts();return products.stream().filter(product -> product.getId().equals(productId)).findFirst().orElse(null);}returnnull;}}

服务消费者(Order Service)的启动类:

importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication@EnableEurekaClientpublicclassOrderServiceApplication{publicstaticvoidmain(String[] args){SpringApplication.run(OrderServiceApplication.class, args);}}

测试类:

importorg.junit.jupiter.api.Test;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.boot.test.web.client.TestRestTemplate;importorg.springframework.boot.web.server.LocalServerPort;importorg.springframework.http.HttpEntity;importorg.springframework.http.HttpHeaders;importorg.springframework.http.HttpMethod;importorg.springframework.http.ResponseEntity;importjava.util.ArrayList;importjava.util.List;importstaticorg.assertj.core.api.Assertions.assertThat;@SpringBootTest(webEnvironment =SpringBootTest.WebEnvironment.RANDOM_PORT)classOrderServiceApplicationTests{@LocalServerPortprivateint port;@AutowiredprivateTestRestTemplate restTemplate;@TestvoidcontextLoads(){}@TestvoidtestGetAllOrders(){List<Order> orders = restTemplate.getForObject("http://localhost:"+ port +"/api/orders/",List.class);assertThat(orders).isNotNull();assertThat(orders.size()).isGreaterThanOrEqualTo(3);}@TestvoidtestAddOrder(){Order order =newOrder(null,"O004",newArrayList<>());Order savedOrder = restTemplate.postForObject("http://localhost:"+ port +"/api/orders/", order,Order.class);assertThat(savedOrder).isNotNull();assertThat(savedOrder.getOrderId()).isEqualTo("O004");}@TestvoidtestAddOrderProduct(){Product product =newProduct(1L,"P001","手机",1000.0,100);HttpEntity<Product> requestEntity =newHttpEntity<>(product);ResponseEntity<Order> response = restTemplate.exchange("http://localhost:"+ port +"/api/orders/1/products",HttpMethod.POST, requestEntity,Order.class);assertThat(response.getStatusCodeValue()).isEqualTo(200);assertThat(response.getBody()).isNotNull();assertThat(response.getBody().getProducts().size()).isGreaterThanOrEqualTo(1);}}

✅ 结论:集成Spring Cloud Eureka的步骤包括创建Spring Boot项目、添加所需的依赖、配置Spring Cloud Eureka、创建服务提供者、创建服务消费者、测试应用。

25.4 Spring Boot与微服务的配置

Spring Boot与微服务的配置是Java开发中的重要内容。

25.4.1 配置Spring Cloud Config

定义:配置Spring Cloud Config是指使用Spring Boot与Spring Cloud Config集成的方法。
步骤

  1. 创建Spring Boot项目。
  2. 添加所需的依赖。
  3. 配置Spring Cloud Config。
  4. 创建配置文件。
  5. 测试应用。

示例
配置服务器(Config Server)的pom.xml文件中的依赖:

<dependencies><!-- Config Server依赖 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency><!-- 测试依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Hoxton.SR12</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>

配置服务器(Config Server)的application.properties文件中的配置:

# 服务器端口 server.port=8888 # 配置服务器配置 spring.cloud.config.server.git.uri=https://github.com/username/config-repo spring.cloud.config.server.git.search-paths=config-repo spring.cloud.config.server.git.username=username spring.cloud.config.server.git.password=password 

配置服务器(Config Server)的启动类:

importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.config.server.EnableConfigServer;@SpringBootApplication@EnableConfigServerpublicclassConfigServerApplication{publicstaticvoidmain(String[] args){SpringApplication.run(ConfigServerApplication.class, args);}}

配置客户端(Product Service)的pom.xml文件中的依赖:

<dependencies><!-- Web依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Config Client依赖 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><!-- 测试依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Hoxton.SR12</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>

配置客户端(Product Service)的bootstrap.properties文件中的配置:

# 应用名称 spring.application.name=product-service # 配置服务器地址 spring.cloud.config.uri=http://localhost:8888 

配置客户端(Product Service)的application.properties文件中的配置:

# 服务器端口 server.port=8081 

配置文件(product-service-dev.properties):

# 应用名称 spring.application.name=product-service # 服务器端口 server.port=8081 # 数据库连接信息 spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driver-class-name=org.h2.Driver spring.datasource.username=sa spring.datasource.password=password # JPA配置 spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true # H2数据库控制台 spring.h2.console.enabled=true spring.h2.console.path=/h2-console 

✅ 结论:配置Spring Cloud Config是指使用Spring Boot与Spring Cloud Config集成的方法,步骤包括创建Spring Boot项目、添加所需的依赖、配置Spring Cloud Config、创建配置文件、测试应用。

25.5 Spring Boot与微服务的基本方法

Spring Boot与微服务的基本方法包括使用Ribbon、使用Feign、使用Hystrix。

25.5.1 使用Ribbon

定义:使用Ribbon是指Spring Boot与微服务集成的基本方法之一。
作用

  • 实现服务间的通信。
  • 提高应用程序的性能。

示例
服务消费者(Order Service)的控制器类:

importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.cloud.client.loadbalancer.LoadBalanced;importorg.springframework.context.annotation.Bean;importorg.springframework.http.ResponseEntity;importorg.springframework.web.bind.annotation.*;importorg.springframework.web.client.RestTemplate;importjava.util.ArrayList;importjava.util.List;importjava.util.stream.Collectors;@RestController@RequestMapping("/api/orders")publicclassOrderController{@AutowiredprivateRestTemplate restTemplate;privateList<Order> orders =newArrayList<>();publicOrderController(){ orders.add(newOrder(1L,"O001",newArrayList<>())); orders.add(newOrder(2L,"O002",newArrayList<>())); orders.add(newOrder(3L,"O003",newArrayList<>()));}@Bean@LoadBalancedpublicRestTemplaterestTemplate(){returnnewRestTemplate();}@GetMapping("/")publicList<Order>getAllOrders(){return orders;}@GetMapping("/{id}")publicOrdergetOrderById(@PathVariableLong id){return orders.stream().filter(order -> order.getId().equals(id)).findFirst().orElse(null);}@PostMapping("/")publicOrderaddOrder(@RequestBodyOrder order){ order.setId((long)(orders.size()+1)); orders.add(order);return order;}@PutMapping("/{id}")publicOrderupdateOrder(@PathVariableLong id,@RequestBodyOrder order){Order existingOrder =getOrderById(id);if(existingOrder !=null){ existingOrder.setOrderId(order.getOrderId()); existingOrder.setProducts(order.getProducts());}return existingOrder;}@DeleteMapping("/{id}")publicvoiddeleteOrder(@PathVariableLong id){ orders.removeIf(order -> order.getId().equals(id));}@GetMapping("/{id}/products")publicList<Product>getOrderProducts(@PathVariableLong id){Order order =getOrderById(id);if(order !=null){List<Product> products = order.getProducts();return products;}returnnewArrayList<>();}@PostMapping("/{id}/products")publicOrderaddOrderProduct(@PathVariableLong id,@RequestBodyProduct product){Order order =getOrderById(id);if(order !=null){ order.getProducts().add(product);}return order;}@DeleteMapping("/{id}/products/{productId}")publicOrderdeleteOrderProduct(@PathVariableLong id,@PathVariableLong productId){Order order =getOrderById(id);if(order !=null){ order.getProducts().removeIf(product -> product.getId().equals(productId));}return order;}@GetMapping("/{id}/product/{productId}")publicProductgetProductById(@PathVariableLong id,@PathVariableLong productId){Order order =getOrderById(id);if(order !=null){List<Product> products = order.getProducts();return products.stream().filter(product -> product.getId().equals(productId)).findFirst().orElse(null);}returnnull;}}

✅ 结论:使用Ribbon是指Spring Boot与微服务集成的基本方法之一,作用是实现服务间的通信、提高应用程序的性能。

25.6 Spring Boot的实际应用场景

在实际开发中,Spring Boot微服务架构设计与实现的应用场景非常广泛,如:

  • 实现产品服务的微服务化。
  • 实现用户服务的微服务化。
  • 实现订单服务的微服务化。
  • 实现支付服务的微服务化。

示例

importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.netflix.eureka.EnableEurekaClient;importorg.springframework.web.bind.annotation.*;importorg.springframework.web.client.RestTemplate;importjava.util.ArrayList;importjava.util.List;importjava.util.stream.Collectors;@SpringBootApplication@EnableEurekaClientpublicclassProductServiceApplication{publicstaticvoidmain(String[] args){SpringApplication.run(ProductServiceApplication.class, args);}}@RestController@RequestMapping("/api/products")classProductController{privateList<Product> products =newArrayList<>();publicProductController(){ products.add(newProduct(1L,"P001","手机",1000.0,100)); products.add(newProduct(2L,"P002","电脑",5000.0,50)); products.add(newProduct(3L,"P003","电视",3000.0,80)); products.add(newProduct(4L,"P004","手表",500.0,200)); products.add(newProduct(5L,"P005","耳机",300.0,150));}@GetMapping("/")publicList<Product>getAllProducts(){return products;}@GetMapping("/{id}")publicProductgetProductById(@PathVariableLong id){return products.stream().filter(product -> product.getId().equals(id)).findFirst().orElse(null);}@PostMapping("/")publicProductaddProduct(@RequestBodyProduct product){ product.setId((long)(products.size()+1)); products.add(product);return product;}@PutMapping("/{id}")publicProductupdateProduct(@PathVariableLong id,@RequestBodyProduct product){Product existingProduct =getProductById(id);if(existingProduct !=null){ existingProduct.setProductId(product.getProductId()); existingProduct.setProductName(product.getProductName()); existingProduct.setPrice(product.getPrice()); existingProduct.setSales(product.getSales());}return existingProduct;}@DeleteMapping("/{id}")publicvoiddeleteProduct(@PathVariableLong id){ products.removeIf(product -> product.getId().equals(id));}}@SpringBootApplication@EnableEurekaClientpublicclassOrderServiceApplication{publicstaticvoidmain(String[] args){SpringApplication.run(OrderServiceApplication.class, args);}}@RestController@RequestMapping("/api/orders")classOrderController{@AutowiredprivateRestTemplate restTemplate;privateList<Order> orders =newArrayList<>();publicOrderController(){ orders.add(newOrder(1L,"O001",newArrayList<>())); orders.add(newOrder(2L,"O002",newArrayList<>())); orders.add(newOrder(3L,"O003",newArrayList<>()));}@Bean@LoadBalancedpublicRestTemplaterestTemplate(){returnnewRestTemplate();}@GetMapping("/")publicList<Order>getAllOrders(){return orders;}@GetMapping("/{id}")publicOrdergetOrderById(@PathVariableLong id){return orders.stream().filter(order -> order.getId().equals(id)).findFirst().orElse(null);}@PostMapping("/")publicOrderaddOrder(@RequestBodyOrder order){ order.setId((long)(orders.size()+1)); orders.add(order);return order;}@PutMapping("/{id}")publicOrderupdateOrder(@PathVariableLong id,@RequestBodyOrder order){Order existingOrder =getOrderById(id);if(existingOrder !=null){ existingOrder.setOrderId(order.getOrderId()); existingOrder.setProducts(order.getProducts());}return existingOrder;}@DeleteMapping("/{id}")publicvoiddeleteOrder(@PathVariableLong id){ orders.removeIf(order -> order.getId().equals(id));}@GetMapping("/{id}/products")publicList<Product>getOrderProducts(@PathVariableLong id){Order order =getOrderById(id);if(order !=null){List<Product> products = order.getProducts();return products;}returnnewArrayList<>();}@PostMapping("/{id}/products")publicOrderaddOrderProduct(@PathVariableLong id,@RequestBodyProduct product){Order order =getOrderById(id);if(order !=null){ order.getProducts().add(product);}return order;}@DeleteMapping("/{id}/products/{productId}")publicOrderdeleteOrderProduct(@PathVariableLong id,@PathVariableLong productId){Order order =getOrderById(id);if(order !=null){ order.getProducts().removeIf(product -> product.getId().equals(productId));}return order;}@GetMapping("/{id}/product/{productId}")publicProductgetProductById(@PathVariableLong id,@PathVariableLong productId){Order order =getOrderById(id);if(order !=null){List<Product> products = order.getProducts();return products.stream().filter(product -> product.getId().equals(productId)).findFirst().orElse(null);}returnnull;}}// 测试类@SpringBootTest(webEnvironment =SpringBootTest.WebEnvironment.RANDOM_PORT)classOrderServiceApplicationTests{@LocalServerPortprivateint port;@AutowiredprivateTestRestTemplate restTemplate;@TestvoidcontextLoads(){}@TestvoidtestGetAllOrders(){List<Order> orders = restTemplate.getForObject("http://localhost:"+ port +"/api/orders/",List.class);assertThat(orders).isNotNull();assertThat(orders.size()).isGreaterThanOrEqualTo(3);}@TestvoidtestAddOrder(){Order order =newOrder(null,"O004",newArrayList<>());Order savedOrder = restTemplate.postForObject("http://localhost:"+ port +"/api/orders/", order,Order.class);assertThat(savedOrder).isNotNull();assertThat(savedOrder.getOrderId()).isEqualTo("O004");}@TestvoidtestAddOrderProduct(){Product product =newProduct(1L,"P001","手机",1000.0,100);HttpEntity<Product> requestEntity =newHttpEntity<>(product);ResponseEntity<Order> response = restTemplate.exchange("http://localhost:"+ port +"/api/orders/1/products",HttpMethod.POST, requestEntity,Order.class);assertThat(response.getStatusCodeValue()).isEqualTo(200);assertThat(response.getBody()).isNotNull();assertThat(response.getBody().getProducts().size()).isGreaterThanOrEqualTo(1);}}

输出结果

  • 访问http://localhost:8082/api/orders/:返回所有订单信息。
  • 访问http://localhost:8082/api/orders/1/products:返回订单1的产品信息。

✅ 结论:在实际开发中,Spring Boot微服务架构设计与实现的应用场景非常广泛,需要根据实际问题选择合适的微服务架构和工具。

总结

本章我们学习了Spring Boot微服务架构设计与实现,包括微服务架构的定义与特点、Spring Boot与微服务的集成、Spring Boot与微服务的配置、Spring Boot与微服务的基本方法、Spring Boot的实际应用场景,学会了在实际开发中处理微服务架构设计与实现问题。其中,微服务架构的定义与特点、Spring Boot与微服务的集成、Spring Boot与微服务的配置、Spring Boot与微服务的基本方法、Spring Boot的实际应用场景是本章的重点内容。从下一章开始,我们将学习Spring Boot的其他组件、微服务等内容。

Read more

Spring Boot 实战:MyBatis 操作数据库(上)

Spring Boot 实战:MyBatis 操作数据库(上)

—JavaEE专栏— Spring Boot 实战:MyBatis 操作数据库(上) 摘要 本文深度解析了 Spring Boot 环境下 MyBatis 的集成与应用。通过回顾传统 JDBC 的局限性,详细展示了 MyBatis 在日志配置、CRUD 操作、自增主键返回及多表查询中的实战用法。同时,文章深入探讨了 #{} 与 ${} 的底层预编译差异及安全风险,并分享了企业级开发中的数据库命名规范与 Druid 连接池配置,助力开发者构建稳健的持久层架构。 文章目录 * Spring Boot 实战:MyBatis 操作数据库(上) * 摘要 * @[toc] * 1. 为什么持久层开发需要 MyBatis? * 1.1 传统 JDBC 的局限性 * 1.2

By Ne0inhk
AGV调度系统(RCS)详解:架构、功能与核心调度算法

AGV调度系统(RCS)详解:架构、功能与核心调度算法

1. 系统概述 AGV调度系统(RCS)是一个基于B/S架构的智能调度平台,采用前后端分离设计,后端使用Java SpringBoot框架,前端使用Vue.js,数据库为MySQL 5.7,缓存使用Redis。系统支持多种导航方式,包括二维码导航、激光SLAM和视觉SLAM,适用于各类工业自动化场景。 2. 系统架构与技术栈 * 后端:SpringBoot + Java * 前端:Vue.js * 数据库:MySQL 5.7 * 缓存:Redis * 导航方式:二维码、激光SLAM、视觉SLAM * 部署架构:前后端分离,支持高并发调度 3. 核心功能模块 3.1 地图实时监控 * 初始化定位:将AGV绑定到地图指定点位 * 门禁线设置:联动外部自动门、显示/

By Ne0inhk
【MySQL】基本查询(第二弹)

【MySQL】基本查询(第二弹)

文章目录 * 1.筛选分页结果 * 2.Update * 3.Delete * 4.截断表 * 5.插入查询结果 * 6.聚合函数 * 6.1什么是聚合函数 * 6.2常见的聚合函数 * 7.group by * 7.1如何显示每个部门的平均工资和最高工资 * 7.2显示每个部门的每种岗位的平均工资和最低工资 * 7.3显示平均工资低于2000的部门和它的平均工资 * 8.总结 1.筛选分页结果 什么是筛选分页结果呢,我们都知道当我们在看小说或者很长的文章的时候,都会分成很多页,在未来数据库处理当中也会面临着处理很多数据,如果直接将所有数据显示出来,会使得负载过高,所以分页或者条件筛选是避不开的话题,所以接下来我们就来学习一下什么是筛选分页结果。 select*from student limit0,3; 上面这种写法表示从哪个位置开始显示多少行数据,注意:表的开始是0好位置,而不是1号位置。 limit不仅可以这样用,还可以通过加上其他的条件达到自己想要的效果,比如order

By Ne0inhk
使用IntelliJ IDEA部署Tomcat本地环境运行Servlet(2025最新版兼容)

使用IntelliJ IDEA部署Tomcat本地环境运行Servlet(2025最新版兼容)

下载部署细节较多可以收藏以防遗忘❤ 1.首先安装好IntelliJ IDEA2025的最新版,各种地方有免激活的方式,大家自行探索即可,注意版本为ultimate edition,如下图。提供一个下载地址(https://www.jetbrains.com/idea/download/#section=windows,看大家需求我会出教学再) 收藏关注后私我发你安装文档也可以。 2.其次在电脑中安装好jdk版本,个人推荐jdk1.8比较稳定,也就是常说的jdk8版本(我后续还会出教程如何安装)安装包jdk-8u341-windows-x64.exe。 需要的收藏关注后私我发你安装包也可以。 示例中我已汉化IJ,若需要可以自行搜索社区教程。 3.选择上图的“New Project”,出现下列界面,填写下列选项后,选择“Create” 创建一个新项目; Name:项目名称,可根据自己的需要填写; Location:项目保存位置,可根据自己的需要选择; Language:编程语言,默认为Java; Bulid

By Ne0inhk