代码提交

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,34 @@
<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>config-server</name>
<artifactId>config-server</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</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,19 @@
package com.mangmang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* 配置中心
* 功能:统一管理各服务的配置,支持从 Git 仓库中读取配置文件
*/
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}

View File

@@ -0,0 +1,27 @@
# 服务器配置
server:
# 服务器端口号设置为8888
port: 8888
# Spring应用配置
spring:
application:
# 应用名称设置为config-server
name: config-server
cloud:
config:
server:
git:
# Git仓库的URI用于存储配置文件
uri: https://github.com/liujing33/MangMang
# 默认的Git分支设置为main
default-label: main
# 搜索路径,使用{application}作为占位符,表示根据应用名称查找配置文件
search-paths: /
# Eureka客户端配置
eureka:
client:
serviceUrl:
# Eureka服务器的默认区域URL设置为本地8761端口
defaultZone: http://localhost:8761/eureka/

View File

@@ -0,0 +1,33 @@
<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>
<artifactId>eureka-server</artifactId>
<packaging>jar</packaging>
<name>eureka-server</name>
<dependencies>
<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-web</artifactId>
</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,17 @@
package com.mangmang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* 服务注册中心
* 功能:用于服务注册和发现,微服务启动时会向 Eureka 注册自己,并通过 Eureka 发现其他服务
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}

View File

@@ -0,0 +1,21 @@
# server: 配置服务器相关的参数
server:
# port: 指定服务器监听的端口号默认值为8761
port: 8761
# spring: 配置Spring应用相关的参数
spring:
application:
# name: 指定Spring应用的名称这里设置为eureka-server
name: eureka-server
# eureka: 配置Eureka服务器和客户端相关的参数
eureka:
client:
# register-with-eureka: 是否将当前实例注册到Eureka服务器false表示不注册
register-with-eureka: false
# fetch-registry: 是否从Eureka服务器获取注册表信息false表示不获取
fetch-registry: false
server:
# wait-time-in-ms-when-sync-empty: 当同步空注册表时的等待时间毫秒0表示不等待
wait-time-in-ms-when-sync-empty: 0

View File

@@ -0,0 +1,46 @@
<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>gateway-service</name>
<artifactId>gateway-service</artifactId>
<packaging>jar</packaging>
<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>
<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.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</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,17 @@
package com.mangmang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* Hello world!
*
*/
@SpringBootApplication
@EnableEurekaClient
public class GatewayServiceApplication {
public static void main( String[] args ) {
SpringApplication.run(GatewayServiceApplication.class, args);
}
}

View File

@@ -0,0 +1,49 @@
# 服务器配置
server:
# 服务器端口号设置为8080
port: 8080
# Eureka客户端配置
eureka:
client:
serviceUrl:
# Eureka服务器的默认区域URL指向本地8761端口的Eureka服务
defaultZone: http://localhost:8761/eureka/
# Spring Cloud配置
spring:
cloud:
gateway:
discovery:
locator:
# 启用服务发现定位器,允许通过服务名称进行路由
enabled: true
routes:
# 用户服务路由配置
- id: user-service
# 使用负载均衡方式指向user-service服务
uri: lb://user-service
predicates:
# 匹配路径为/api/users/**的请求
- Path=/api/users/**
filters:
# 去除路径中的第一个前缀
- StripPrefix=1
# 订单服务路由配置
- id: order-service
# 使用负载均衡方式指向order-service服务
uri: lb://order-service
predicates:
# 匹配路径为/api/orders/**的请求
- Path=/api/orders/**
filters:
# 去除路径中的第一个前缀
- StripPrefix=1
# 管理端点配置
management:
endpoints:
web:
exposure:
# 暴露所有管理端点
include: "*"

View File

@@ -0,0 +1,18 @@
# Spring 应用程序配置
spring:
# 应用程序相关配置
application:
# 应用程序名称,用于标识当前服务
name: gateway-service
# Spring Cloud 相关配置
cloud:
# 配置中心相关配置
config:
# 配置中心服务发现相关配置
discovery:
# 是否启用配置中心服务发现true 表示启用
enabled: true
# 配置中心服务的服务ID用于服务发现
service-id: config-server
# 是否在配置中心不可用时快速失败true 表示快速失败
fail-fast: true

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

34
spring-cloud-demo/pom.xml Normal file
View File

@@ -0,0 +1,34 @@
<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>learning-nexus</artifactId>
<version>1.0.0</version>
</parent>
<name>spring-cloud-demo</name>
<artifactId>spring-cloud-demo</artifactId>
<packaging>pom</packaging>
<modules>
<module>eureka-server</module>
<module>config-server</module>
<module>user-service</module>
<module>order-service</module>
<module>gateway-service</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,53 @@
<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>user-service</name>
<artifactId>user-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.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;
/**
* 用户服务
*/
@SpringBootApplication
@EnableEurekaClient
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args);
}
}

View File

@@ -0,0 +1,43 @@
package com.mangmang.controller;
import com.mangmang.entity.User;
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.Arrays;
import java.util.List;
@RestController
@RequestMapping("/users")
public class UserController {
@Value("${server.port}")
private String port;
private final List<User> users = Arrays.asList(
new User(1L, "张三", "zhangsan@example.com"),
new User(2L, "李四", "lisi@example.com"),
new User(3L, "王五", "wangwu@example.com")
);
@GetMapping
public List<User> getAllUsers() {
return users;
}
@GetMapping("/{id}")
public User getUserById(@PathVariable Long id) {
return users.stream()
.filter(user -> user.getId().equals(id))
.findFirst()
.orElseThrow(() -> new RuntimeException("User not found"));
}
@GetMapping("/port")
public String getPort() {
return "User service running on port: " + port;
}
}

View File

@@ -0,0 +1,14 @@
package com.mangmang.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private Long id;
private String name;
private String email;
}

View File

@@ -0,0 +1,19 @@
# server: 配置服务器相关的设置
server:
# port: 指定服务器监听的端口号默认为8081
port: 8081
# eureka: 配置Eureka客户端相关的设置
eureka:
client:
serviceUrl:
# defaultZone: 指定Eureka服务器的默认注册地址默认为http://localhost:8761/eureka/
defaultZone: http://localhost:8761/eureka/
# management: 配置Spring Boot Actuator相关的设置
management:
endpoints:
web:
exposure:
# include: 指定暴露的Actuator端点*表示暴露所有端点
include: "*"

View File

@@ -0,0 +1,19 @@
# Spring 应用配置
spring:
# 应用名称配置
application:
# 设置应用的名称为 user-service
name: user-service
# Spring Cloud 配置
cloud:
config:
# 配置服务发现相关设置
discovery:
# 启用配置服务发现功能
enabled: true
# 指定配置服务的服务ID为 config-server
service-id: config-server
# 启用快速失败机制,当配置服务不可用时,应用将快速失败而不是等待
fail-fast: true