Spring Cloud Eureka 入门 (一)服务注册中心详解

「Spring Cloud Eureka 入门系列」


本文提纲
1. Eureka 服务治理
1.1 什么是 Eureka
1.2 Eureka 集群架构
2. 运行 Eureka 工程 springcloud-eureka-server
3. 详解 Eureka 工程 springcloud-eureka-server
 

一、Eureka 服务治理
1.1 什么是 Eureka
Eureka,这里是 Spring Cloud Eureka 的简称,是 Spring Cloud Netflix 组件之一。Spring Cloud Netflix 中核心的组件包括了服务治理(Eureka),服务容断(Hystrix),路由(Zuul)和客户端负载均衡(Ribbon)。在系列第三篇,服务消费者讲解会涉及到 Ribbon 的使用。
回到 Spring Cloud Eureka,是基于 Netflix Eureka (Netflix 是 Java 实现的开源软件)。服务治理(Eureka)包括服务注册、服务发现和服务检测监控等,自然本文介绍下 Eureka 作为服务注册中心。
 
1.2 Eureka 架构

Eureka 作为服务治理,必然满足下面几点:
- 服务本身不存在单点故障,
- 支持集群,即高可用性
- 服务与服务之间通过服务注册中心找到彼此实例
 
作为服务端(即服务注册中心),包括
- 管理服务实例
- 提供服务注册或下线
- 提供服务发现
- 提供服务注册表至两类客户端(即服务提供者和消费者)
 
作为客户端(即服务提供者和消费者),包括
- 连接服务注册中心
- 向服务注册中心注册或者下线服务实例
- 向服务注册中心或服务注册缓存列表查询服务
 
Eureka 集群架构如图所示:
Eureka集群.jpeg




二、运行 Eureka 工程 springcloud-eureka-server 

运行环境:JDK 7 或 8,Maven 3.0+
技术栈:Spring Cloud Dalston.SR1、 spring-cloud-netflix 1.3.1、Spring Boot 1.5.4
 
1. git clone 下载工程 springcloud-learning-example
项目地址见 GitHub - https://github.com/JeffLi1993/ ... ample
git clone https://github.com/JeffLi1993/ ... e.git

 
2. Maven 编译安装这个工程:
cd springcloud-learning-example
mvn clean install





3. 运行 springcloud-eureka-server Eureka 工程
右键 Main 函数 Run Eureka Server 启动类 EurekaServerApplication,启动服务注册中心工程。
EurekaServerApplication 类地址:/springcloud-learning-example/springcloud-eureka-sample/springcloud-eureka-server/src/main/java/org/spring/springboot/EurekaServerApplication.java
控制台 Console 看到这类信息,代表启动成功:
2017-06-30 10:32:47.549  INFO 2977 --- [      Thread-11] e.s.EurekaServerInitializerConfiguration : Started Eureka Server
2017-06-30 10:32:47.625 INFO 2977 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8888 (http)
2017-06-30 10:32:47.626 INFO 2977 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8888
2017-06-30 10:32:47.632 INFO 2977 --- [ main] o.s.springboot.EurekaServerApplication : Started EurekaServerApplication in 23.168 seconds


4. 访问 Eureka 注册中心可视化界面
打开浏览器,访问 http://localhost:8888/ ,如图所示

WechatIMG406.jpeg


可以看到主体信息包括:
- 系统状态:环境、运行时间、更新时间等
- 注册信息:服务名、服务地址、服务状态
- 基本信息:环境、内存、副本信息
- 实例信息:IP、端口
 

三、详解 Eureka 工程 springcloud-eureka-server 
1.springcloud-eureka-server  工程目录结构
├── pom.xml
└── src
└── main
├── java
│ └── org
│ └── spring
│ └── springcloud
│ ├── EurekaServerApplication.java
└── resources
└── application.yml
EurekaServerApplication.java Eureka Server 启动类
application.yml 配置文件

2. pom.xml 配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/ma ... gt%3B
<modelVersion>4.0.0</modelVersion>
<groupId>springcloud</groupId>
<artifactId>springcloud-eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-eureka-server :: Spring Cloud Eureka 服务注册中心</name>
<!-- Spring Boot 启动父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<dependencies>
<!-- Spring Cloud Netflix Eureka Server 依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<!-- Spring Boot Test 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<!-- Spring Cloud Netflix 依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix</artifactId>
<version>1.3.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
使用的依赖是
- spring-cloud-netflix 1.3.1 是 Spring Cloud Dalston.SR1 版本。
- spring-cloud-starter-eureka-server Eureka Server 模块依赖
上面提到的客户端负载均衡 Ribbon ,可以依赖树中看出 spring-cloud-starter-eureka-server 依赖了 Ribbon 相关的库。因为一般 eureka 本身作为服务自注册实现高可用,也可以作为客户端调用其他服务。
 
