zl程序教程

您现在的位置是:首页 >  后端

当前栏目

非java应用,如何使用eureka的rest api来操作eureka?

JAVAeureka应用API 如何 操作 rest 使用
2023-09-11 14:14:16 时间

其实这是一个非常有意思的问题······

 

对于大多数非开发的运维来说,有需要维护的eureka服务的场景,那么,问题是:如何才能对eureka进行运维?首要的条件是什么呢?

 

没错,就是eureka,了解其中的参数,了解客户端注册的时候,到底注册了哪些的信息,如果对这个信息有非常充分的了解了,那么自然,对于eureka的运行也会有更加深刻的理解,那么接下来的文章中,我们就来看看如何通过eureka的接口来获取应用的信息,以及怎么进行注册、删除。

 

先来看看eureka的提供的一些接口。

 

  • 查询所有应用的实例

 

get /eureka/v2/apps

 

 

<applications>
    <versions__delta>1</versions__delta>
    <apps__hashcode>UP_3_</apps__hashcode>
    <application>
        <name>ZHAIMM2</name>
        <instance>
            <instanceId>nctest-snap-zhaimm</instanceId>
            <hostName>nctest-snap-test-02</hostName>
            <app>ZHAIMM2</app>
            <ipAddr>172.20.58.152</ipAddr>
            <status>UP</status>
            <overriddenstatus>UNKNOWN</overriddenstatus>
            <port enabled="true">8080</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>1659593782745</registrationTimestamp>
                <lastRenewalTimestamp>1659593782745</lastRenewalTimestamp>
                <evictionTimestamp>0</evictionTimestamp>
                <serviceUpTimestamp>1659593297381</serviceUpTimestamp>
            </leaseInfo>
            <metadata class="java.util.Collections$EmptyMap"/>
            <appGroupName>UNKNOWN</appGroupName>
            <homePageUrl>http://nctest-snap-test-02:8080/</homePageUrl>
            <statusPageUrl>http://nctest-snap-test-02:8080/Status</statusPageUrl>
            <healthCheckUrl>http://nctest-snap-test-02:8080/healthcheck</healthCheckUrl>
            <vipAddress>eureka.mydomain.net</vipAddress>
            <isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer>
            <lastUpdatedTimestamp>1659593782745</lastUpdatedTimestamp>
            <lastDirtyTimestamp>1659505507804</lastDirtyTimestamp>
            <actionType>ADDED</actionType>
        </instance>
    </application>
    <application>
        <name>EUREKA</name>
        <instance>
            <instanceId>nctest-snap-test-02</instanceId>
            <hostName>nctest-snap-test-02</hostName>
            <app>EUREKA</app>
            <ipAddr>172.20.58.152</ipAddr>
            <status>UP</status>
            <overriddenstatus>UNKNOWN</overriddenstatus>
            <port enabled="true">8080</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>1659505508457</registrationTimestamp>
                <lastRenewalTimestamp>1659593867486</lastRenewalTimestamp>
                <evictionTimestamp>0</evictionTimestamp>
                <serviceUpTimestamp>1659505507898</serviceUpTimestamp>
            </leaseInfo>
            <metadata class="java.util.Collections$EmptyMap"/>
            <appGroupName>UNKNOWN</appGroupName>
            <homePageUrl>http://nctest-snap-test-02:8080/</homePageUrl>
            <statusPageUrl>http://nctest-snap-test-02:8080/Status</statusPageUrl>
            <healthCheckUrl>http://nctest-snap-test-02:8080/healthcheck</healthCheckUrl>
            <vipAddress>eureka.mydomain.net</vipAddress>
            <isCoordinatingDiscoveryServer>true</isCoordinatingDiscoveryServer>
            <lastUpdatedTimestamp>1659505508457</lastUpdatedTimestamp>
            <lastDirtyTimestamp>1659505507804</lastDirtyTimestamp>
            <actionType>ADDED</actionType>
        </instance>
    </application>
    <application>
        <name>ZHAIMM</name>
        <instance>
            <instanceId>nctest-snap-zhaimm</instanceId>
            <hostName>nctest-snap-test-02</hostName>
            <app>ZHAIMM</app>
            <ipAddr>172.20.58.152</ipAddr>
            <status>UP</status>
            <overriddenstatus>UNKNOWN</overriddenstatus>
            <port enabled="true">8080</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>1659593278568</registrationTimestamp>
                <lastRenewalTimestamp>1659593278568</lastRenewalTimestamp>
                <evictionTimestamp>0</evictionTimestamp>
                <serviceUpTimestamp>1659593261166</serviceUpTimestamp>
            </leaseInfo>
            <metadata class="java.util.Collections$EmptyMap"/>
            <appGroupName>UNKNOWN</appGroupName>
            <homePageUrl>http://nctest-snap-test-02:8080/</homePageUrl>
            <statusPageUrl>http://nctest-snap-test-02:8080/Status</statusPageUrl>
            <healthCheckUrl>http://nctest-snap-test-02:8080/healthcheck</healthCheckUrl>
            <vipAddress>eureka.mydomain.net</vipAddress>
            <isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer>
            <lastUpdatedTimestamp>1659593278568</lastUpdatedTimestamp>
            <lastDirtyTimestamp>1659505507804</lastDirtyTimestamp>
            <actionType>ADDED</actionType>
        </instance>
    </application>
