远程调用基本实现

This commit is contained in:
bunny 2025-05-26 10:08:40 +08:00
parent 89c14ddeeb
commit a390e31477
16 changed files with 379 additions and 8 deletions

26
cloud-demo/model/pom.xml Normal file
View File

@ -0,0 +1,26 @@
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
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>cn.bunny.mq</groupId>
<artifactId>cloud-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>model</artifactId>
<packaging>jar</packaging>
<name>model</name>
<url>https://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,24 @@
package cn.bunny.model.order.bean;
import cn.bunny.model.product.bean.Product;
import lombok.Data;
import java.math.BigDecimal;
import java.util.List;
@Data
public class Order {
private Long id;
private BigDecimal totalAmount;
private Long userId;
private String nickName;
private String address;
private List<Product> productList;
}

View File

@ -0,0 +1,18 @@
package cn.bunny.model.product.bean;
import lombok.Data;
import java.math.BigDecimal;
@Data
public class Product {
private Long id;
private BigDecimal price;
private String productName;
private int num;
}

View File

@ -16,6 +16,7 @@
<description>cloud-demo</description>
<modules>
<module>services</module>
<module>model</module>
</modules>
<packaging>pom</packaging>
@ -24,21 +25,17 @@
<junit.version>3.8.1</junit.version>
<knife4j.version>4.5.0</knife4j.version>
<fastjson2.version>2.0.47</fastjson2.version>
<lombok.version>1.18.32</lombok.version>
<knife4j.version>4.5.0</knife4j.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<version>${lombok.version}</version>
</dependency>
<!-- test -->
@ -48,7 +45,7 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- spring-cloud-alibaba -->
<dependency>
<groupId>com.alibaba.cloud</groupId>

View File

@ -22,11 +22,22 @@
</properties>
<dependencies>
<dependency>
<groupId>cn.bunny.mq</groupId>
<artifactId>model</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- devtools -->
<dependency>
@ -58,5 +69,20 @@
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- knife4j -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
</dependency>
<!-- <dependency> -->
<!-- <groupId>org.springdoc</groupId> -->
<!-- <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> -->
<!-- <version>2.8.6</version> -->
<!-- </dependency> -->
<!-- <dependency> -->
<!-- <groupId>io.swagger</groupId> -->
<!-- <artifactId>swagger-annotations</artifactId> -->
<!-- <version>1.6.14</version> -->
<!-- </dependency> -->
</dependencies>
</project>

View File

@ -0,0 +1,38 @@
package cn.bunny.service.config;
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@Slf4j
public class Knife4jConfig {
@Bean
public OpenAPI openAPI() {
// 作者等信息
Contact contact = new Contact().name("Bunny").email("1319900154@qq.com").url("http://bunny-web.site");
// 使用协议
License license = new License().name("MIT").url("https://mit-license.org");
// 相关信息
Info info = new Info().title("Bunny-Admin")
.contact(contact).license(license)
.description("微服务")
.summary("微服务教程")
.termsOfService("http://bunny-web.site")
.version("v0.0.1");
return new OpenAPI().info(info).externalDocs(new ExternalDocumentation());
}
@Bean
public GroupedOpenApi all() {
return GroupedOpenApi.builder().group("全部请求接口").pathsToMatch("/api/**").build();
}
}

View File

@ -0,0 +1,15 @@
package cn.bunny.service.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class OrderServiceConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}

View File

@ -0,0 +1,13 @@
package cn.bunny.service.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class IndexController {
@GetMapping("")
public String index() {
return "index";
}
}

View File

@ -0,0 +1,23 @@
package cn.bunny.service.controller;
import cn.bunny.model.order.bean.Order;
import cn.bunny.service.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/order")
@RequiredArgsConstructor
public class OrderController {
private final OrderService orderService;
@Operation(summary = "创建订单")
@GetMapping("create")
public Order createOrder(Long userId, Long productId) {
return orderService.createOrder(productId, userId);
}
}

