部署Eureka集群实现高可用服务
部署Eureka集群,记录在此
1.环境准备
服务器:Ubuntu-Server 20.04 TLS
JDK:jdk-11.0.8
SpringBoot: 2.3.7.RELEASE
SpringCloud:Hoxton.SR9
Ubuntu安装可以参考文末链接
2.部署Eureka集群
这里推荐使用阿里云脚手架创建Eureka服务项目 脚手架生成

启动类启动Eureka注册中心
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}
编写yml配置
192.168.73.128配置文件
server:
  port: 21000
spring:
  application:
    name: eureka-server
#Eureka setting
eureka:
  instance:
    hostname: 192.168.73.128
  client:
    register-with-eureka: true #是否将自己注册到注册中心上
    fetch-registry: true #是否从Eureka上获取注册信息
    service-url:
      defaultZone: http://192.168.73.129:21000/eureka/,http://192.168.73.130:21000/eureka/
192.168.73.129配置文件
server:
  port: 21000
spring:
  application:
    name: eureka-server
#Eureka setting
eureka:
  instance:
    hostname: 192.168.73.129
  client:
    register-with-eureka: true #是否将自己注册到注册中心上
    fetch-registry: true #是否从Eureka上获取注册信息
    service-url:
      defaultZone: http://192.168.73.128:21000/eureka/,http://192.168.73.130:21000/eureka/
192.168.73.130配置文件
server:
  port: 21000
spring:
  application:
    name: eureka-server
#Eureka setting
eureka:
  instance:
    hostname: 192.168.73.130
  client:
    register-with-eureka: true #是否将自己注册到注册中心上
    fetch-registry: true #是否从Eureka上获取注册信息
    service-url:
      defaultZone: http://192.168.73.128:21000/eureka/,http://192.168.73.129:21000/eureka/
分别在当前主机注册其它两台主机
达到三台主机都互相注册
打jar包,分别放到三台服务器上启动~
128节点上

129节点上

130节点上

3.部署服务
创建producer服务
@EnableEurekaClient
@SpringBootApplication
public class ProducerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProducerApplication.class, args);
    }
}
配置文件 注册到三个EurekaServer中
server:
  port: 18000
spring:
  application:
    name: producer-server
#Eureka setting
eureka:
  instance:
    prefer-ip-address: true #使用ip进行注册
  client:
    register-with-eureka: true #是否将自己注册到注册中心上
    fetch-registry: true #是否从Eureka上获取注册信息
    service-url:
      defaultZone: http://192.168.73.128:21000/eureka/,http://192.168.73.129:21000/eureka/,http://192.168.73.130:21000/eureka/
提供sayhalo接口
@RestController
@RequestMapping("producer")
public class ProducerController {
    @GetMapping("sayhalo")
    public String sayhalo(){
        return "sayhalo";
    }
}
启动producer服务 可以看到 在三个注册中心都已经注册上了



创建consumer服务
@EnableFeignClients
@EnableEurekaClient
@SpringBootApplication
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }
}
server:
  port: 18001
spring:
  application:
    name: consumer-server
#Eureka setting
eureka:
  instance:
    prefer-ip-address: true #使用ip进行注册
  client:
    register-with-eureka: true #是否将自己注册到注册中心上
    fetch-registry: true #是否从Eureka上获取注册信息
    service-url:
      defaultZone: http://192.168.73.128:21000/eureka/,http://192.168.73.129:21000/eureka/,http://192.168.73.130:21000/eureka/
使用feign调用producer服务的sayhalo接口
@FeignClient("producer-server")
public interface ProducerFeign {
    @GetMapping("producer/sayhalo")
    public String sayhalo();
}
@RestController
@RequestMapping("consumer")
public class ConsumerController {
    @Autowired
    ProducerFeign feign;
    @GetMapping("getproducer")
    public String getproducer(){
        return feign.sayhalo();
    }
}
启动consumer服务 可以看到在三个eureka注册中心都已经注册上了



调用producer服务的sayhalo接口

可以看到通过consumer服务调用producer服务的sayhalo接口成功
4.测试高可用
关闭128节点的eureka 可以看到在其它两个注册中心上已经没有128节点的eureka服务了


我们再次请求接口 是没有问题的

我们把128节点启动 在其它节点可以看到它注册上来了


上边是eureka正常退出的情况,我们模拟下因网络波动或者无法抗拒的原因导致的非正常退出情况
我直接把130的node关机 可以看到eureka集群上还是可以看到这个节点,但是上边出现了一行红色的字
这是因为Eureka的自我自我保护机制,它会认为服务可能网络波动在等待服务上线 可以参考文末的Eureka自我保护机制

现在是128 129 两台Eureka,我同时也起了两个生产者服务,也断掉了一台。
这时候我们压测一下,可以看到成功率是百分之百的。


5.Code
代码已上传github.
Eureka集群部署Demo
