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

相关推荐
历程里程碑11 分钟前
普通数组----轮转数组
java·数据结构·c++·算法·spring·leetcode·eclipse
callJJ15 分钟前
Spring AI ImageModel 完全指南:用 OpenAI DALL-E 生成图像
大数据·人工智能·spring·openai·springai·图像模型
郝学胜-神的一滴1 小时前
超越Spring的Summer(一): PackageScanner 类实现原理详解
java·服务器·开发语言·后端·spring·软件构建
MX_935914 小时前
Spring的bean工厂后处理器和Bean后处理器
java·后端·spring
程序员泠零澪回家种桔子15 小时前
Spring AI框架全方位详解
java·人工智能·后端·spring·ai·架构
晚霞的不甘16 小时前
CANN 编译器深度解析:UB、L1 与 Global Memory 的协同调度机制
java·后端·spring·架构·音视频
寄存器漫游者17 小时前
Linux 软件编程 - IO 编程
linux·运维·spring
我真会写代码18 小时前
SSM(指南一)---Maven项目管理从入门到精通|高质量实操指南
java·spring·tomcat·maven·ssm
vx_Biye_Design18 小时前
【关注可免费领取源码】房屋出租系统的设计与实现--毕设附源码40805
java·spring boot·spring·spring cloud·servlet·eclipse·课程设计
独断万古他化20 小时前
【SSM开发实战:博客系统】(三)核心业务功能开发与安全加密实现
spring boot·spring·mybatis·博客系统·加密