简易Spring Cloud + Eureka微服务搭建
快速构建Maven项目生成如下目录结构
目录结构.png
本文注册服务用的是 Spring 内置 Eureka。
搭建Eureka服务
在pom中添加必要依赖:
<groupId>com.syy</groupId>
<artifactId>eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-server</name>
<description>Eureka Server</description>
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Camden.SR7</version>
</parent>
<dependencies>
...
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
...
</dependencies>
在application.properties中添加eureka相关信息:
server.port=8761
eureka.instance.hostname=127.0.0.1
eureka.instance.prefer-ip-address=true
# defaultZone 设置eureka服务器所在的地址,查询服务和注册服务都需要依赖这个地址
eureka.client.service-url.defaultZone= http://${eureka.instance.hostname}:${server.port}/eureka/
# registerWithEureka表示是否注册自身到eureka服务器,当前这个应用就是eureka服务器,没必要注册自身
eureka.client.registerWithEureka=false
# fetchRegistry表示是否从eureka服务器获取注册信息, 同上
eureka.client.fetchRegistry=false
在main启动程序添加注解@SpringBootApplication:
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
启动服务:
...
2018-08-07 14:27:15.457 INFO 15308 --- [ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 0
2018-08-07 14:27:15.458 INFO 15308 --- [ main] c.n.e.EurekaDiscoveryClientConfiguration : Registering application unknown with eureka with status UP
2018-08-07 14:27:15.458 INFO 15308 --- [ Thread-10] o.s.c.n.e.server.EurekaServerBootstrap : Setting the eureka configuration..
2018-08-07 14:27:15.459 INFO 15308 --- [ Thread-10] o.s.c.n.e.server.EurekaServerBootstrap : Eureka data center value eureka.datacenter is not set, defaulting to default
2018-08-07 14:27:15.459 INFO 15308 --- [ Thread-10] o.s.c.n.e.server.EurekaServerBootstrap : Eureka environment value eureka.environment is not set, defaulting to test
2018-08-07 14:27:15.510 INFO 15308 --- [ Thread-10] o.s.c.n.e.server.EurekaServerBootstrap : isAws returned false
2018-08-07 14:27:15.513 INFO 15308 --- [ Thread-10] o.s.c.n.e.server.EurekaServerBootstrap : Initialized server context
2018-08-07 14:27:15.515 INFO 15308 --- [ Thread-10] c.n.e.r.PeerAwareInstanceRegistryImpl : Got 1 instances from neighboring DS node
2018-08-07 14:27:15.515 INFO 15308 --- [ Thread-10] c.n.e.r.PeerAwareInstanceRegistryImpl : Renew threshold is: 1
2018-08-07 14:27:15.515 INFO 15308 --- [ Thread-10] c.n.e.r.PeerAwareInstanceRegistryImpl : Changing status to UP
2018-08-07 14:27:15.522 INFO 15308 --- [ Thread-10] e.s.EurekaServerInitializerConfiguration : Started Eureka Server
2018-08-07 14:27:15.580 INFO 15308 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8761 (http)
2018-08-07 14:27:15.582 INFO 15308 --- [ main] c.n.e.EurekaDiscoveryClientConfiguration : Updating port to 8761
2018-08-07 14:27:15.585 INFO 15308 --- [ main] com.syy.EurekaServerApplication : Started EurekaServerApplication in 7.588 seconds (JVM running for 8.917)
访问http://127.0.0.1:8761出现如下界面,即服务启动成功。
Eureka服务端启动成功.png注册服务至EurekaServer
pom同样需要添加必备的依赖
application.properties配置如下:
server.port=8088
application.properties
# 当前应用名称
spring.application.name=microapp
# eureka注册中心地址
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8761/eureka/
在main启动程序添加注解@EnableDiscoveryClient或者针对Eureka服务的注解@EnableEurekaClient
@EnableDiscoveryClient
@SpringBootApplication
public class MicroAppApplication {
public static void main(String[] args) {
SpringApplication.run(MicroAppApplication.class, args);
}
}
客户端成功注册到Eureka.png
启动客户端服务可能出现connection:refused的地方:
- 检查注册中心地址是否一致
- 配置文件中横线命名调整为驼峰命名
- Eureka服务端eureka.client.registerWithEureka,eureka.client.fetchRegistry均应设为false。因为本身为服务端,不是客户端,不需要注册。
- security相关。一旦引入security包,就默认开启鉴权,暂时设置spring.security.basic.enabled为false看一下。
自我保护机制
把客户端服务停掉,会发现注册中心中依然存在该服务信息。
自我保护机制体现.png官方文档原文地址
When the Eureka server comes up, it tries to get all of the instance registry information from a neighboring node. If there is a problem getting the information from a node, the server tries all of the peers before it gives up. If the server is able to successfully get all of the instances, it sets the renewal threshold that it should be receiving based on that information. If any time, the renewals falls below the percent configured for that value (below 85% within 15 mins), the server stops expiring instances to protect the current instance registry information.
In Netflix, the above safeguard is called as self-preservation mode and is primarily used as a protection in scenarios where there is a network partition between a group of clients and the Eureka Server. In these scenarios, the server tries to protect the information it already has. There may be scenarios in case of a mass outage that this may cause the clients to get the instances that do not exist anymore. The clients must make sure they are resilient to eureka server returning an instance that is non-existent or un-responsive. The best protection in these scenarios is to timeout quickly and try other servers.
自我保护机制工作机制:
如果在15分钟内超过85%的客户端节点都没有正常的心跳,Eureka就认为客户端与注册中心出现了网络故障,Eureka Server自动进入自我保护机制,此时会出现以下几种情况:
- Eureka Server不再从注册列表中移除因为长时间没收到心跳而应该过期的服务。
- Eureka Server仍然能够接受新服务的注册和查询请求,但是不会被同步到其它节点上,保证当前节点依然可用。
- 当网络稳定时,当前Eureka Server新的注册信息会被同步到其它节点中。
简单来说,自我保护机制是一种针对网络异常波动的保护措施,通过这种机制使Eureka集群得以健壮、稳定的运行。
自我保护默认为打开状态,测试环境可以关闭,但是建议生产环境打开此配置。
服务端配置:
eureka.server.enable-self-preservation=false
客户端配置:
# 每间隔1s,向服务端发送一次心跳,证明自己依然”存活“
eureka.instance.lease-renewal-interval-in-seconds=1
# 告诉服务端,如果我2s之内没有给你发心跳,就代表我“死”了,将我踢出掉。
eureka.instance.lease-expiration-duration-in-seconds=2