使用应用名称而不是主机名

时间:2016-11-03 05:59:28

标签: spring-cloud netflix-feign

简而言之:

假装客户端尝试拨打http://MyApp/endpoint而不是http://10.0.1.24:8080/endpoint

我有一个应用程序,其中AWS中的实例注册到同一Eureka服务器上的Eureka服务器和假装客户端,试图与应用程序通信(如果您愿意,服务器也是客户端)。

这是服务器上/ eureka / apps输出的示例:

<application>
        <name>JSAPI</name>
        <instance>
            <hostName>10.0.1.24</hostName>
            <app>JSAPI</app>
            <ipAddr>10.0.1.24</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.AmazonInfo">
                <name>Amazon</name>
                <metadata>
                    <public-ipv4>52.91.157.255</public-ipv4>
                    <accountId>831427253318</accountId>
                    <local-hostname>10.0.1.24</local-hostname>
                    <public-hostname>ec2-52-91-157-255.compute-1.amazonaws.com</public-hostname>
                    <instance-id>i-1f86e68c</instance-id>
                    <local-ipv4>10.0.1.24</local-ipv4>
                    <instance-type>m4.large</instance-type>
                    <vpc-id>vpc-3c5e155b</vpc-id>
                    <ami-id>ami-ee5737f9</ami-id>
                    <mac>0e:3c:e0:fa:3f:1c</mac>
                    <availability-zone>us-east-1a</availability-zone>
                </metadata>
            </dataCenterInfo>
            <leaseInfo>
                <renewalIntervalInSecs>30</renewalIntervalInSecs>
                <durationInSecs>90</durationInSecs>
                <registrationTimestamp>1478146401223</registrationTimestamp>
                <lastRenewalTimestamp>1478152383740</lastRenewalTimestamp>
                <evictionTimestamp>0</evictionTimestamp>
                <serviceUpTimestamp>1478146378932</serviceUpTimestamp>
            </leaseInfo>
            <metadata class="java.util.Collections$EmptyMap"/>
            <homePageUrl>http://10.0.1.24:8080/</homePageUrl>
            <statusPageUrl>http://10.0.1.24:8080/info</statusPageUrl>
            <healthCheckUrl>http://10.0.1.24:8080/health</healthCheckUrl>
            <vipAddress>JSAPI</vipAddress>
            <secureVipAddress>JSAPI</secureVipAddress>
            <isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer>
            <lastUpdatedTimestamp>1478146401223</lastUpdatedTimestamp>
            <lastDirtyTimestamp>1478110734306</lastDirtyTimestamp>
            <actionType>ADDED</actionType>
        </instance>...

客户端代码如下:

@FeignClient("JSAPI")
public interface JsapiCustomerClient {

    @RequestMapping(method = RequestMethod.POST, value = "/customers")
    public void createCustomer(@Valid @RequestBody CustomerConfig customer);
}

尝试执行该方法时出现以下错误:

Connection refused executing POST http://JSAPI/customers

我希望它尝试拨打POST http://10.0.1.24:8080/customers

有任何线索吗?这适用于我的本地,在云端失败一次。

由于

编辑:添加pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.knetikcloud</groupId>
        <artifactId>clustermanager</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>clustermanager-server</artifactId>
    <name>clustermanager-server</name>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>com.knetikcloud</groupId>
            <artifactId>clustermanager-core</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-oauth2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk</artifactId>
            <version>1.11.45</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-acm</artifactId>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>    
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!-- Packages the application to run in Elastic Beanstalk -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptor>assembly/beanstalk.xml</descriptor>
                    <finalName>${project.name}</finalName>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <description>A Eureka server paired with JSAPI apps specific status endpoints. The server is used to manage instance and customer status within a JSAPI cluster.</description>
</project>

2 个答案:

答案 0 :(得分:0)

您可以在Eureka配置中指定eureka.instance.preferIpAddress,然后使用ip而不是主机名。

如果您想使用主机名,您可以指定eureka.instance.hostname: ${vcap.application.uris[0]},例如来自环境。

更多信息: http://cloud.spring.io/spring-cloud-static/Brixton.SR6/#_eureka_metadata_for_instances_and_clients

答案 1 :(得分:0)

您可以发布您的eureka客户端设置吗?

我相信您需要将@FeignClient的名称(在本例中为JSAPI)设置为application.yml(或.properties)中的映射,而不是服务名称。

我最近在博客上发表了关于Service registration and discovery using Spring Cloud, Eureka, Ribbon, Feign, ...的文章,但这里是随附源代码的摘录:

@FeignClient(name = ActorsClient.ACTORS_SERVIDE_ID)
public interface ActorsClient {

  String ACTORS_SERVIDE_ID = "the-demo-registration-api-1";
  String ACTORS_ENDPOINT = "/actors";
  String ACTOR_BY_ID_ENDPOINT = "/actors/{id}";
...

演示服务2的application.yml

...
 eureka:
   client:
     registerWithEureka: true
     fetchRegistry: true
     serviceUrl:
       defaultZone: http://localhost:8000/eureka/
   instance:
     hostname: ${hostName}
     statusPageUrlPath: ${management.context-path}/info
     healthCheckUrlPath: ${management.context-path}/health
     preferIpAddress: true
     metadataMap:
       instanceId: ${spring.application.name}:${server.port}
 ...
 the-demo-registration-api-1:
   ribbon:
     # Eureka vipAddress of the target service
     DeploymentContextBasedVipAddresses: demo-registration-api-1

     #listOfServers: localhost:${SERVER.PORT}
     NIWSServerListClassName: com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList

     # Interval to refresh the server list from the source (ms)
     ServerListRefreshInterval: 30000
 ...

您可以看到the-demo-registration-api-1是映射到名称为demo-registration-api-1的注册服务的yml文件中的密钥