View File

@ -0,0 +1,15 @@
package cn.bunny.service.service;
import cn.bunny.model.order.bean.Order;
public interface OrderService {
/**
* 创建订单信息
*
* @param productId 订单信息
* @param userId 用户id
* @return 订单内容
*/
Order createOrder(Long productId, Long userId);
}

View File

@ -0,0 +1,60 @@
package cn.bunny.service.service.impl;
import cn.bunny.model.order.bean.Order;
import cn.bunny.model.product.bean.Product;
import cn.bunny.service.service.OrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.math.BigDecimal;
import java.util.List;
@Slf4j
@Service
@RequiredArgsConstructor
public class OrderServiceImpl implements OrderService {
private final DiscoveryClient discoveryClient;
private final RestTemplate restTemplate;
/**
* 创建订单信息
*
* @param productId 订单信息
* @param userId 用户id
* @return 订单内容
*/
@Override
public Order createOrder(Long productId, Long userId) {
Product product = getProductFromRemote(productId);
Order order = new Order();
order.setId(1L);
// 查询总金额
BigDecimal amount = product.getPrice().multiply(new BigDecimal(product.getNum()));
order.setTotalAmount(amount);
order.setNickName("在资质");
order.setAddress("地址地址。。。");
// TODO 远程查询
order.setProductList(List.of(product));
return order;
}
private Product getProductFromRemote(Long productId) {
// 获取商品服务所有及其的 IP+port
List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
ServiceInstance instance = instances.get(0);
// 远程URL
String url = "http://" + instance.getHost() + ":" + instance.getPort() + "/api/product/" + productId;
// 2. 远程发送请求
log.info("远程调用:{}", url);
return restTemplate.getForObject(url, Product.class);
}
}

View File

@ -0,0 +1,38 @@
package cn.bunny.service.config;
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@Slf4j
public class Knife4jConfig {
@Bean
public OpenAPI openAPI() {
// 作者等信息
Contact contact = new Contact().name("Bunny").email("1319900154@qq.com").url("http://bunny-web.site");
// 使用协议
License license = new License().name("MIT").url("https://mit-license.org");
// 相关信息
Info info = new Info().title("Bunny-Admin")
.contact(contact).license(license)
.description("微服务")
.summary("微服务教程")
.termsOfService("http://bunny-web.site")
.version("v0.0.1");
return new OpenAPI().info(info).externalDocs(new ExternalDocumentation());
}
@Bean
public GroupedOpenApi all() {
return GroupedOpenApi.builder().group("全部请求接口").pathsToMatch("/api/**").build();
}
}

View File

@ -0,0 +1,15 @@
package cn.bunny.service.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class ProductServiceConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}

View File

@ -0,0 +1,24 @@
package cn.bunny.service.controller;
import cn.bunny.model.product.bean.Product;
import cn.bunny.service.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/product")
@RequiredArgsConstructor
public class ProductController {
private final ProductService productService;
@Operation(summary = "根据id查询商品")
@GetMapping("{id}")
public Product getProduct(@PathVariable("id") Long productId) {
return productService.getProductById(productId);
}
}

View File

@ -0,0 +1,13 @@
package cn.bunny.service.service;
import cn.bunny.model.product.bean.Product;
public interface ProductService {
/**
* 根据id获取商品
*
* @param productId 商品id
* @return 商品内容
*/
Product getProductById(Long productId);
}

View File

@ -0,0 +1,26 @@
package cn.bunny.service.service.impl;
import cn.bunny.model.product.bean.Product;
import cn.bunny.service.service.ProductService;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
@Service
public class ProductServiceImpl implements ProductService {
/**
* 根据id获取商品
*
* @param productId 商品id
* @return 商品内容
*/
@Override
public Product getProductById(Long productId) {
Product product = new Product();
product.setId(productId);
product.setPrice(new BigDecimal("10"));
product.setProductName("xxx");
product.setNum(2);
return product;
}
}