SpringCloud系列教程(1)——Eureka注册中心
本文环境 springboot2.0.6 spring cloud 版本是Finchley.SR2
Spring Cloud Eureka 是基于Netflix Eureka做了2次封装
有2个组件:
Eureka Server 注册中心
Eureka Client 服务注册
-=---------------------------------
eureka server的使用:
1、创建项目
我右上角的springboot 选择了2.0.6
2、修改EurekaApplication
在启动类上添加注解:
@EnableEurekaServer
3、修改application.yml:
eureka.client.serviceUrl.defaultZone与eureka.client.service-url.defaultZone有什么区别?
驼峰命名的区别,都可以用
报错:
Description:
Failed to bind properties under 'eureka.client.service-url' to java.util.Map<java.lang.String, java.lang.String>:
Reason: No converter found capable of converting from type [java.lang.String] to type [java.util.Map<java.lang.String, java.lang.String>]
注意defaultZone不要与client对齐
-------------------
修改端口
server:
port: 8761
默认是:
defaultZone: http://localhost:8761/eureka/
打开浏览器:
发现已经注册进来了,但我们这个是注册中心本身,我们不用把他注册进来,
修改application.yml:
eureka:
client:
registerWithEureka: false
这样就看不到了
补充:
- eureka.client.register-with-eureka :表示是否将自己注册到Eureka Server,默认为true。
- eureka.client.fetch-registry :表示是否从Eureka Server获取注册信息,默认为true。
- eureka.client.serviceUrl.defaultZone :设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。默认是http://localhost:8761/eureka ;多个地址可使用 , 分隔。
最后的yml:server:
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
serviceUrl:
defaultZone: http://localhost:8761/eureka/
# #defaultZone: http://localhost:8761/eureka/ # ==== 可用,与上面写法一样
spring:
application:
name: eurka-server
==================================
eureka client的使用
1、创建项目
2、application.yml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
3、EurekaClientApplication
在启动类,添加这个注解
@EnableDiscoveryClient
然后启动,报错
Invocation of destroy method failed on bean with name 'scopedTarget.eurekaClient': org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'eurekaInstanceConfigBean': Singleton bean creation not allowed while singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)
这是因为client里不包含Tomcat的依赖,所以Spring容器无法创建一些实例,从而导致项目无法启动,只需在pom.xml文件中,加上web依赖即可:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
重新启动
服务已经注册进来了
又因为上面的图,服务名为 UNKONOWN
所以修改.yml文件
spring:
application:
name: service-hi
重启
点击上图绿色的部分,服务的地址
出现如下图所示:
如何自定义链接别名呢?
application.yml:
eureka:
instance:
hostname: ClientNEW
重启
成功
另外,浏览器出现
表明你的client上线率太低了,但在开发环境下,client要频繁上下线
所以我们可以把他给关了
在eureka sever端
的application.yml中修改
eureka:
server:
enable-self-preservation: false
然后重启
没了,但出现了一个新的警告,它的意思是不要把保护模式关掉
所以生存环境一定不要把它关掉。我把它给还原回去~~
===========================
eureka 高可用
注册中心这么关键的服务,如果是单点话,遇到故障就是毁灭性的。为了维持其可用性,使用集群是很好的解决方案。Eureka通过互相注册的方式来实现高可用的部署,所以我们只需要将Eureke Server配置其他可用的serviceUrl就能实现高可用部署。
现在:
双节点注册中心:
如上图
再创一个eureka-server2
与前面类似,故略
保证:
eureka的application.yml:
server:
port: 8761
eureka:
# server:
# enable-self-preservation: false //关掉保护模式
instance:
hostname: node1
client:
serviceUrl:
defaultZone: http://node2:8762/eureka/
spring:
application:
name: eureka-server
--------------------------------
eureka2的application.yml:
server:
port: 8762
eureka:
instance:
hostname: node2
client:
serviceUrl:
defaultZone: http://node1:8761/eureka/
spring:
application:
name: eureka-server
ps:
spring.application.name 要相同
- Mac或者Linux配置方式 如果你使用的是osx系统。可以找到/etc/hosts文件并添加如下内容:
127.0.0.1 node1
127.0.0.1 node2
复制代码
一般情况下配置完成后就会生效,如果你的配置并没有生效,你可以尝试重启。
- Windows配置方式 如果你使用的是windows系统,你可以修改C:\Windows\System32\drivers\etc\hosts文件,添加内容与Mac方式一致。
这样2个注册中心就互相交互了,组成一个“伪集群
但还需将client分别注册到注册中心去
client
application.yml:
eureka:
client:
serviceUrl:
defaultZone: http://node1:8761/eureka/,http://node2:8762/eureka/
instance:
hostname: localhost
spring:
application:
name: service-hi
集群(3台及以上):
与双节点类似,故略
可参考:https://blog.rmiao.top/springcloud-eureka/
Eureka 自此体现了其具有心跳检测,健康检查,高可用等功能
分布式系统中,服务注册中心是最重要的基础部分
拓展:
客户端发现:Eureka
服务端发现:Nginx,Zookeeper,Kubernetes
参考:https://blog.rmiao.top/springcloud-eureka/
https://juejin.im/post/5bb726136fb9a05cd24da9b0
https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f1-eureka/