3. application.yml 配置
server:
port: 8888 # 服务端口
eureka:
instance:
hostname: localhost # 设置主机名
client:
registerWithEureka: false # 是否向 Eureka 注册服务。该应用为服务注册中心,不需要自注册,设置为 false
fetchRegistry: false # 是否检索服务。该应用为服务注册中心,职责为注册和发现服务,无需检索服务,设置为 false
server:
waitTimeInMsWhenSyncEmpty: 0 # 设置同步为空时的等待时间。默认 5 * MINUTES
application.property,可以看下面的配置解释:
- server.port 设置工程服务端口
- eureka.instance.hostname Eureka 实例主机名
- eureka.client.registerWithEureka 是否向 Eureka 注册服务。服务注册中心服务,没有作为集群,所以不需要自注册,设置为 false
- eureka.client.fetchRegistry 是否检索服务。该应用为服务注册中心,职责为注册和发现服务,无需检索服务,设置为 false
- eureka.server.waitTimeInMsWhenSyncEmpty 设置同步为空时的等待时间。默认 5 * MINUTES
 
4.注册中心应用启动类
/**
* Spring Boot Eureka Server 应用启动类
*
* Created by bysocket on 21/06/17.
*/
@EnableEurekaServer // Eureka Server 标识
@SpringBootApplication // Spring Boot 应用标识
public class EurekaServerApplication {
public static void main(String args) {
// 程序启动入口
// 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
SpringApplication.run(EurekaServerApplication.class,args);
}
}
@EnableEurekaServer 标志该应用作为 Eureka Server ,并会自动化读取相关配置。
 
 
四、小结
此小章节介绍了如何 Eureka 作为服务注册中心 Server,下一小结讲下 服务提供者详解 具体是如何向服务注册中心注册自己的。系列目录如下:

 
资料:
《Spring Cloud微服务实战》https://re.jd.com/cps/item/12172344.html
官方文档
http://cloud.spring.io/spring- ... EASE/


欢迎扫一扫我的公众号关注 — 及时得到博客订阅哦!
http://www.bysocket.com/
https://github.com/JeffLi1993

687474703a2f2f7777772e6279736f636b65742e636f6d2f77702d636f6e74656e742f75706c6f6164732f323031372f30312f7172636f64655f666f725f67685f6364343231653765623764365f3433302e6a7067.jpeg


3 个评论

赞,收下我的膝盖
2017-07-15 16:36:51.854 ERROR 67341 --- [ main] c.n.d.s.t.d.RedirectingEurekaHttpClient : Request execution error

