目录
- SpringCloud微服务入门(零),开篇
- SpringCloud微服务入门(一),Eureka注册中心
- SpringCloud微服务入门(二),Eureka Client
- SpringCloud微服务入门(三),Eureka高可用集群
- SpringCloud微服务入门(四),创建服务
- SpringCloud微服务入门(五),Feign实现服务通信
- SpringCloud微服务入门(六),负载均衡Ribbon
- SpringCloud微服务入门(七),服务网关Zuul
- SpringCloud微服务入门(八),服务熔断组件Hystrix
- SpringCloud微服务入门(九),统一配置中心Config
- SpringCloud微服务入门(十),完结篇,是结束也是开始
- 番外篇 - 安装Docker,运行RabbitMQ镜像)
正文
抛开一切业务代码,以最简单的方式创建一个Cureka Client应用,并向注册中心注册。
现在进入IDEA创建项目,依据第一节的经验,我们点击Create New Project后,选择Spring Assistant来快速创建项目,点击Next后填好相关项目信息。
1、这一步跟第一节不一样了,我们要选择Eureka Discovery Client,表示我们创建的是Client项目。
点击Next后,进入下一步,再点击FINISH按钮完成项目的创建。
2、项目创建后,等待Maven加载完资源。我们开始修改代码。找到启动类。在上面加上@EnableDiscoveryClient
注解。这个里跟第一节的eureka-server
启动类一样,都是通过注解来开启功能。往后面学习使用的其它组件也是用同样的方法。
如果现在直接启动eureka-client项目,会看到一堆错误,并且启动失败。我们还没有启动eureka-server。
3、现在我们去启动server端项目,并且将默认的8080端口修改为8761,因为8080端口我们留给client作为默认端口,避免与server端口冲突。运行时修改端口的方法如下:
可爱的你会发现,通过VM options参数:-Dserver.port=8761
修改了server默认端口。在浏览器打开http://localhost:8761
就可以看到我们启动成功了。
4、当Server端启动成功了,再去启动Client项目,可能会出现这样的问题,我先将Console日志贴出:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.5.RELEASE)
2020-03-01 20:52:03.899 INFO 34606 --- [ main] c.j.e.EureaClientApplication : No active profile set, falling back to default profiles: default
2020-03-01 20:52:04.195 INFO 34606 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=220349b1-2385-3a0a-928f-f59d9d77bb1f
2020-03-01 20:52:04.262 WARN 34606 --- [ main] o.s.c.n.a.ArchaiusAutoConfiguration : No spring.application.name found, defaulting to 'application'
2020-03-01 20:52:04.265 WARN 34606 --- [ main] c.n.c.sources.URLConfigurationSource : No URLs will be polled as dynamic configuration sources.
2020-03-01 20:52:04.265 INFO 34606 --- [ main] c.n.c.sources.URLConfigurationSource : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2020-03-01 20:52:04.269 WARN 34606 --- [ main] c.n.c.sources.URLConfigurationSource : No URLs will be polled as dynamic configuration sources.
2020-03-01 20:52:04.269 INFO 34606 --- [ main] c.n.c.sources.URLConfigurationSource : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2020-03-01 20:52:04.450 WARN 34606 --- [ main] ockingLoadBalancerClientRibbonWarnLogger : You already have RibbonLoadBalancerClient on your classpath. It will be used by default. As Spring Cloud Ribbon is in maintenance mode. We recommend switching to BlockingLoadBalancerClient instead. In order to use it, set the value of `spring.cloud.loadbalancer.ribbon.enabled` to `false` or remove spring-cloud-starter-netflix-ribbon from your project.
2020-03-01 20:52:04.483 INFO 34606 --- [ main] o.s.c.n.eureka.InstanceInfoFactory : Setting initial instance status as: STARTING
2020-03-01 20:52:04.547 INFO 34606 --- [ main] com.netflix.discovery.DiscoveryClient : Initializing Eureka in region us-east-1
2020-03-01 20:52:04.763 INFO 34606 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using JSON encoding codec LegacyJacksonJson
2020-03-01 20:52:04.763 INFO 34606 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using JSON decoding codec LegacyJacksonJson
2020-03-01 20:52:04.866 INFO 34606 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using XML encoding codec XStreamXml
2020-03-01 20:52:04.866 INFO 34606 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using XML decoding codec XStreamXml
2020-03-01 20:52:05.050 INFO 34606 --- [ main] c.n.d.s.r.aws.ConfigClusterResolver : Resolving eureka endpoints via configuration
2020-03-01 20:52:05.079 INFO 34606 --- [ main] com.netflix.discovery.DiscoveryClient : Disable delta property : false
2020-03-01 20:52:05.079 INFO 34606 --- [ main] com.netflix.discovery.DiscoveryClient : Single vip registry refresh property : null
2020-03-01 20:52:05.079 INFO 34606 --- [ main] com.netflix.discovery.DiscoveryClient : Force full registry fetch : false
2020-03-01 20:52:05.079 INFO 34606 --- [ main] com.netflix.discovery.DiscoveryClient : Application is null : false
2020-03-01 20:52:05.079 INFO 34606 --- [ main] com.netflix.discovery.DiscoveryClient : Registered Applications size is zero : true
2020-03-01 20:52:05.079 INFO 34606 --- [ main] com.netflix.discovery.DiscoveryClient : Application version is -1: true
2020-03-01 20:52:05.079 INFO 34606 --- [ main] com.netflix.discovery.DiscoveryClient : Getting all instance registry info from the eureka server
2020-03-01 20:52:05.301 INFO 34606 --- [ main] com.netflix.discovery.DiscoveryClient : The response status is 200
2020-03-01 20:52:05.305 INFO 34606 --- [ main] com.netflix.discovery.DiscoveryClient : Starting heartbeat executor: renew interval is: 30
2020-03-01 20:52:05.307 INFO 34606 --- [ main] c.n.discovery.InstanceInfoReplicator : InstanceInfoReplicator onDemand update allowed rate per min is 4
2020-03-01 20:52:05.312 INFO 34606 --- [ main] com.netflix.discovery.DiscoveryClient : Discovery Client initialized at timestamp 1583067125310 with initial instances count: 1
2020-03-01 20:52:05.314 INFO 34606 --- [ main] o.s.c.n.e.s.EurekaServiceRegistry : Registering application UNKNOWN with eureka with status UP
2020-03-01 20:52:05.315 INFO 34606 --- [ main] com.netflix.discovery.DiscoveryClient : Saw local status change event StatusChangeEvent [timestamp=1583067125315, current=UP, previous=STARTING]
2020-03-01 20:52:05.316 INFO 34606 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_UNKNOWN/192.168.124.2: registering service...
2020-03-01 20:52:05.328 INFO 34606 --- [ main] c.j.e.EureaClientApplication : Started EureaClientApplication in 1.908 seconds (JVM running for 2.25)
2020-03-01 20:52:05.332 INFO 34606 --- [extShutdownHook] o.s.c.n.e.s.EurekaServiceRegistry : Unregistering application UNKNOWN with eureka with status DOWN
2020-03-01 20:52:05.332 WARN 34606 --- [extShutdownHook] com.netflix.discovery.DiscoveryClient : Saw local status change event StatusChangeEvent [timestamp=1583067125332, current=DOWN, previous=UP]
2020-03-01 20:52:05.336 INFO 34606 --- [extShutdownHook] com.netflix.discovery.DiscoveryClient : Shutting down DiscoveryClient ...
2020-03-01 20:52:05.353 INFO 34606 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_UNKNOWN/192.168.124.2 - registration status: 204
2020-03-01 20:52:05.353 INFO 34606 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_UNKNOWN/192.168.124.2: registering service...
2020-03-01 20:52:05.359 INFO 34606 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_UNKNOWN/192.168.124.2 - registration status: 204
2020-03-01 20:52:05.360 INFO 34606 --- [extShutdownHook] com.netflix.discovery.DiscoveryClient : Unregistering ...
2020-03-01 20:52:05.369 INFO 34606 --- [extShutdownHook] com.netflix.discovery.DiscoveryClient : DiscoveryClient_UNKNOWN/192.168.124.2 - deregister status: 200
2020-03-01 20:52:05.394 INFO 34606 --- [extShutdownHook] com.netflix.discovery.DiscoveryClient : Completed shut down of DiscoveryClient
从日志看,Client向Server项目注册成功了,但是没多久又Unregistering了。应用也随即退出了。解决方法时,你只需要在Client项目的pom.xml中加上spring-boot-starter-web
依赖就可以了。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
再重新启动Client项目就没问题了。下面我们再次访问EurekaServer的Web界面,会发现Client出现在Server的列表中了。
UNKNOWN就是我们向Server注册的Client。但是为什么出现两个呢?
5、修改应用名称
上一节笔者有提到,Eureka Server自身也是一个Client。所以也会向Server(自己)注册。这样就出现了上图中两个UNKNOWN的情况。可以通过修改application.properties文件来更改应用名。
先找到Server项目的application.properties文件,加上以下配置:
spring.application.name=eureka-server
再找到Client项目的application.properties文件,加上以下配置:
spring.application.name=eureka-client
分别重启Server和Client项目,再刷新http://localhost:8761
页面,就可以看到UNKNOWN没了,并且出现了两个应用在列表中。
我们在写代码时特别要注意,一定要配置应用名称。
配置应用名称还有一种方式,就是eureka.instance.appname
,但这个配置的优先级没有spring.application.name
高,一般直接使用spring.application.name
就可以了。
有没有感觉很简单,没写啥代码,应用注册就做好了,SpringCloud并没有想象中的那么难。
6、配置Client注册地址
你可能会有些疑惑,Client是如何找到Server并注册的。因为笔者除了开启了一个注解,其它啥也没干,Client怎么就注册成功了。
因为Cureka Client默认会去找http://localhost:8761/eureka/
进行注册。如果你想了解经过,仔细观察Client项目的运行日志就可以看到。
那如果要手动配置怎么搞?在Client的application.properties文件中加入注册配置就可以了。像这样指定注册地址
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
本节学习分享就到这里为止,下一节分享Eureka Server高可用集群。用粗暴一点的话说就是,一个注册中心挂掉了,怎么让另外一个来抗着。