Spring Cloud微服务

发布于:2025-02-21 ⋅ 阅读:(15) ⋅ 点赞:(0)


一、什么是Spring Cloud微服务?

想象一下,你有一个超大的应用程序,就像一个巨大的蛋糕。如果把它做成一个整体,一旦有一小部分坏了,整个蛋糕都可能坏掉。微服务就是把这块大蛋糕切成很多小块,每一块都能独立工作,坏了也不会影响其他部分。Spring Cloud就是帮助我们把这些小块(微服务)很好地组织起来的工具。


二、开发前的准备

1. 安装Java

Spring Cloud是基于Java的,所以得先安装Java。去Oracle官网下载安装JDK。

2. 安装Maven

Maven是用来管理项目依赖的工具。去Maven官网下载,安装完成后,记得配置环境变量。

3. 安装IDE

推荐用IntelliJ IDEA,因为它对Spring Boot和Spring Cloud支持特别好。去官网下载安装就行。


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

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

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

步骤:
  1. 打开Spring Initializr网站。

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

  3. 添加依赖:Spring WebEureka Server

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

配置文件(application.yml):

yaml复制

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