com.sun.jersey.api.client.ClientHandlerException: org.apache.http.conn.ConnectTimeoutException: Connect to hostB:8889 timed out
at com.sun.jersey.client.apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4Handler.java:187) ~[jersey-apache-client4-1.19.1.jar:1.19.1]
at com.sun.jersey.api.client.filter.GZIPContentEncodingFilter.handle(GZIPContentEncodingFilter.java:123) ~[jersey-client-1.19.1.jar:1.19.1]
at com.netflix.discovery.EurekaIdentityHeaderFilter.handle(EurekaIdentityHeaderFilter.java:27) ~[eureka-client-1.6.2.jar:1.6.2]
at com.sun.jersey.api.client.Client.handle(Client.java:652) ~[jersey-client-1.19.1.jar:1.19.1]
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:682) ~[jersey-client-1.19.1.jar:1.19.1]
at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74) ~[jersey-client-1.19.1.jar:1.19.1]
at com.sun.jersey.api.client.WebResource$Builder.get(WebResource.java:509) ~[jersey-client-1.19.1.jar:1.19.1]
at com.netflix.discovery.shared.transport.jersey.AbstractJerseyEurekaHttpClient.getApplicationsInternal(AbstractJerseyEurekaHttpClient.java:194) ~[eureka-client-1.6.2.jar:1.6.2]
at com.netflix.discovery.shared.transport.jersey.AbstractJerseyEurekaHttpClient.getApplications(AbstractJerseyEurekaHttpClient.java:165) ~[eureka-client-1.6.2.jar:1.6.2]
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$6.execute(EurekaHttpClientDecorator.java:137) [eureka-client-1.6.2.jar:1.6.2]
at com.netflix.discovery.shared.transport.decorator.MetricsCollectingEurekaHttpClient.execute(MetricsCollectingEurekaHttpClient.java:73) ~[eureka-client-1.6.2.jar:1.6.2]
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.getApplications(EurekaHttpClientDecorator.java:134) [eureka-client-1.6.2.jar:1.6.2]
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$6.execute(EurekaHttpClientDecorator.java:137) [eureka-client-1.6.2.jar:1.6.2]
at com.netflix.discovery.shared.transport.decorator.RedirectingEurekaHttpClient.executeOnNewServer(RedirectingEurekaHttpClient.java:118) ~[eureka-client-1.6.2.jar:1.6.2]
at com.netflix.discovery.shared.transport.decorator.RedirectingEurekaHttpClient.execute(RedirectingEurekaHttpClient.java:79) ~[eureka-client-1.6.2.jar:1.6.2]
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.getApplications(EurekaHttpClientDecorator.java:134) [eureka-client-1.6.2.jar:1.6.2]
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$6.execute(EurekaHttpClientDecorator.java:137) [eureka-client-1.6.2.jar:1.6.2]
at com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.execute(RetryableEurekaHttpClient.java:119) [eureka-client-1.6.2.jar:1.6.2]
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.getApplications(EurekaHttpClientDecorator.java:134) [eureka-client-1.6.2.jar:1.6.2]
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$6.execute(EurekaHttpClientDecorator.java:137) [eureka-client-1.6.2.jar:1.6.2]
at com.netflix.discovery.shared.transport.decorator.SessionedEurekaHttpClient.execute(SessionedEurekaHttpClient.java:77) [eureka-client-1.6.2.jar:1.6.2]
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.getApplications(EurekaHttpClientDecorator.java:134) [eureka-client-1.6.2.jar:1.6.2]
at com.netflix.discovery.DiscoveryClient.getAndStoreFullRegistry(DiscoveryClient.java:1013) [eureka-client-1.6.2.jar:1.6.2]
at com.netflix.discovery.DiscoveryClient.fetchRegistry(DiscoveryClient.java:927) [eureka-client-1.6.2.jar:1.6.2]
at com.netflix.discovery.DiscoveryClient.<init>(DiscoveryClient.java:408) [eureka-client-1.6.2.jar:1.6.2]
at com.netflix.discovery.DiscoveryClient.<init>(DiscoveryClient.java:266) [eureka-client-1.6.2.jar:1.6.2]
at com.netflix.discovery.DiscoveryClient.<init>(DiscoveryClient.java:262) [eureka-client-1.6.2.jar:1.6.2]
at org.springframework.cloud.netflix.eureka.CloudEurekaClient.<init>(CloudEurekaClient.java:60) [spring-cloud-netflix-eureka-client-1.3.1.RELEASE.jar:1.3.1.RELEASE]
at org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration$RefreshableEurekaClientConfiguration.eurekaClient(EurekaClientAutoConfiguration.java:228) [spring-cloud-netflix-eureka-client-1.3.1.RELEASE.jar:1.3.1.RELEASE]
at org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration$RefreshableEurekaClientConfiguration$$EnhancerBySpringCGLIB$$a3b657fb.CGLIB$eurekaClient$0(<generated>) [spring-cloud-netflix-eureka-client-1.3.1.RELEASE.jar:1.3.1.RELEASE]
at org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration$RefreshableEurekaClientConfiguration$$EnhancerBySpringCGLIB$$a3b657fb$$FastClassBySpringCGLIB$$1b19fda6.invoke(<generated>) [spring-cloud-netflix-eureka-client-1.3.1.RELEASE.jar:1.3.1.RELEASE]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) [spring-core-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358) [spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration$RefreshableEurekaClientConfiguration$$EnhancerBySpringCGLIB$$a3b657fb.eurekaClient(<generated>) [spring-cloud-netflix-eureka-client-1.3.1.RELEASE.jar:1.3.1.RELEASE]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_112]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_112]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_112]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_112]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1067) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$2.getObject(AbstractBeanFactory.java:345) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.cloud.context.scope.GenericScope$BeanLifecycleWrapper.getBean(GenericScope.java:359) [spring-cloud-context-1.2.2.RELEASE.jar:1.2.2.RELEASE]
at org.springframework.cloud.context.scope.GenericScope.get(GenericScope.java:176) [spring-cloud-context-1.2.2.RELEASE.jar:1.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:340) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:35) [spring-aop-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.cloud.netflix.eureka.serviceregistry.EurekaRegistration.getTargetObject(EurekaRegistration.java:133) [spring-cloud-netflix-eureka-client-1.3.1.RELEASE.jar:1.3.1.RELEASE]
at org.springframework.cloud.netflix.eureka.serviceregistry.EurekaRegistration.getEurekaClient(EurekaRegistration.java:122) [spring-cloud-netflix-eureka-client-1.3.1.RELEASE.jar:1.3.1.RELEASE]
at org.springframework.cloud.netflix.eureka.serviceregistry.EurekaServiceRegistry.maybeInitializeClient(EurekaServiceRegistry.java:56) [spring-cloud-netflix-eureka-client-1.3.1.RELEASE.jar:1.3.1.RELEASE]
at org.springframework.cloud.netflix.eureka.serviceregistry.EurekaServiceRegistry.register(EurekaServiceRegistry.java:37) [spring-cloud-netflix-eureka-client-1.3.1.RELEASE.jar:1.3.1.RELEASE]
at org.springframework.cloud.netflix.eureka.serviceregistry.EurekaAutoServiceRegistration.start(EurekaAutoServiceRegistration.java:73) [spring-cloud-netflix-eureka-client-1.3.1.RELEASE.jar:1.3.1.RELEASE]
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:175) [spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:50) [spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:348) [spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:151) [spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:114) [spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:879) [spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:144) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:545) [spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
at org.spring.springcloud.ProviderApplication.main(ProviderApplication.java:22) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_112]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_112]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_112]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_112]
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) [idea_rt.jar:na]
eureka.server.waitTimeInMsWhenSyncEmpty这个参数具体是什么意思?我测试过程中发现这个好像跟自我保护模式的时间有关系

要回复文章请先登录注册