Spring Cloud集成了Netflix的微服务开源管理软件,包括Eureka。Eureka用于微服务注册和发现的工具,
下面讲下如何利用Spring Cloud实现Eureka的服务器
本次演示使用的NetBeans IDE8.2,JDK使用的是jdk1.8.0_121
1. 登录http://start.spring.io/,生成Spring项目:
1)Group填上:com.accenture,Artifact填上:eureka-server
2) 点击“Switch to the full version.",下面会展开所有的选项,滑动到“Cloud Discovery”部分,勾选“Eureka Server”
3)点击“Generate Project”按钮,Spring项目文件会自动打包成zip格式,并自动下载到本地。项目文件名称为{Artifact}.zip,如本次生成的项目文件即为eurake-server.zip
2. 使用NetBeans,完成Eureka Server剩余的编码和配置工作
1)打开NetBeans IDE,点击File菜单,选择Import Porject->From ZIP…,选择刚才生成的项目文件导入
2)打开项目文件pom.xml,我们发现项目依赖里已经包含spring-cloud-starter-eureka-server
3) 导入后的项目是有问题的,问题描述是“依赖项没有在本地库”里
4) 在项目名称处点击右键,选择“Resolve Project Problems”
会弹出下面的窗口:
点击“Resolve…”按钮,会启动Priming构建,开始下载项目的依赖包到本地
待所有的依赖包都下载到本地库后,问题窗口的标志会变成绿色
5) 打开主程序(包含main入口的程序),本演示里为“EurekaServerApplication.java”
导入“org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;”,增加“@EnableEurekaServer”类注解
6)配置Eureka Server的端口和访问网址,在默认设置下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为:
server.port=1111
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
3. 到现在为止,Eureka Server的编程和配置都已经完成了,下面我们要运行Eureka-Server,并在浏览器里看下神秘的Eureka到底是什么样子的
1) 点击项目名称,选择Run Maven->Goals
在弹出的对话框里,Goals里填入“spring-boot:run”,点击OK开始运行
在输出框,我们可以看到类似于下面的输出:
2017-02-27 22:37:49.257INFO44248 --- [main]o.s.j.e.a.AnnotationMBeanExporter: Located managed bean 'configurationPropertiesRebinder': registeringwith JMX server as MBean[org.springframework.cloud.context.properties:name=configurationPropertiesRebinder,context=c797fc,type=ConfigurationPropertiesRebinder]
2017-02-27 22:37:49.275INFO44248 --- [main]o.s.j.e.a.AnnotationMBeanExporter: Located managed bean 'refreshEndpoint': registering with JMX server asMBean[org.springframework.cloud.endpoint:name=refreshEndpoint,type=RefreshEndpoint]
2017-02-27 22:37:49.913INFO44248 --- [main]o.s.c.support.DefaultLifecycleProcessor: Starting beans in phase 0
2017-02-27 22:37:49.916INFO44248 --- [Thread-11]o.s.c.n.e.server.EurekaServerBootstrap: Setting the eureka configuration..
2017-02-27 22:37:49.916INFO44248 --- [main]o.s.c.n.e.s.EurekaServiceRegistry: Registering application unknown with eureka with status UP
2017-02-27 22:37:49.917INFO44248 --- [Thread-11]o.s.c.n.e.server.EurekaServerBootstrap: Eureka data center value eureka.datacenter is not set, defaulting todefault
2017-02-27 22:37:49.918INFO44248 --- [Thread-11]o.s.c.n.e.server.EurekaServerBootstrap: Eureka environment value eureka.environment is not set, defaulting totest
2017-02-27 22:37:49.942INFO44248 --- [Thread-11]o.s.c.n.e.server.EurekaServerBootstrap: isAws returned false
2017-02-27 22:37:49.944INFO44248 --- [Thread-11]o.s.c.n.e.server.EurekaServerBootstrap: Initialized server context
2017-02-27 22:37:49.944INFO44248 --- [Thread-11]c.n.e.r.PeerAwareInstanceRegistryImpl: Got 1 instances from neighboring DS node
2017-02-27 22:37:49.944INFO44248 --- [Thread-11]c.n.e.r.PeerAwareInstanceRegistryImpl: Renew threshold is: 1
2017-02-27 22:37:49.945INFO44248 --- [Thread-11]c.n.e.r.PeerAwareInstanceRegistryImpl: Changing status to UP
2017-02-27 22:37:49.963INFO44248 --- [Thread-11]e.s.EurekaServerInitializerConfiguration : Started Eureka Server
2017-02-27 22:37:50.396INFO44248 --- [main]s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 1111(http)
2017-02-27 22:37:50.398INFO44248 --- [main].s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 1111
2017-02-27 22:37:50.407INFO44248 --- [main]com.accenture.EurekaServerApplication: Started EurekaServerApplication in 33.808 seconds (JVM running for46.782)
2)打开浏览器,输入网址“http://localhost:1111/”,大名鼎鼎的Eureka界面就展现在我们面前,激动中。。。。
我们看到现在还没有服务注册到Eureka里
下一部分我们会在NetBeans IDE开发并实现一个微服务,并将该服务注册到Eureka里,敬请期待