第二章:微服务与Spring Cloud Eureka 上篇
一、Eureka 简介
服务发现技术选型
二、Spring Cloud Eureka 入门案例
1、在浏览器输入:http://localhost:8761/
2、在浏览器输入:http://localhost:8761/eureka/apps
<applications><versions__delta>1</versions__delta><apps__hashcode>UP_1_</apps__hashcode><application><name>DEMO-CLIENT1</name><instance><instanceId>LAH:demo-client1:8081</instanceId><hostName>LAH</hostName><app>DEMO-CLIENT1</app><ipAddr>192.168.118.30</ipAddr><status>UP</status><overriddenstatus>UNKNOWN</overriddenstatus><port enabled="true">8081</port><securePort enabled="false">443</securePort><countryId>1</countryId><dataCenterInfo class="com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo"><name>MyOwn</name></dataCenterInfo><leaseInfo><renewalIntervalInSecs>30</renewalIntervalInSecs><durationInSecs>90</durationInSecs><registrationTimestamp>1541662811411</registrationTimestamp><lastRenewalTimestamp>1541662961393</lastRenewalTimestamp><evictionTimestamp>0</evictionTimestamp><serviceUpTimestamp>1541660845280</serviceUpTimestamp></leaseInfo><metadata><management.port>8081</management.port><jmx.port>55850</jmx.port></metadata><homePageUrl>http://LAH:8081/</homePageUrl><statusPageUrl>http://LAH:8081/actuator/info</statusPageUrl><healthCheckUrl>http://LAH:8081/actuator/health</healthCheckUrl><vipAddress>demo-client1</vipAddress><secureVipAddress>demo-client1</secureVipAddress><isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer><lastUpdatedTimestamp>1541662811412</lastUpdatedTimestamp><lastDirtyTimestamp>1541662811343</lastDirtyTimestamp><actionType>ADDED</actionType></instance></application></applications>
三、 REST API实例
1、查询所有应用示例
1、命令
curl -i 192.168.118.30:8761/eureka/apps
2、输出
1 HTTP/1.1 200 2 Content-Type: application/xml 3 Transfer-Encoding: chunked 4 Date: Thu, 08 Nov 2018 08:40:36 GMT 5 6 <applications> 7 <versions__delta>1</versions__delta> 8 <apps__hashcode>UP_1_</apps__hashcode> 9 <application> 10 <name>DEMO-CLIENT1</name> 11 <instance> 12 <instanceId>LAH:demo-client1:8081</instanceId> 13 <hostName>LAH</hostName> 14 <app>DEMO-CLIENT1</app> 15 <ipAddr>192.168.118.30</ipAddr> 16 <status>UP</status> 17 <overriddenstatus>UNKNOWN</overriddenstatus> 18 <port enabled="true">8081</port> 19 <securePort enabled="false">443</securePort> 20 <countryId>1</countryId> 21 <dataCenterInfo class="com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo"> 22 <name>MyOwn</name> 23 </dataCenterInfo> 24 <leaseInfo> 25 <renewalIntervalInSecs>30</renewalIntervalInSecs> 26 <durationInSecs>90</durationInSecs> 27 <registrationTimestamp>1541662811411</registrationTimestamp> 28 <lastRenewalTimestamp>1541666502900</lastRenewalTimestamp> 29 <evictionTimestamp>0</evictionTimestamp> 30 <serviceUpTimestamp>1541660845280</serviceUpTimestamp> 31 </leaseInfo> 32 <metadata> 33 <management.port>8081</management.port> 34 <jmx.port>55850</jmx.port> 35 </metadata> 36 <homePageUrl>http://LAH:8081/</homePageUrl> 37 <statusPageUrl>http://LAH:8081/actuator/info</statusPageUrl> 38 <healthCheckUrl>http://LAH:8081/actuator/health</healthCheckUrl> 39 <vipAddress>demo-client1</vipAddress> 40 <secureVipAddress>demo-client1</secureVipAddress> 41 <isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer> 42 <lastUpdatedTimestamp>1541662811412</lastUpdatedTimestamp> 43 <lastDirtyTimestamp>1541662811343</lastDirtyTimestamp> 44 <actionType>ADDED</actionType> 45 </instance> 46 </application>
2、根据appid查询
1、命令
curl -i 192.168.118.30:8761/eureka/apps/DEMO-CLIENT1
2、输出结果
1 HTTP/1.1 200 2 Content-Type: application/xml 3 Transfer-Encoding: chunked 4 Date: Thu, 08 Nov 2018 09:00:54 GMT 5 6 <application> 7 <name>DEMO-CLIENT1</name> 8 <instance> 9 <instanceId>LAH:demo-client1:8081</instanceId> 10 <hostName>LAH</hostName> 11 <app>DEMO-CLIENT1</app> 12 <ipAddr>192.168.118.30</ipAddr> 13 <status>UP</status> 14 <overriddenstatus>UNKNOWN</overriddenstatus> 15 <port enabled="true">8081</port> 16 <securePort enabled="false">443</securePort> 17 <countryId>1</countryId> 18 <dataCenterInfo class="com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo"> 19 <name>MyOwn</name> 20 </dataCenterInfo> 21 <leaseInfo> 22 <renewalIntervalInSecs>30</renewalIntervalInSecs> 23 <durationInSecs>90</durationInSecs> 24 <registrationTimestamp>1541662811411</registrationTimestamp> 25 <lastRenewalTimestamp>1541667733506</lastRenewalTimestamp> 26 <evictionTimestamp>0</evictionTimestamp> 27 <serviceUpTimestamp>1541660845280</serviceUpTimestamp> 28 </leaseInfo> 29 <metadata> 30 <management.port>8081</management.port> 31 <jmx.port>55850</jmx.port> 32 </metadata> 33 <homePageUrl>http://LAH:8081/</homePageUrl> 34 <statusPageUrl>http://LAH:8081/actuator/info</statusPageUrl> 35 <healthCheckUrl>http://LAH:8081/actuator/health</healthCheckUrl> 36 <vipAddress>demo-client1</vipAddress> 37 <secureVipAddress>demo-client1</secureVipAddress> 38 <isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer> 39 <lastUpdatedTimestamp>1541662811412</lastUpdatedTimestamp> 40 <lastDirtyTimestamp>1541662811343</lastDirtyTimestamp> 41 <actionType>ADDED</actionType> 42 </instance>
3、查询不到的返回结果
[root@kakfa ~]# curl -i 192.168.118.30:8761/eureka/apps/DEMO-CLIENT2 HTTP/1.1 404 Content-Type: application/xml Content-Length: 0 Date: Thu, 08 Nov 2018 09:02:31 GMT
3、根据appid及instanceld查询
[root@kakfa ~]# curl -i 192.168.118.30:8761/eureka/apps/DEMO-CLIENT2/notfound HTTP/1.1 404 Content-Type: application/xml Content-Length: 0 Date: Thu, 08 Nov 2018 09:05:30 GMT
4、根据instanceld查询
[root@ccb-manage ccb-backend-8080]# curl -i http://192.168.118.30:8761/eureka/apps/instances/192.168.0.142:8090 HTTP/1.1 401 WWW-Authenticate: Basic realm="Spring" X-Content-Type-Options: nosniff X-XSS-Protection: 1; mode=block Cache-Control: no-cache, no-store, max-age=0, must-revalidate Pragma: no-cache Expires: 0 X-Frame-Options: DENY Strict-Transport-Security: max-age=31536000 ; includeSubDomains Content-Type: application/json;charset=UTF-8 Transfer-Encoding: chunked Date: Thu, 08 Nov 2018 09:15:24 GMT {"timestamp":1541668524776,"status":401,"error":"Unauthorized","message":"Full authentication is required to access this resource","path":"/eureka/apps/instances/192.168.0.134:8090"} his resource","path":"/eureka/apps/instances/192.168.0.142:8080"}
查询不到返回的结果
[root@kakfa ~]# curl -i 192.168.118.30:8761/eureka/apps/instances/LAH:demo-client1:8081 HTTP/1.1 404 Content-Type: application/xml Content-Length: 0 Date: Thu, 08 Nov 2018 09:07:40 GM
5、注册新的应用实例
6、注销应用实例
curl -i -X http://localhost:8761/eureka/apps/client2/client2:8080
7、暂停下线应用实例
curl -i -X PUT http://localhost:8761/eureka/apps/demo-client1/10.2.238.223:demo-client:8081/status\?value\=OUT_OF_SERVICE
找不到该实例
[f:\~]$ curl -i -X PUT http://localhost:8761/eureka/apps/demo-client1/10.2.238.223:demo-client:8081/status\?value\=OUT_OF_SERVICE % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0 HTTP/1.1 400 Transfer-Encoding: chunked Date: Thu, 08 Nov 2018 09:52:22 GMT Connection: close
8、恢复应用实例
curl -i -X DELETE http://localhost:8761/eureka/apps/demo-client1/10.2.238.223:demo-client:8081/status
9、应用实例发送心跳
curl -i -X PUT http://localhost:8761/eureka/apps/demo-client1/10.2.238.223:demo-client:8081
10、修改应用实例元数据
curl -i -X PUT http://localhost:8761/eureka/apps/demo-client1/10.2.238.223:demo-client:8081/metadata\?profile\=canary
设置metadata,其中keyprofile,value为canary
作者:罗阿红
出处:http://www.cnblogs.com/luoahong/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。