Spring Cloud微服务入门

一、什么是Spring Cloud微服务?

想象一下,你有一个超大的玩具积木,把它拆成很多个小积木,每个小积木都有自己的功能,比如有的是轮子,有的是车身,有的是发动机。这些小积木就是"微服务",它们可以独立运行,也可以组合起来完成复杂的功能。Spring Cloud就是帮你管理和连接这些小积木的工具。

二、开始前的准备

在动手之前,你需要准备几样东西:

  1. **Java开发环境(JDK)**:推荐使用JDK 17或更高版本。

  2. **Maven**:用于项目管理和构建,版本推荐3.9.4或更高。

  3. **开发工具**:推荐使用IntelliJ IDEA,它对Spring Cloud支持得很好。

三、搭建第一个Spring Cloud微服务项目

1. 创建服务注册中心(Eureka Server)

服务注册中心就像一个"电话簿",所有微服务都会把自己"注册"到这里,方便其他服务找到它们。

**步骤:**

  1. 打开 [Spring Initializr](https://start.spring.io/) 网站。

  2. 选择Maven项目,语言选Java。

  3. 添加依赖:`Spring Web` 和 `Eureka Server`。

  4. 点击"Generate",下载解压后导入到IDE中。

**配置文件(`application.yml`):**

```yaml

server:

port: 8761 # Eureka Server的端口

spring:

application:

name: eureka-server

eureka:

client:

register-with-eureka: false

fetch-registry: false

```

**启动类:**

```java

package com.example.eurekaserver;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication

@EnableEurekaServer

public class EurekaServerApplication {

public static void main(String[] args) {

SpringApplication.run(EurekaServerApplication.class, args);

}

}

```

运行这个项目后,你的服务注册中心就启动了。

2. 创建一个微服务(Eureka Client)

**步骤:**

  1. 再次使用Spring Initializr,添加依赖:`Spring Web` 和 `Eureka Discovery Client`。

  2. 下载解压后导入到IDE中。

**配置文件(`application.yml`):**

```yaml

server:

port: 8081 # 微服务的端口

spring:

application:

name: my-service

eureka:

client:

service-url:

defaultZone: http://localhost:8761/eureka/

```

**启动类:**

```java

package com.example.microservice;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication

@EnableDiscoveryClient

public class MicroserviceApplication {

public static void main(String[] args) {

SpringApplication.run(MicroserviceApplication.class, args);

}

}

```

3. 测试服务注册与发现

  1. 启动Eureka Server。

  2. 启动微服务(Eureka Client)。

  3. 打开浏览器,访问 `http://localhost:8761`,你会看到Eureka的管理界面,上面显示了已经注册的微服务。

四、微服务之间的通信

微服务之间需要互相调用,比如一个订单服务可能需要调用用户服务来获取用户信息。Spring Cloud提供了几种方式来实现这一点,最简单的是Feign。

示例:使用Feign实现服务调用

**1. 添加Feign依赖**

在微服务项目的`pom.xml`中添加:

```xml

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-openfeign</artifactId>

</dependency>

```

**2. 创建Feign客户端**

```java

package com.example.microservice.client;

import org.springframework.cloud.openfeign.FeignClient;

import org.springframework.web.bind.annotation.GetMapping;

@FeignClient(name = "user-service")

public interface UserClient {

@GetMapping("/user")

String getUser();

}

```

**3. 调用服务**

在你的微服务中注入这个客户端,然后调用:

```java

package com.example.microservice.controller;

import com.example.microservice.client.UserClient;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RestController;

@RestController

public class MyController {

@Autowired

private UserClient userClient;

@GetMapping("/call-user")

public String callUserService() {

return userClient.getUser();

}

}

```

五、总结

通过上面的步骤,你已经搭建了一个简单的Spring Cloud微服务架构。你可以继续扩展,比如添加配置中心(Spring Cloud Config)、网关(Spring Cloud Gateway)等组件。

Spring Cloud虽然看起来复杂,但其实就是把一个大应用拆成小块,然后用工具把它们串起来。希望这个教程能让你轻松入门,如果有问题,随时问我哦!

相关推荐
砍材农夫1 天前
spring-ai 第四多模态API
java·人工智能·spring
她说..1 天前
Java 对象相关高频面试题
java·开发语言·spring·java-ee
计算机毕设指导61 天前
基于SpringBoot校园学生健康监测管理系统【源码文末联系】
java·spring boot·后端·spring·tomcat·maven·intellij-idea
RInk7oBjo1 天前
spring-事务管理
数据库·sql·spring
希望永不加班1 天前
SpringBoot 数据库连接池配置(HikariCP)最佳实践
java·数据库·spring boot·后端·spring
yaoyouzhong1 天前
基于SpringBoot和PostGIS的云南与缅甸的千里边境线实战
java·spring boot·spring
带刺的坐椅1 天前
Spring-AI 与 Solon-AI 深度对比分析报告
java·spring·ai·llm·solon·spring-ai·solon-ai
做个文艺程序员1 天前
Spring AI + Qwen3.5 实现多步 Agent:从工具调用到自主任务拆解的踩坑全记录
java·人工智能·spring
Aktx20FNz1 天前
一文学习 Spring AOP 源码全过程
java·学习·spring
明灯伴古佛1 天前
面试:对Spring AOP的理解
java·spring·面试