Spring Cloud 学习之旅 --- 随机端口启动多实例
来源:互联网 发布:php技术文档模板 编辑:程序博客网 时间:2024/06/05 07:35
本文内容很简单,就是使用随机端口来启动多个实例。
还记得之前我们在 Spring Cloud 学习之旅 — 服务注册与发现(二) 学习创建服务提供者的时候,我们写了个配置文件吗?在配置文件中,我们指定了一个 2222
端口作为我们的监听端口,但是往往我们的服务提供者是多实例的,因此如果在单台机器中启动多个实例,不修改监听端口,势必会造成端口冲突,导致启动一个实例后,后续实例无法正常启动。
那么这里有两种解决方法:
(一)配置多个配置文件,通过不同的启动参数设置不同的端口进行实例的运行。
(二)采用随机端口的方式,每次启动时获取一个随机端口进行启动。
第一个方法其实很有效,但是在本文中,介绍的是第二种方法。
在 .properties / .yml
文件中,设置 server.port
属性的值为 ${random.int}
或者设置其值为 0
即可。
下面我们来做一个实验。
我们先启动我们的注册中心和服务消费者,以观察我们的最终结果。(由于注册中心会定时刷新可使用的服务提供者名单,所以修改了服务提供者后,无需等待服务提供者注册后再重启消费者)
实验(一)
采用 ${random.int}
的方式取随机值
修改配置文件,使 server.port
配置的值为 ${random.int}
,具体如下图:
随后将其启动,我们看看控制台的输出。
2017-06-19 19:33:57.321 INFO 10512 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 14572 (http)2017-06-19 19:33:57.322 INFO 10512 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 14572
可以看到其中一条,我们的tomcat监听了14572端口。接着我们打开注册中心看看,具体如下图:
可以看到服务的确正常注册进来了。我们通过上一节的消费者调用一下该服务,看看效果。具体如下图:
但是好像有点事与愿违。我们为什么会调用不到呢?
我们查看一下注册中心中的simple-service的实例情况。我们通过访问注册中心的 /eureka/apps
查看所有注册到注册中心的实例的信息。
<applications> <versions__delta>1</versions__delta> <apps__hashcode>UP_5_</apps__hashcode> <application> <name>SIMPLE-SERVICE</name> <instance> <instanceId>simple-service:-87414967</instanceId> <hostName>localhost</hostName> <app>SIMPLE-SERVICE</app> <ipAddr>172.30.0.177</ipAddr> <status>UP</status> <overriddenstatus>UNKNOWN</overriddenstatus> <port enabled="true">14258</port> <securePort enabled="false">443</securePort> <countryId>1</countryId> <dataCenterInfo class="com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo"> <name>MyOwn</name> </dataCenterInfo> <leaseInfo> <renewalIntervalInSecs>5</renewalIntervalInSecs> <durationInSecs>10</durationInSecs> <registrationTimestamp>1497872037342</registrationTimestamp> <lastRenewalTimestamp>1497872287304</lastRenewalTimestamp> <evictionTimestamp>0</evictionTimestamp> <serviceUpTimestamp>1497872037342</serviceUpTimestamp> </leaseInfo> <metadata class="java.util.Collections$EmptyMap"/> <homePageUrl>http://localhost:14258/</homePageUrl> <statusPageUrl>http://localhost:14258/info</statusPageUrl> <healthCheckUrl>http://localhost:14258/health</healthCheckUrl> <vipAddress>simple-service</vipAddress> <secureVipAddress>simple-service</secureVipAddress> <isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer> <lastUpdatedTimestamp>1497872037342</lastUpdatedTimestamp> <lastDirtyTimestamp>1497872037172</lastDirtyTimestamp> <actionType>ADDED</actionType> </instance> </application></applications>
从上方的 <applications>
节点中的 <application>
中,有一个 <port>
断点,看到端口是 14258
,与我们启动时控制台的 14572
有点不符,那么到底是什么原因导致这样的情况呢?
其实是每次在读取 server.port
属性时,都会获取到 ${random.int}
,然后对其进行解析,即每次读取该属性时都会重新获取一次随机值。因此会出现服务成功注册,但是缺无法调用的情况,根本原因是端口不对。
实验(二)
修改配置文件,使 server.port
配置的值为 0
,具体如下图:
跟上一个实验相同进行启动,看到控制台的输出,监听端口为 57239
(以本人本机启动为例)
2017-06-19 19:48:04.433 INFO 9380 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 57239 (http)2017-06-19 19:48:04.434 INFO 9380 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 57239
同样的,我们先不调用,直接看注册信息。访问注册中心IP端口的 /eureka/apps
查看信息。(此次截取的是部分信息)
<instance> <instanceId>simple-service:663939329</instanceId> <hostName>localhost</hostName> <app>SIMPLE-SERVICE</app> <ipAddr>172.30.0.177</ipAddr> <status>UP</status> <overriddenstatus>UNKNOWN</overriddenstatus> <port enabled="true">57239</port> <securePort enabled="false">443</securePort> <countryId>1</countryId> <dataCenterInfo class="com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo"> <name>MyOwn</name> </dataCenterInfo> <leaseInfo> <renewalIntervalInSecs>5</renewalIntervalInSecs> <durationInSecs>10</durationInSecs> <registrationTimestamp>1497872885163</registrationTimestamp> <lastRenewalTimestamp>1497872915123</lastRenewalTimestamp> <evictionTimestamp>0</evictionTimestamp> <serviceUpTimestamp>1497872885163</serviceUpTimestamp> </leaseInfo> <metadata class="java.util.Collections$EmptyMap"/> <homePageUrl>http://localhost:57239/</homePageUrl> <statusPageUrl>http://localhost:57239/info</statusPageUrl> <healthCheckUrl>http://localhost:57239/health</healthCheckUrl> <vipAddress>simple-service</vipAddress> <secureVipAddress>simple-service</secureVipAddress> <isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer> <lastUpdatedTimestamp>1497872885163</lastUpdatedTimestamp> <lastDirtyTimestamp>1497872885130</lastDirtyTimestamp> <actionType>ADDED</actionType></instance>
此次实例的端口是57239
,与控制台启动的端口相同。我们再调用一下消费者,看看效果。具体如下图:
我们可以看到消费者调用服务恢复正常,因此正确的随机多端口启动实例的方式应该是采用 server.port
为 0
的方式。当然,如果读者能够重写配置文件的解析方法,将实验一中的问题排除掉,那么您当我没说这句话23333
- Spring Cloud 学习之旅 --- 随机端口启动多实例
- 四、Spring Cloud系列:问题-随机端口启动项目无法访问
- Spring Cloud实战小贴士:随机端口
- Spring Cloud 学习之旅 --- 断路器
- Spring-Cloud 学习之旅 --- 快速开始(一)
- Spring-Cloud 学习之旅 --- 快速开始(二)
- Spring Cloud 学习之旅 --- 服务的消费
- Spring cloud config 实例
- Spring Cloud学习笔记之12-Factor
- Spring-Cloud学习之路-版本说明
- Spring-Cloud学习之路-问题记录
- Spring Cloud 之 基础学习资料
- 02.Spring Cloud学习笔记之Spring Boot配置文件详解
- Spring5学习(二)-spring projects之Spring Cloud
- spring cloud学习-什么是Spring Cloud Eureka?
- Spring Cloud入门之Spring Cloud Config
- spring cloud 之Spring Cloud Config 尝鲜
- spring cloud 学习地址
- 微服务,够了 (对微服务的一些客观正名和经验指导)
- springboot整合springSecurity出现的问题,post,delete,put无法使用
- ios 判断当前页面从哪个页面跳转过来的(上个界面是哪个ViewController)
- [PPPOE]PPPD源码分析
- 游戏UI框架设计(三) : 窗体的层级管理
- Spring Cloud 学习之旅 --- 随机端口启动多实例
- <input />调用日历组件 触发键盘输入法
- Java实现-旋转字符串
- CSS-Cascading Style Sheet 层叠样式表
- xml(❤U)
- Easyui小知识点汇总
- MYSQL修改表结构语句 alter
- Go语言学习之html/template包(The way to go)
- 文章标题