《重新定义Spring Cloud实战》——2.3.2 REST API实例

举报
华章计算机 发表于 2019/06/04 14:21:38 2019/06/04
【摘要】 本书摘自《重新定义Spring Cloud实战》——书中第2章,第2.3.2节,作者是许进、叶志远、钟尊发、蔡波斯、方志朋、郭芳碧、朱德明。

2.3.2 REST API实例

1.查询所有应用实例

HTTP/1.1 200

Content-Type: application/xml

Transfer-Encoding: chunked

Date: Tue, 19 Jun 2018 06:22:56 GMT


<applications>

    <versions__delta>1</versions__delta>

    <apps__hashcode>UP_1_</apps__hashcode>

    <application>

        <name>DEMO-CLIENT1</name>

        <instance>

            <instanceId>10.2.238.223:demo-client1:8081</instanceId>

            <hostName>10.2.238.223</hostName>

            <app>DEMO-CLIENT1</app>

            <ipAddr>10.2.238.223</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>1529377552795</registrationTimestamp>


        <lastRenewalTimestamp>1529389437739</lastRenewalTimestamp>

                <evictionTimestamp>0</evictionTimestamp>

                <serviceUpTimestamp>1529377552795</serviceUpTimestamp>

            </leaseInfo>

            <metadata>

                <management.port>8081</management.port>

            </metadata>

            <homePageUrl>http://10.2.238.223:8081/</homePageUrl>


<statusPageUrl>http://10.2.238.223:8081/actuator/info</statusPageUrl>


<healthCheckUrl>http://10.2.238.223:8081/actuator/health</healthCheckUrl>

            <vipAddress>demo-client1</vipAddress>

            <secureVipAddress>demo-client1</secureVipAddress>


<isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer>

            <lastUpdatedTimestamp>1529377552795</lastUpdatedTimestamp>

            <lastDirtyTimestamp>1529377552665</lastDirtyTimestamp>

            <actionType>ADDED</actionType>

        </instance>

    </application>

</applications>

2.根据appId查询

curl -i http://localhost:8761/eureka/apps/demo-client1


HTTP/1.1 200

Content-Type: application/xml

Transfer-Encoding: chunked

Date: Tue, 19 Jun 2018 06:28:34 GMT


<application>

    <name>DEMO-CLIENT1</name>

    <instance>

        <instanceId>10.2.238.223:demo-client1:8081</instanceId>

        <hostName>10.2.238.223</hostName>

        <app>DEMO-CLIENT1</app>

        <ipAddr>10.2.238.223</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>1529377552795</registrationTimestamp>

            <lastRenewalTimestamp>1529389797866</lastRenewalTimestamp>

            <evictionTimestamp>0</evictionTimestamp>

            <serviceUpTimestamp>1529377552795</serviceUpTimestamp>

        </leaseInfo>

        <metadata>

            <management.port>8081</management.port>

        </metadata>

        <homePageUrl>http://10.2.238.223:8081/</homePageUrl>

        <statusPageUrl>http://10.2.238.223:8081/actuator/info</statusPageUrl>


        <healthCheckUrl>http://10.2.238.223:8081/actuator/health</healthCheckUrl>

        <vipAddress>demo-client1</vipAddress>

        <secureVipAddress>demo-client1</secureVipAddress>

        <isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer>

        <lastUpdatedTimestamp>1529377552795</lastUpdatedTimestamp>

        <lastDirtyTimestamp>1529377552665</lastDirtyTimestamp>

        <actionType>ADDED</actionType>

    </instance>

</application>

查询不到则返回如下结果:

curl -i http://localhost:8761/eureka/apps/demo

HTTP/1.1 404

Content-Type: application/xml

Content-Length: 0

Date: Tue, 19 Jun 2018 06:38:14 GMT

3.根据appId及instanceId查询

查询出数据时跟上面的返回一致,查找不到则返回如下结果:

curl -i http://localhost:8761/eureka/apps/demo-client1/notfound

HTTP/1.1 404

Content-Type: application/xml

Content-Length: 0

Date: Tue, 19 Jun 2018 06:33:42 GMT

4.根据instanceId查询

curl -i http://localhost:8761/eureka/instances/10.2.238.223:demo-client1:8081

HTTP/1.1 200

Content-Type: application/xml

Transfer-Encoding: chunked

Date: Tue, 19 Jun 2018 06:36:09 GMT


<instance>

    <instanceId>10.2.238.223:demo-client1:8081</instanceId>

    <hostName>10.2.238.223</hostName>

    <app>DEMO-CLIENT1</app>

    <ipAddr>10.2.238.223</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>1529377552795</registrationTimestamp>

        <lastRenewalTimestamp>1529390248015</lastRenewalTimestamp>

        <evictionTimestamp>0</evictionTimestamp>

        <serviceUpTimestamp>1529377552795</serviceUpTimestamp>

    </leaseInfo>

    <metadata>

        <management.port>8081</management.port>

    </metadata>

    <homePageUrl>http://10.2.238.223:8081/</homePageUrl>

    <statusPageUrl>http://10.2.238.223:8081/actuator/info</statusPageUrl>


