以下是Spring Cloud高可用微服务集群搭建的实战指南,包含核心组件配置和部署策略:

一、集群架构设计

graph TD
    A[客户端] --> B[Nginx负载均衡]
    B --> C[Zuul网关集群]
    C --> D[服务注册中心集群]
    D --> E[微服务实例集群]
    E --> F[Config配置中心]
    F --> G[Git远程仓库]

二、关键组件配置

1. Eureka注册中心集群(3节点)

application-peer1.yml

server:
  port: 8761
eureka:
  instance:
    hostname: peer1
  client:
    serviceUrl:
      defaultZone: http://peer2:8762/eureka/,http://peer3:8763/eureka/

启动命令(需配置hosts):

# 节点1
java -jar eureka-server.jar --spring.profiles.active=peer1

# 节点2
java -jar eureka-server.jar --spring.profiles.active=peer2

# 节点3
java -jar eureka-server.jar --spring.profiles.active=peer3

2. 服务提供者配置
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    serviceUrl:
      defaultZone: http://peer1:8761/eureka/,http://peer2:8762/eureka/,http://peer3:8763/eureka/
  instance:
    prefer-ip-address: true

3. Zuul网关集群
@EnableZuulProxy
@SpringBootApplication
public class ZuulApplication {
    public static void main(String[] args) {
        SpringApplication.run(ZuulApplication.class, args);
    }
}

路由配置

zuul:
  routes:
    user-service:
      path: /user/**
      serviceId: user-service
    order-service:
      path: /order/**
      serviceId: order-service

三、高可用策略

  1. 注册中心

    • 使用3节点组成集群
    • 开启自我保护模式:eureka.server.enable-self-preservation=true
    • 同步间隔:eureka.client.registry-fetch-interval-seconds=30
  2. 服务熔断(Hystrix):

    @HystrixCommand(
      fallbackMethod = "defaultUser",
      commandProperties = {
        @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="2000")
      })
    public User getUserById(String id) {
      // 服务调用
    }
    

  3. 负载均衡(Ribbon):

    user-service:
      ribbon:
        NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule
        ConnectTimeout: 1000
        ReadTimeout: 3000
    

四、部署拓扑

                          +-----------------+
                          |     Nginx       |
                          | (负载均衡)      |
                          +-------+---------+
                                  |
                 +----------------+-----------------+
                 |                |                 |
          +------+------+ +-------+-------+ +-------+-------+
          |  Zuul Node1 | |   Zuul Node2  | |   Zuul Node3  |
          +------+------+ +-------+-------+ +-------+-------+
                 |                |                 |
          +------+------+ +-------+-------+ +-------+-------+
          |  Eureka 1   | |   Eureka 2    | |   Eureka 3    |
          +-------------+ +---------------+ +---------------+

五、验证步骤

  1. 服务注册检查
    curl http://peer1:8761/eureka/apps
    

  2. 网关路由测试
    curl http://zuul:8080/user/1
    

  3. 熔断模拟
    // 主动抛出异常触发fallback
    throw new RuntimeException("Service forced failure");
    

注意事项

  1. 所有节点时间必须同步(NTP服务)
  2. 生产环境需配置SSL加密通信
  3. 建议配合Spring Boot Actuator实现健康检查
  4. 使用Config Server统一管理配置,支持动态刷新

通过以上配置,可实现:

  • 注册中心宕机容忍度:允许1个节点故障
  • 服务调用成功率:>99.95%(依赖具体基础设施)
  • 横向扩展能力:随时增加服务实例

更多推荐