走进Java:创建SpringCloud项目

1. 环境准备

  • JDK 17+:Spring Boot 3.x需要JDK 17及以上。
  • Maven 3.6+Gradle 7.x:构建工具。
  • IDE:推荐IntelliJ IDEA或VS Code。

2. 创建父项目(管理依赖)

使用Spring Initializr(start.spring.io)生成父项目:

  • Project: Maven
  • Language: Java
  • Spring Boot: 3.2.x
  • Packaging: Jar
  • Java Version: 17

Advanced Options中添加依赖:

  • Spring Cloud Config(配置中心)
  • Spring Cloud Discovery(服务发现)

生成项目后,修改pom.xml,添加dependencyManagement管理Spring Cloud版本:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>2023.0.0</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

3. 创建Eureka服务注册中心

  • 新建子模块:在父项目下创建eureka-server模块。
  • 添加依赖pom.xml):
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    
  • 配置文件application.yml):
    server:
      port: 8761
    eureka:
      client:
        register-with-eureka: false
        fetch-registry: false
    
  • 启动类:添加@EnableEurekaServer注解:
    @SpringBootApplication
    @EnableEurekaServer
    public class EurekaServerApplication {
        public static void main(String[] args) {
            SpringApplication.run(EurekaServerApplication.class, args);
        }
    }
    

4. 创建配置中心(Config Server)

  • 新建子模块:创建config-server模块。
  • 添加依赖
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    
  • 配置文件
    server:
      port: 8888
    spring:
      cloud:
        config:
          server:
            git:
              uri: https://github.com/your-repo/config-repo
              clone-on-start: true
    
  • 启动类:添加@EnableConfigServer注解。

5. 创建微服务(Eureka Client)

  • 新建子模块:如user-service
  • 添加依赖
    <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>
    
  • 配置文件bootstrap.yml):
    spring:
      application:
        name: user-service
      cloud:
        config:
          uri: http://localhost:8888
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka
    

6. 创建API网关(Spring Cloud Gateway)

  • 新建子模块:创建api-gateway
  • 添加依赖
    <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>
    
  • 配置文件
    server:
      port: 8080
    spring:
      cloud:
        gateway:
          routes:
            - id: user-service
              uri: lb://user-service
              predicates:
                - Path=/api/users/**
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka
    

7. 服务间通信与熔断

  • 使用OpenFeign
    添加依赖:
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    
    启动类添加@EnableFeignClients
  • 熔断器(Resilience4j)
    添加依赖:
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-circuitbreaker-resilience4j</artifactId>
    </dependency>
    

8. 测试验证

  1. 启动顺序:Config Server → Eureka Server → 微服务 → Gateway。
  2. 访问Eureka Dashboardhttp://localhost:8761,确认服务注册。
  3. 通过网关访问服务http://localhost:8080/api/users/1
  4. 检查配置中心:确保微服务从Git仓库获取配置。

常见问题解决

  • 服务未注册:检查defaultZone配置和Eureka Server状态。
  • 配置加载失败:确认bootstrap.yml正确,Config Server可达。
  • 版本冲突:统一父pom中的Spring Cloud和Boot版本。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Small Cow

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值