向Eureka注册Web服务

时间:2015-05-08 17:07:58

标签: java web-services netflix-eureka

我试图使用Eureka(Netflix)服务探索服务发现。我已按照下面显示的wiki链接中提到的步骤进行操作:

  https://github.com/Netflix/eureka

Eureka服务正常运行。

接下来,在读取xml架构并将内容类型作为JSON发送后,我尝试使用postman客户端使用POST方法注册服务,如twiki中所述。

XML

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xsd:element name="instance">
        <xsd:complexType>
            <xsd:all>
                <!-- hostName in ec2 should be the public dns name, within ec2 public dns name will
                     always resolve to its private IP -->
                <xsd:element name="hostName" type="xsd:string" />
                <!-- app name
                     Instructions for adding a new app name - <a _jive_internal="true" href="/clearspace/docs/DOC-20965" target="_blank">http://wiki.netflix.com/clearspace/docs/DOC-20965</a> -->
                <xsd:element name="app" type="xsd:string" />
                <xsd:element name="ipAddr" type="xsd:string" />
                <xsd:element name="vipAddress" type="xsd:string" />
                <xsd:element name="secureVipAddress" type="xsd:string" />
                <xsd:element name="status" type="statusType" />
                <xsd:element name="port" type="xsd:positiveInteger" minOccurs="0" />
                <xsd:element name="securePort" type="xsd:positiveInteger" />
                <xsd:element name="homePageUrl" type="xsd:string" />
                <xsd:element name="statusPageUrl" type="xsd:string" />
                <xsd:element name="healthCheckUrl" type="xsd:string" />
               <xsd:element ref="dataCenterInfo" minOccurs="1" maxOccurs="1" />
                <!-- optional -->
                <xsd:element ref="leaseInfo" minOccurs="0"/>
                <!-- optional app specific metadata -->
                <xsd:element name="metadata" type="appMetadataType" minOccurs="0" />
            </xsd:all>
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="dataCenterInfo">
        <xsd:complexType>
             <xsd:all>
                 <xsd:element name="name" type="dcNameType" />
                 <!-- metadata is only required if name is Amazon -->
                 <xsd:element name="metadata" type="amazonMetdataType" minOccurs="0"/>
             </xsd:all>
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="leaseInfo">
        <xsd:complexType>
            <xsd:all>
                <!-- (optional) if you want to change the length of lease - default if 90 secs -->
                <xsd:element name="evictionDurationInSecs" minOccurs="0"  type="xsd:positiveInteger"/>
            </xsd:all>
        </xsd:complexType>
    </xsd:element>

    <xsd:simpleType name="dcNameType">
        <!-- Restricting the values to a set of value using 'enumeration' -->
        <xsd:restriction base = "xsd:string">
            <xsd:enumeration value = "MyOwn"/>
            <xsd:enumeration value = "Amazon"/>
        </xsd:restriction>
    </xsd:simpleType>

    <xsd:simpleType name="statusType">
        <!-- Restricting the values to a set of value using 'enumeration' -->
        <xsd:restriction base = "xsd:string">
            <xsd:enumeration value = "UP"/>
            <xsd:enumeration value = "DOWN"/>
            <xsd:enumeration value = "STARTING"/>
            <xsd:enumeration value = "OUT_OF_SERVICE"/>
            <xsd:enumeration value = "UNKNOWN"/>
        </xsd:restriction>
    </xsd:simpleType>

    <xsd:complexType name="amazonMetdataType">
        <!-- From <a class="jive-link-external-small" href="http://docs.amazonwebservices.com/AWSEC2/latest/DeveloperGuide/index.html?AESDG-chapter-instancedata.html" target="_blank">http://docs.amazonwebservices.com/AWSEC2/latest/DeveloperGuide/index.html?AESDG-chapter-instancedata.html</a> -->
        <xsd:all>
            <xsd:element name="ami-launch-index" type="xsd:string" />
            <xsd:element name="local-hostname" type="xsd:string" />
            <xsd:element name="availability-zone" type="xsd:string" />
            <xsd:element name="instance-id" type="xsd:string" />
            <xsd:element name="public-ipv4" type="xsd:string" />
            <xsd:element name="public-hostname" type="xsd:string" />
            <xsd:element name="ami-manifest-path" type="xsd:string" />
            <xsd:element name="local-ipv4" type="xsd:string" />
            <xsd:element name="hostname" type="xsd:string"/>       
            <xsd:element name="ami-id" type="xsd:string" />
            <xsd:element name="instance-type" type="xsd:string" />
        </xsd:all>
    </xsd:complexType>

    <xsd:complexType name="appMetadataType">
        <xsd:sequence>
            <!-- this is optional application specific name, value metadata -->
            <xsd:any minOccurs="0" maxOccurs="unbounded" processContents="skip"/>
        </xsd:sequence>
    </xsd:complexType>

