搭建一个简单的springcloud服务
搭建一个简单的 Spring Cloud 微服务示例,包含服务注册中心和两个微服务。
介绍一个项目结构
springcloud-demo/
├── eureka-server/ # 服务注册中心
├── user-service/ # 用户服务
├── order-service/ # 订单服务
└── pom.xml # 父POM
1,父项目 (springcloud-demo)
pom文件
<?xml version="1.0" encoding="UTF-8"?>
<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-SNAPSHOT</version><packaging>pom</packaging><modules><module>eureka-server</module><module>user-service</module><module>order-service</module></modules><!-- 修正父POM配置 --><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.0</version><relativePath/> <!-- 这行很重要 --></parent><properties><java.version>1.8</java.version><spring-cloud.version>2021.0.8</spring-cloud.version> <!-- 添加Spring Cloud版本 --><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencyManagement><dependencies><!-- 添加Spring Cloud依赖管理 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency><!-- 项目内部模块依赖 --><dependency><groupId>com.example</groupId><artifactId>eureka-server</artifactId><version>${project.version}</version></dependency><dependency><groupId>com.example</groupId><artifactId>user-service</artifactId><version>${project.version}</version></dependency><dependency><groupId>com.example</groupId><artifactId>order-service</artifactId><version>${project.version}</version></dependency></dependencies></dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
</project>
2,服务注册中心 (Eureka Server)
eureka-server/pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>demo</artifactId><version>1.0-SNAPSHOT</version></parent><artifactId>eureka-server</artifactId><packaging>jar</packaging><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency></dependencies>
</project>
eureka-server/src/main/resources/application.yml
server:port: 8761eureka:client:register-with-eureka: false # 不注册自己fetch-registry: false # 不获取注册表service-url:defaultZone: http://localhost:8761/eureka/instance:hostname: localhostspring:application:name: eureka-server
eureka-server/src/main/java/com/example/EurekaServerApplication.java
package com.example;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication
@EnableEurekaServer // 启用 Eureka 服务端
public class EurekaServerApplication {public static void main(String[] args) {SpringApplication.run(EurekaServerApplication.class, args);}
}
3,用户服务 (User Service)
user-service/pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>demo</artifactId><version>1.0-SNAPSHOT</version></parent><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></dependencies>
</project>
user-service/src/main/resources/application.yml
server:port: 8081spring:application:name: user-service # 服务名称eureka:client:service-url:defaultZone: http://localhost:8761/eureka/instance:prefer-ip-address: true
user-service/src/main/java/com/example/UserServiceApplication.java
package com.example;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication
@EnableDiscoveryClient // 启用服务发现
public class UserServiceApplication {public static void main(String[] args) {SpringApplication.run(UserServiceApplication.class, args);}
}
user-service/src/main/java/com/example/controller/UserController.java
package com.example.controller;import org.springframework.web.bind.annotation.*;import java.util.HashMap;
import java.util.Map;@RestController
@RequestMapping("/users")
public class UserController {// 模拟用户数据private Map<Long, Map<String, Object>> users = new HashMap<>();public UserController() {// 初始化测试数据
// users.put(1L, Map.of("id", 1, "name", "张三", "age", 25));
// users.put(2L, Map.of("id", 2, "name", "李四", "age", 30));// 初始化测试数据 - 使用传统方式users = new HashMap<>();Map<String, Object> order1 = new HashMap<>();order1.put("id", 1);order1.put("name", "张三");order1.put("age", 25);users.put(1L, order1);Map<String, Object> order2 = new HashMap<>();order2.put("id", 2);order2.put("name", "李四");order2.put("age", 30);users.put(2L, order2);}@GetMapping("/{id}")public Map<String, Object> getUser(@PathVariable Long id) {Map<String, Object> user = users.get(id);if (user == null) {throw new RuntimeException("用户不存在");}return user;}@GetMapping("/hello")public String hello() {return "Hello from User Service!";}@GetMapping("/")public Map<String, Object> getAllUsers() {Map<String,Object> usersT = new HashMap<>();usersT.put("users",users.values());usersT.put("service","user-service");return usersT;}
}
4,订单服务 (Order Service)
order-service/pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>demo</artifactId><version>1.0-SNAPSHOT</version></parent><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-openfeign</artifactId></dependency></dependencies>
</project>
order-service/src/main/resources/application.yml
server:port: 8082spring:application:name: order-service # 服务名称eureka:client:service-url:defaultZone: http://localhost:8761/eureka/instance:prefer-ip-address: true# Feign 配置
feign:client:config:default:connectTimeout: 5000readTimeout: 5000
order-service/src/main/java/com/example/OrderServiceApplication.java
package com.example;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication
@EnableDiscoveryClient // 启用服务发现
@EnableFeignClients // 启用 Feign 客户端
public class OrderServiceApplication {public static void main(String[] args) {SpringApplication.run(OrderServiceApplication.class, args);}
}
order-service/src/main/java/com/example/feign/UserFeignClient.java
package com.example.feign;import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;import java.util.Map;@FeignClient(name = "user-service") // 声明为 Feign 客户端,指向 user-service
public interface UserFeignClient {@GetMapping("/users/{id}")Map<String, Object> getUser(@PathVariable("id") Long userId);@GetMapping("/users/hello")String hello();
}
order-service/src/main/java/com/example/controller/OrderController.java
package com.example.controller;import com.example.feign.UserFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.HashMap;
import java.util.Map;@RestController
@RequestMapping("/orders")
public class OrderController {@Autowiredprivate UserFeignClient userFeignClient;// 模拟订单数据private Map<Long, Map<String, Object>> orders = new HashMap<>();public OrderController() {// 初始化测试数据
// orders.put(1001L, Map.of("id", 1001, "name", "订单1", "userId", 1, "amount", 199.9));
// orders.put(1002L, Map.of("id", 1002, "name", "订单2", "userId", 2, "amount", 299.9));// 初始化测试数据 - 使用传统方式orders = new HashMap<>();Map<String, Object> order1 = new HashMap<>();order1.put("id", 1001L);order1.put("name", "订单1");order1.put("userId", 1L);order1.put("amount", 199.9);orders.put(1001L, order1);Map<String, Object> order2 = new HashMap<>();order2.put("id", 1002L);order2.put("name", "订单2");order2.put("userId", 2L);order2.put("amount", 299.9);orders.put(1002L, order2);}@GetMapping("/{id}")public Map<String, Object> getOrderWithUser(@PathVariable Long id) {Map<String, Object> order = orders.get(id);if (order == null) {throw new RuntimeException("订单不存在");}// 通过 Feign 调用用户服务Long userId = (Long) order.get("userId");Map<String, Object> user = userFeignClient.getUser(userId);// 组合订单和用户信息Map<String, Object> result = new HashMap<>();result.put("order", order);result.put("user", user);result.put("service", "order-service");return result;}@GetMapping("/test-feign")public String testFeign() {return userFeignClient.hello();}@GetMapping("/")public Map<String, Object> getAllOrders() {Map<String,Object> ordersT = new HashMap<>();ordersT.put("orders",orders.values());ordersT.put("service","order-service");return ordersT;}
}
5,启动和测试
启动顺序:
- 启动 Eureka Server (端口 8761)
- 启动 User Service (端口 8081)
- 启动 Order Service (端口 8082)
访问测试:
1,查看服务注册情况
http://localhost:8761
2,测试用户服务
# 获取用户信息
curl http://localhost:8081/users/1# 用户服务hello
curl http://localhost:8081/users/hello# 所有用户
curl http://localhost:8081/users/
3,测试订单服务(包含服务调用)
# 获取订单信息(包含用户信息)
curl http://localhost:8082/orders/1001# 测试Feign调用
curl http://localhost:8082/orders/test-feign# 所有订单
curl http://localhost:8082/orders/
项目特点
✅ 服务注册与发现 - Eureka Server
✅ 服务间调用 - Feign Client
✅ 负载均衡 - 内置 Ribbon
✅ 配置管理 - 统一配置
✅ 独立部署 - 每个服务可独立运行
这个简单的 Spring Cloud 项目展示了微服务的基本架构