<healthCheckUrl>http://10.2.238.223:8081/actuator/health</healthCheckUrl>

    <vipAddress>demo-client1</vipAddress>

    <secureVipAddress>demo-client1</secureVipAddress>

    <isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer>

    <lastUpdatedTimestamp>1529377552795</lastUpdatedTimestamp>

    <lastDirtyTimestamp>1529377552665</lastDirtyTimestamp>

    <actionType>ADDED</actionType>

</instance>

查询不到则返回如下结果:

curl -i http://localhost:8761/eureka/instances/demo-instance-id

HTTP/1.1 404

Content-Type: application/xml

Content-Length: 0

Date: Tue, 19 Jun 2018 06:36:51 GMT

5.注册新应用实例

xml格式提交的实例如下:

curl -i -H "Content-Type: application/xml" -H "Content-Length: 773" -H "Accept-Encoding: gzip" -X POST -d '<instance>

    <instanceId>client2:8082</instanceId>

    <hostName>127.0.0.1</hostName>

    <app>CLIENT2</app>

    <ipAddr>127.0.0.1</ipAddr>

    <status>UP</status>

    <overriddenstatus>UNKNOWN</overriddenstatus>

    <port enabled="true">8082</port>

    <securePort enabled="false">7002</securePort>

    <countryId>1</countryId>

    <dataCenterInfo class="com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo">

        <name>MyOwn</name>

    </dataCenterInfo>

    <metadata class="java.util.Collections$EmptyMap"/>

    <vipAddress>client2</vipAddress>

    <secureVipAddress>client2</secureVipAddress>

    <isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer>

    <lastUpdatedTimestamp>1529397327986</lastUpdatedTimestamp>

    <lastDirtyTimestamp>1529397327986</lastDirtyTimestamp>

</instance>' http://localhost:8761/eureka/apps/client2

HTTP/1.1 204

Content-Type: application/xml

Date: Tue, 19 Jun 2018 08:35:59 GMT

json格式提交的实例如下:

curl -i -H "Content-Type: application/json" -X POST -d '{

    "instance": {

        "instanceId": "client2:8082",

        "app": "client2",

        "appGroupName": null,

        "ipAddr": "127.0.0.1",

        "sid": "na",

        "homePageUrl": null,

        "statusPageUrl": null,

        "healthCheckUrl": null,

        "secureHealthCheckUrl": null,

        "vipAddress": "client2",

        "secureVipAddress": "client2",

        "countryId": 1,

        "dataCenterInfo": {

            "@class": "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo",

            "name": "MyOwn"

        },

        "hostName": "127.0.0.1",

        "status": "UP",

        "leaseInfo": null,

        "isCoordinatingDiscoveryServer": false,

        "lastUpdatedTimestamp": 1529391461000,

        "lastDirtyTimestamp": 1529391461000,

        "actionType": null,

        "asgName": null,

        "overridden_status": "UNKNOWN",

        "port": {

            "$": 8082,

            "@enabled": "true"

        },

        "securePort": {

            "$": 7002,

            "@enabled": "false"

        },

        "metadata": {

            "@class": "java.util.Collections$EmptyMap"

        }

    }

}' http://localhost:8761/eureka/apps/client2

HTTP/1.1 204

Content-Type: application/xml

Date: Tue, 19 Jun 2018 06:58:18 GMT

6.注销应用实例

curl -i -X DELETE http://localhost:8761/eureka/apps/client2/client2:8082

HTTP/1.1 200

Content-Type: application/xml

Content-Length: 0

Date: Tue, 19 Jun 2018 06:59:31 GMT

7.暂停/下线应用实例

成功则返回:

curl -i -X PUT http://localhost:8761/eureka/apps/demo-client1/10.2.238.223:demo-client1:8081/status\?value\=OUT_OF_SERVICE

HTTP/1.1 200

Content-Type: application/xml

Content-Length: 0

Date: Tue, 19 Jun 2018 07:12:24 GMT

此时Eureka界面示例如图2-3所示。

image.png

图2-3 下线服务时Eureka界面展示示例

找不到该实例返回:

curl -i -X PUT http://localhost:8761/eureka/apps/demo-client1/10.2.238.223:demo-client1:8081/status\?value\=OUT_OF_SERVICE

HTTP/1.1 404

Content-Type: application/xml

Content-Length: 0

Date: Tue, 19 Jun 2018 07:11:17 GMT

8.恢复应用实例

curl -i -X DELETE http://localhost:8761/eureka/apps/demo-client1/10.2.238.223:demo-client1:8081/status

HTTP/1.1 200

Content-Type: application/xml

Content-Length: 0

Date: Tue, 19 Jun 2018 07:14:03 GMT

9.应用实例发送心跳

curl -i -X PUT http://localhost:8761/eureka/apps/demo-client1/10.2.238.223:demo-client1:8081

HTTP/1.1 200

Content-Type: application/xml

Content-Length: 0

Date: Tue, 19 Jun 2018 07:16:38 GMT

10.修改应用实例元数据

curl -i -X PUT http://localhost:8761/eureka/apps/demo-client1/10.2.238.223:demo-client1:8081/metadata\?profile\=canary

HTTP/1.1 200

Content-Type: application/xml

Content-Length: 0

Date: Tue, 19 Jun 2018 07:18:18 GMT

设置metadata,其中key为proflie,value为canary。


【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。