-
eureka就是服务注册发现 ,为什么需要用到eureka.
- 因为在一个完整的系统架构中,任何单点的服务都不能保证不会中断,因此我们需要服务发现机制,在某个节点中断后,其它的节点能够继续提供服务,从而保证整个系统是高可用的。
-
如何达到高可用的eureka?
- 需要将eureka配置为对等模式,多个eureka互相注册,防止某个节点挂了,不影响到整个服务的运行
-
server: port: 8762 eureka: client: register-with-eureka: false fetch-registry: false serviceUrl: defaultZone: http://test:111@localhost:8761/eureka server: enableSelfPreservation: false instance: prefer-ip-address: true #安全认证的配置 security: basic: enabled: true user: name: test password: 111
server: port: 8761 eureka: client: register-with-eureka: false fetch-registry: false serviceUrl: defaultZone: http://test:111@localhost:8762/eureka server: enableSelfPreservation: false instance: prefer-ip-address: true #安全认证的配置 security: basic: enabled: true user: name: test password: 111
对Eureka服务的身份验证
- 如果其中一个
eureka.client.serviceUrl.defaultZone
的url已经把凭证嵌入到它里面,那么HTTP基本的身份验证将会被自动添加到你的eureka客户端(curl风格,如 http://user:password@localhost:8761/eureka)。 -
创建“注册与发现服务”
- 创建一个基础的Spring Boot工程,并在
pom.xml
中引入需要的以下依赖内容: -
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.7</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- eureka support --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
通过
@EnableEurekaServer
注解启动一个服务注册中心提供给其他应用进行对话。这一步非常的简单,只需要在一个普通的Spring Boot应用中添加这个注解就能开启此功能,比如下面的例子: -
@EnableEurekaServer //启动一个服务注册中心提供给其他应用进行对话 @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } }
在默认设置下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为,只需要在
application.properties
中问增加如下配置:server.port=8761 eureka.client.register-with-eureka=false eureka.client.fetch-registry=false eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
如何查看eureka是否启动成功?
- 在浏览器中输入eureka部署的机器的IP地址以及端口号,就可以查看有哪些服务已注册。
-
创建注册与发现客户端
- 首先,创建一个基本的Spring Boot应用,在
pom.xml
中,加入如下配置: -
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
然后在启动类上加@EnableEurekaClient注解,该注解能激活Eureka中的
DiscoveryClient
实现。 -
@EnableEurekaClient @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } }
之后在对应的application.properties文件配置
-
spring.application.name=test server.port=8180 eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/,http://localhost:8762/eureka/
通过
spring.application.name
属性,我们可以指定微服务的名称后续在调用的时候只需要使用该名称就可以进行服务的访问 -
eureka.client.serviceUrl.defaultZone
属性对应服务注册中心的配置内容,指定服务注册中心的位置,告诉我们该服务注册到哪个eureka上,若是多个eureka的话,中间用逗号隔开。 -
然后再次在浏览器中输入对应的eureka的地址,就能看到对应的test项目注册在eureka上去了。
-
eureka常见问题总结
- eureka使用IP注册(Eureka配置instanceId显示IP)
-
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ #关键在于下面2个配置 instance: preferIpAddress: true instance-id: ${spring.cloud.client.ipAddress}:${server.port}
eureka注册服务慢的问题如何解决?
-
eureka.instance.leaseRenewalIntervalInSeconds 参考文档: http://cloud.spring.io/spring-cloud-static/Camden.SR1/#_why_is_it_so_slow_to_register_a_service
原文: Why is it so Slow to Register a Service? Being an instance also involves a periodic heartbeat to the registry (via the client’s serviceUrl) with default duration 30 seconds. A service is not available for discovery by clients until the instance, the server and the client all have the same metadata in their local cache (so it could take 3 heartbeats). You can change the period using eureka.instance.leaseRenewalIntervalInSeconds and this will speed up the process of getting clients connected to other services. In production it’s probably better to stick with the default because there are some computations internally in the server that make assumptions about the lease renewal period. 翻译: 作为实例还涉及到与注册中心的周期性心跳,默认持续时间为30秒(通过serviceUrl)。在实例、服务器、客户端都在本地缓存中具有相同的元数据之前,服务不可用于客户端发现(所以可能需要3次心跳)。你可以使用eureka.instance.leaseRenewalIntervalInSeconds 配置,这将加快客户端连接到其他服务的过程。在生产中,最好坚持使用默认值,因为在服务器内部有一些计算,他们对续约做出假设。
如何解决eureka Server不踢出已关停的节点的问题?
-
server端: eureka.server.enable-self-preservation (设为false,关闭自我保护主要) eureka.server.eviction-interval-timer-in-ms 清理间隔(单位毫秒,默认是60*1000) client端: eureka.client.healthcheck.enabled = true 开启健康检查(需要spring-boot-starter-actuator依赖) eureka.instance.lease-renewal-interval-in-seconds =10 租期更新时间间隔(默认30秒) eureka.instance.lease-expiration-duration-in-seconds =30 租期到期时间(默认90秒) 示例: 服务器端配置: eureka: server: enableSelfPreservation: false evictionIntervalTimerInMs: 4000 客户端配置: eureka: instance: leaseRenewalIntervalInSeconds: 10 leaseExpirationDurationInSeconds: 30 注意: 更改Eureka更新频率将打破服务器的自我保护功能 参考文档 https://github.com/spring-cloud/spring-cloud-netflix/issues/373
Eureka Environment的配置
-
eureka.environment: 字符串 参考文档: https://github.com/Netflix/eureka/wiki/Configuring-Eureka
Eureka DataCenter的配置
-
eureka.datacenter: cloud https://github.com/Netflix/eureka/wiki/Configuring-Eureka 这边说:配置-Deureka.datacenter=cloud,这样eureka将会知道是在AWS云上
转载于:https://my.oschina.net/u/3370769/blog/884333