</xsd:schema>

注册请求的参数如下所示:

网址:http://tpdeva13:8080/eureka/v2/apps/service1 Content-Type:application / json

以json发送的参数是:

 {  
     "hostName":"tpdeva13",
     "app":"service1",
     "ipAddr":"tpdeva13",
     "vipAddress":"tpdeva13",
     "secureVipAddress":"tpdeva13",
     "status":"STARTING",
     "port":"11111"

  }

我不确定我提供的json是否与xml架构保持一致?

当我将发布请求发送到托管eureka的tomcat服务器时,我收到以下错误:

com.thoughtworks.xstream.mapper.CannotResolveClassException: hostName
com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:56)
com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
com.thoughtworks.xstream.mapper.DynamicProxyMapper.realClass(DynamicProxyMapper.java:55)
com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
com.thoughtworks.xstream.mapper.PackageAliasingMapper.realClass(PackageAliasingMapper.java:88)
com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
com.thoughtworks.xstream.mapper.ClassAliasingMapper.realClass(ClassAliasingMapper.java:79)
com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
com.thoughtworks.xstream.mapper.ArrayMapper.realClass(ArrayMapper.java:74)
com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
com.thoughtworks.xstream.mapper.CachingMapper.realClass(CachingMapper.java:45)
com.thoughtworks.xstream.core.util.HierarchicalStreams.readClassType(HierarchicalStreams.java:29)
com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:133)
com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32)
com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1052)
com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1036)
com.thoughtworks.xstream.XStream.fromXML(XStream.java:921)
com.netflix.discovery.converters.EntityBodyConverter.read(EntityBodyConverter.java:57)
com.netflix.discovery.provider.DiscoveryJerseyProvider.readFrom(DiscoveryJerseyProvider.java:94)
com.sun.jersey.spi.container.ContainerRequest.getEntity(ContainerRequest.java:474)
com.sun.jersey.server.impl.model.method.dispatch.EntityParamDispatchProvider$EntityInjectable.getValue(EntityParamDispatchProvider.java:123)
com.sun.jersey.server.impl.inject.InjectableValuesProvider.getInjectableValues(InjectableValuesProvider.java:46)
com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$EntityParamInInvoker.getParams(AbstractResourceMethodDispatchProvider.java:153)
com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$VoidOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:166)
com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
com.sun.jersey.server.impl.uri.rules.SubLocatorRule.accept(SubLocatorRule.java:134)
com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)
com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)
com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
com.sun.jersey.spi.container.servlet.ServletContainer.doFilter(ServletContainer.java:895)
com.sun.jersey.spi.container.servlet.ServletContainer.doFilter(ServletContainer.java:843)
com.sun.jersey.spi.container.servlet.ServletContainer.doFilter(ServletContainer.java:804)
com.netflix.eureka.ServerRequestAuthFilter.doFilter(ServerRequestAuthFilter.java:34)
com.netflix.eureka.StatusFilter.doFilter(StatusFilter.java:68

Web服务将使用C ++编写,因此我需要使用eureka公开的REST API来注册,取消注册我的服务。

有人可以帮忙。我坚持这个。

eureka服务器正在开发模式下运行。

谢谢!

3 个答案:

答案 0 :(得分:1)

port有这种语法 &#34; port&#34;:{&#34; $&#34;:8001,&#34; @ enabled&#34;:&#34; true&#34; }

并且还需要dataCenterInfo &#34; dataCenterInfo&#34;:{&#34; name&#34;:&#34; MyOwn&#34; }

答案 1 :(得分:1)

您看到的错误是由于缺少外部instance字段而应该包含您定义的字段。

{
    "instance": {
        "hostName":"tpdeva13",
        "app":"service1",
        "ipAddr":"tpdeva13",
        "vipAddress":"tpdeva13",
        "secureVipAddress":"tpdeva13",
        "status":"STARTING",
        "port":"11111"
    }
}

答案 2 :(得分:0)

Vanderhoef已经回答了这个问题,但我使用eureka的java客户端得到了完全相同的错误。经过长时间的调试后,我发现原2.4.3jackson2.2.1覆盖了,并且没有尊重根元素序列化&#34;实例&#34;。< / p>