Eureka元数据
获取元数据信息
从Eureka Server获取
元数据举例
<application> <name>SERVICE-SMS</name> <instance> <instanceId>30.136.133.11:service-sms:8002</instanceId> <hostName>30.136.133.11</hostName> <app>SERVICE-SMS</app> <ipAddr>30.136.133.11</ipAddr> <status>UP</status> <overriddenstatus>UNKNOWN</overriddenstatus> <port enabled="true">8002</port> <securePort enabled="false">443</securePort> <countryId>1</countryId> <dataCenterInfo class="com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo"> <name>MyOwn</name> </dataCenterInfo> <leaseInfo> <renewalIntervalInSecs>1</renewalIntervalInSecs> <durationInSecs>1</durationInSecs> <registrationTimestamp>1579673388471</registrationTimestamp> <lastRenewalTimestamp>1579673470319</lastRenewalTimestamp> <evictionTimestamp>0</evictionTimestamp> <serviceUpTimestamp>1579673387755</serviceUpTimestamp> </leaseInfo> <metadata> <user.password>root</user.password> <management.port>8002</management.port> <jmx.port>62449</jmx.port> <user.name>root</user.name> </metadata> <homePageUrl>http://30.136.133.11:8002/</homePageUrl> <statusPageUrl>http://30.136.133.11:8002/actuator/info</statusPageUrl> <healthCheckUrl>http://30.136.133.11:8002/actuator/health</healthCheckUrl> <vipAddress>service-sms</vipAddress> <secureVipAddress>service-sms</secureVipAddress> <isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer> <lastUpdatedTimestamp>1579673388471</lastUpdatedTimestamp> <lastDirtyTimestamp>1579673387301</lastDirtyTimestamp> <actionType>ADDED</actionType> </instance> </application>
手写获取元数据
在一个eureka client(api-listen-order)中,写一个controller
import org.springframework.cloud.client.discovery.DiscoveryClient; @RestController @RequestMapping("/service-instance") public class ServiceInstanceController { @Autowired private DiscoveryClient discoveryClient; @GetMapping("/query-by-application-name/{applicationName}") public List<ServiceInstance> getInstance(@PathVariable String applicationName){ return discoveryClient.getInstances(applicationName); } }
- 启动eureka(7900)单节点
- 启动api-listen-order
自己开发
- http://localhost:8084/service-instance/query-by-application-name/api-listen-order
- http://localhost:8084/service-instance/query-by-application-name/service-sms
- 结果看到一堆json信息
- 表示api-listen-order在eureka server中的实例信息
标准元数据和自定义元数据
标准元数据:主机名,ip,端口,健康检查等信息
会被发布到注册表中,用于服务间调用
自定义元数据
eureka.instance.metadata-map
自定义key:自定义value,可以直接在application.properites文件中配置
远程客户端访问。自定义属性。
api-listen-order中ServiceInstanceController,读取instance信息
启动eureka,api-listen-order,
http://localhost:8084/service-instance/query-by-application-name/api-listen-order
{
"host": "127.0.0.1",
"port": 8084,
"metadata": {
"yueyi": "2019",
"user.password": "root",
"management.port": "8084",
"jmx.port": "61378",
"user.name": "root"
},
"secure": false,
"uri": "http://127.0.0.1:8084",
"instanceId": "api-listen-order:30.136.133.11:port",
"serviceId": "API-LISTEN-ORDER",
"instanceInfo": {
"instanceId": "api-listen-order:30.136.133.11:port",
"app": "API-LISTEN-ORDER",
"appGroupName": null,
"ipAddr": "127.0.0.1",
"sid": "na",
"homePageUrl": "http://127.0.0.1:8084/",
"statusPageUrl": "http://127.0.0.1:8084/actuator/info",
"healthCheckUrl": "http://127.0.0.1:8084/actuator/health",
"secureHealthCheckUrl": null,
"vipAddress": "api-listen-order",
"secureVipAddress": "api-listen-order",
"countryId": 1,
"dataCenterInfo": {
"@class": "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo",
"name": "MyOwn"
},
"hostName": "127.0.0.1",
"status": "UP",
"overriddenStatus": "UNKNOWN",
"leaseInfo": {
"renewalIntervalInSecs": 1,
"durationInSecs": 1,
"registrationTimestamp": 1579489514655,
"lastRenewalTimestamp": 1579489524146,
"evictionTimestamp": 0,
"serviceUpTimestamp": 1579489514147
},
"isCoordinatingDiscoveryServer": false,
"metadata": {
"yueyi": "2019",
"user.password": "root",
"management.port": "8084",
"jmx.port": "61378",
"user.name": "root"
},
"lastUpdatedTimestamp": 1579489514655,
"lastDirtyTimestamp": 1579489514111,
"actionType": "ADDED",
"asgName": null
},
"scheme": null
}
自定义元数据,可以结合ribbon 分发规则使用
网关做分发规则选择服务时,可以作为依据
灰度发布,有用,例子
client端:
eureka:
instance:
metadataMap:
tag: pre-prd
调用方
List<ServiceInstance> instances = discoveryClient.getInstances(applicationName); for (ServiceInstance serviceInstance : instances) { Map<String, String> metadata = serviceInstance.getMetadata(); String metaValue = metadata.get("yueyi"); log.info("元数据:"+metaValue); }
获取到元数据
- 分发的时候,根据app客户端传过来的参数:比如加tag=pre,只分发到pre-prd的eureka client。
- 在新发布的app版本带tag参数,就可以实现灰度测试
论读书
睁开眼,书在面前 闭上眼,书在心里
睁开眼,书在面前 闭上眼,书在心里