Spring Cloud Eureka:
组成:
- Eureka Server 注册中心
- Eureka Client 服务注册
Eureka Server 注册中心
使用 IntelliJ Idea 创建一个Eureka Server项目
在Dependencies选择时 选择:Cloud Discovery - Eureka Server
踩坑日记: 最初我使用Edgware SR5版本,在fegin+hystrix结合使用时会出现找到两个bean的情况,坑了我三天,之后升级版本Finchley SR2(官方推荐)解决,之后项目版本保持一致
创建完成后需要在启动类上使用 @EnableEurekaServer 注解,如下:
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
启动项目,打开 http://localhost:8080/ 成功并进入Eureka 管理界面,但此时控制台报错了,这是为什么呢?是因为Eureka Server同时具备注册中心和服务注册功能,所以你必须找个地方注册才行!
在配置文件application.yml加入以下配置,8761是默认端口,建议把项目端口也改成8761,剩下的控制台报错是正常现象(心跳检测机制)
spring:
application:
name: eureka
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
register-with-eureka: false // 隐藏自身
Eureka Client 服务注册
使用 IntelliJ Idea 创建一个Eureka Client项目
在Dependencies选择时选择:Cloud Discovery - Eureka Discovery;
创建完成后需要在启动类上使用 @EnableDiscoveryClient 注解,如下:
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
application.yml配置如下:
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
hostname: clientName //链接别名,保护地址不被暴露(可忽略此步)
spring:
application:
name: client
先启动server端再启动client端,查看管理页面,注册成功
Eureka 的高可用:
需要启动两个server端,并且互相注册,实现信息交换
- server1:8761
- server2:8762
启动后你会发现注册在server1上的client端也会出现在server2上。
弊端:当server1关掉时,虽然server2上还拥有client端的信息,但是在client端重启后由于心跳机制会出现找不到注册中心的错误!
所以client端需要同时注册到server1,server2上,实现高可用配置如下:
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
spring:
application:
name: client
Spring Cloud Eureka 学习结束
CSDN著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。