代码提交

This commit is contained in:
liujing33
2025-05-08 18:02:47 +08:00
parent 55fcc338d7
commit ba04a1047b
60 changed files with 1961 additions and 201 deletions

View File

@@ -0,0 +1,55 @@
<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.mangmang</groupId>
<artifactId>spring-cloud-demo</artifactId>
<version>1.0.0</version>
</parent>
<name>order-service</name>
<artifactId>order-service</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,16 @@
package com.mangmang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class OrderServiceApplication {
public static void main(String[] args) {
SpringApplication.run(OrderServiceApplication.class, args);
}
}

View File

@@ -0,0 +1,13 @@
package com.mangmang.orderservice.client;
import com.mangmang.orderservice.client.model.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient(name = "user-service")
public interface UserClient {
@GetMapping("/users/{id}")
User getUserById(@PathVariable("id") Long id);
}

View File

@@ -0,0 +1,15 @@
package com.mangmang.orderservice.client.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Order {
private Long id;
private Long userId;
private String productName;
private Double price;
}

View File

@@ -0,0 +1,10 @@
package com.mangmang.orderservice.client.model;
import lombok.Data;
@Data
public class User {
private Long id;
private String name;
private String email;
}

View File

@@ -0,0 +1,65 @@
package com.mangmang.orderservice.controller;
import com.mangmang.orderservice.client.UserClient;
import com.mangmang.orderservice.client.model.Order;
import com.mangmang.orderservice.client.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@RestController
@RequestMapping("/orders")
public class OrderController {
@Value("${server.port}")
private String port;
@Autowired
private UserClient userClient;
private List<Order> orders = new ArrayList<>();
public OrderController() {
orders.add(new Order(1L, 1L, "iPhone 13", 5999.99));
orders.add(new Order(2L, 1L, "MacBook Pro", 12999.99));
orders.add(new Order(3L, 2L, "iPad Pro", 4999.99));
}
@GetMapping
public List<Order> getAllOrders() {
return orders;
}
@GetMapping("/{id}")
public Order getOrderById(@PathVariable Long id) {
return orders.stream()
.filter(order -> order.getId().equals(id))
.findFirst()
.orElseThrow(() -> new RuntimeException("Order not found"));
}
@GetMapping("/user/{userId}")
public List<Order> getOrdersByUserId(@PathVariable Long userId) {
// 验证用户是否存在
User user = userClient.getUserById(userId);
if (user == null) {
throw new RuntimeException("User not found");
}
return orders.stream()
.filter(order -> order.getUserId().equals(userId))
.collect(Collectors.toList());
}
@GetMapping("/port")
public String getPort() {
return "Order service running on port: " + port;
}
}

View File

@@ -0,0 +1,19 @@
# server配置块用于定义服务器相关的配置
server:
# 服务器端口号,指定应用程序监听的端口
port: 8082
# eureka配置块用于定义Eureka客户端相关的配置
eureka:
client:
serviceUrl:
# Eureka服务器的默认区域URL指定Eureka服务器的地址
defaultZone: http://localhost:8761/eureka/
# management配置块用于定义管理端点相关的配置
management:
endpoints:
web:
exposure:
# 暴露所有管理端点允许通过Web访问所有管理端点
include: "*"

View File

@@ -0,0 +1,18 @@
# Spring 应用程序配置
spring:
# 应用程序相关配置
application:
# 应用程序名称,用于标识当前服务
name: order-service
# Spring Cloud 相关配置
cloud:
# Spring Cloud Config 配置
config:
# 配置服务发现相关设置
discovery:
# 启用配置服务发现,允许通过服务发现机制查找配置服务器
enabled: true
# 配置服务器的服务ID用于在服务注册中心查找配置服务器
service-id: config-server
# 配置客户端在启动时快速失败,如果无法连接到配置服务器,则应用程序将无法启动
fail-fast: true