</applications>

查询出来的是所有的应用的注册的信息。

 

  • 查询某个特定的应用

 

get /eureka/v2/apps/<app名字>

 

 

<application>
    <name>EUREKA</name>
    <instance>
        <instanceId>nctest-snap-test-02</instanceId>
        <hostName>nctest-snap-test-02</hostName>
        <app>EUREKA</app>
        <ipAddr>172.20.58.152</ipAddr>
        <status>UP</status>
        <overriddenstatus>UNKNOWN</overriddenstatus>
        <port enabled="true">8080</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>1659505508457</registrationTimestamp>
            <lastRenewalTimestamp>1659593987495</lastRenewalTimestamp>
            <evictionTimestamp>0</evictionTimestamp>
            <serviceUpTimestamp>1659505507898</serviceUpTimestamp>
        </leaseInfo>
        <metadata class="java.util.Collections$EmptyMap"/>
        <appGroupName>UNKNOWN</appGroupName>
        <homePageUrl>http://nctest-snap-test-02:8080/</homePageUrl>
        <statusPageUrl>http://nctest-snap-test-02:8080/Status</statusPageUrl>
        <healthCheckUrl>http://nctest-snap-test-02:8080/healthcheck</healthCheckUrl>
        <vipAddress>eureka.mydomain.net</vipAddress>
        <isCoordinatingDiscoveryServer>true</isCoordinatingDiscoveryServer>
        <lastUpdatedTimestamp>1659505508457</lastUpdatedTimestamp>
        <lastDirtyTimestamp>1659505507804</lastDirtyTimestamp>
        <actionType>ADDED</actionType>
    </instance>
</application>

这样得到的结果,就是具体的某个应用的信息。

 

  • 查询某个应用的某个特定的实例

 

get /eureka/v2/apps/<app名字>/<实例ID>

 

 

<instance>
    <instanceId>nctest-snap-test-02</instanceId>
    <hostName>nctest-snap-test-02</hostName>
    <app>EUREKA</app>
    <ipAddr>172.20.58.152</ipAddr>
    <status>UP</status>
    <overriddenstatus>UNKNOWN</overriddenstatus>
    <port enabled="true">8080</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>1659505508457</registrationTimestamp>
        <lastRenewalTimestamp>1659594497540</lastRenewalTimestamp>
        <evictionTimestamp>0</evictionTimestamp>
        <serviceUpTimestamp>1659505507898</serviceUpTimestamp>
    </leaseInfo>
    <metadata class="java.util.Collections$EmptyMap"/>
    <appGroupName>UNKNOWN</appGroupName>
    <homePageUrl>http://nctest-snap-test-02:8080/</homePageUrl>
    <statusPageUrl>http://nctest-snap-test-02:8080/Status</statusPageUrl>
    <healthCheckUrl>http://nctest-snap-test-02:8080/healthcheck</healthCheckUrl>
    <vipAddress>eureka.mydomain.net</vipAddress>
    <isCoordinatingDiscoveryServer>true</isCoordinatingDiscoveryServer>
    <lastUpdatedTimestamp>1659505508457</lastUpdatedTimestamp>
    <lastDirtyTimestamp>1659505507804</lastDirtyTimestamp>
    <actionType>ADDED</actionType>
</instance>

 

  • 注册某个应用

 

POST /eureka/v2/apps/<应用ID>

 

BODY如下:

<instance>
    <instanceId>zhaimm01</instanceId>
    <hostName>nctest-snap-test-02</hostName>
    <app>ZHAIMMTEST</app>
    <ipAddr>172.20.58.152</ipAddr>
    <status>UP</status>
    <overriddenstatus>UNKNOWN</overriddenstatus>
    <port enabled="true">8080</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>1659505508457</registrationTimestamp>
        <lastRenewalTimestamp>1659593267433</lastRenewalTimestamp>
        <evictionTimestamp>0</evictionTimestamp>
        <serviceUpTimestamp>1659505507898</serviceUpTimestamp>
    </leaseInfo>
    <metadata class="java.util.Collections$EmptyMap"/>
    <appGroupName>UNKNOWN</appGroupName>
    <homePageUrl>http://nctest-snap-test-02:8080/</homePageUrl>
    <statusPageUrl>http://nctest-snap-test-02:8080/Status</statusPageUrl>
    <healthCheckUrl>http://nctest-snap-test-02:8080/healthcheck</healthCheckUrl>
    <vipAddress>eureka.mydomain.net</vipAddress>
    <isCoordinatingDiscoveryServer>true</isCoordinatingDiscoveryServer>
    <lastUpdatedTimestamp>1659505508457</lastUpdatedTimestamp>
    <lastDirtyTimestamp>1659505507804</lastDirtyTimestamp>
    <actionType>ADDED</actionType>
</instance>

 

 

注册中心,可以看到这个应用和对应的实例信息。

 

多个实例,循环执行上面的注册过程就可以了。

 

 

  • 删除某个应用的某个实例

 

DELETE /eureka/v2/apps/appID/instanceID

 

 

当所有的实例,都被删除后,应用也就不存在了。

 

 

OK,如果以上的东西,你都掌握了,通过api的方式,就可以轻而易举的操作eureka了。

 

对于非java应用,可以轻松的通过eureka来实现服务、发现了。