什么是 Spring 使用 Cloud Foundry Service Registry (Netflix Eureka) 服务的引导配置?

What is Spring Boot config to use Cloud Foundry Service Registry (Netflix Eureka) Service?

我能够在本地设置我自己的 Eureka 服务器并向其注册我的服务,没有任何问题,但是当我使用 Pivotal Cloud Foundry 的 "Service Registry" 服务创建 Eureka 服务器时,我的应用程序没有显示在 "Registered Apps"。

我做了以下事情:

  1. 通过 Cloud Foundry Marketplace 创建了服务注册表。
  2. 将我的应用程序绑定到服务。
  3. 已成功将我的应用程序注册到该服务。

日志:

2017-03-25T11:32:26.994-04:00 [APP/PROC/WEB/0] [OUT] 2017-03-25 15:32:26.994 INFO 13 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Located managed bean 'refreshEndpoint': registering with JMX server as MBean [org.springframework.cloud.endpoint:name=refreshEndpoint,type=RefreshEndpoint]
2017-03-25T11:32:27.426-04:00 [APP/PROC/WEB/0] [OUT] 2017-03-25 15:32:27.424 INFO 13 --- [ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 0
2017-03-25T11:32:27.444-04:00 [APP/PROC/WEB/0] [OUT] 2017-03-25 15:32:27.443 INFO 13 --- [ main] o.s.c.n.eureka.InstanceInfoFactory : Setting initial instance status as: STARTING
2017-03-25T11:32:27.758-04:00 [APP/PROC/WEB/0] [OUT] 2017-03-25 15:32:27.755 INFO 13 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using JSON encoding codec LegacyJacksonJson
2017-03-25T11:32:27.758-04:00 [APP/PROC/WEB/0] [OUT] 2017-03-25 15:32:27.758 INFO 13 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using JSON decoding codec LegacyJacksonJson
2017-03-25T11:32:28.114-04:00 [APP/PROC/WEB/0] [OUT] 2017-03-25 15:32:28.113 INFO 13 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using XML decoding codec XStreamXml
2017-03-25T11:32:28.114-04:00 [APP/PROC/WEB/0] [OUT] 2017-03-25 15:32:28.113 INFO 13 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using XML encoding codec XStreamXml
2017-03-25T11:32:28.682-04:00 [APP/PROC/WEB/0] [OUT] 2017-03-25 15:32:28.678 INFO 13 --- [ main] c.n.d.s.r.aws.ConfigClusterResolver : Resolving eureka endpoints via configuration
2017-03-25T11:32:28.745-04:00 [APP/PROC/WEB/0] [OUT] 2017-03-25 15:32:28.745 INFO 13 --- [ main] com.netflix.discovery.DiscoveryClient : Disable delta property : false
2017-03-25T11:32:28.745-04:00 [APP/PROC/WEB/0] [OUT] 2017-03-25 15:32:28.745 INFO 13 --- [ main] com.netflix.discovery.DiscoveryClient : Single vip registry refresh property : null
2017-03-25T11:32:28.745-04:00 [APP/PROC/WEB/0] [OUT] 2017-03-25 15:32:28.745 INFO 13 --- [ main] com.netflix.discovery.DiscoveryClient : Force full registry fetch : false
2017-03-25T11:32:28.746-04:00 [APP/PROC/WEB/0] [OUT] 2017-03-25 15:32:28.745 INFO 13 --- [ main] com.netflix.discovery.DiscoveryClient : Application is null : false
2017-03-25T11:32:28.746-04:00 [APP/PROC/WEB/0] [OUT] 2017-03-25 15:32:28.745 INFO 13 --- [ main] com.netflix.discovery.DiscoveryClient : Registered Applications size is zero : true
2017-03-25T11:32:28.747-04:00 [APP/PROC/WEB/0] [OUT] 2017-03-25 15:32:28.745 INFO 13 --- [ main] com.netflix.discovery.DiscoveryClient : Application version is -1: true
2017-03-25T11:32:28.761-04:00 [APP/PROC/WEB/0] [OUT] 2017-03-25 15:32:28.747 INFO 13 --- [ main] com.netflix.discovery.DiscoveryClient : Getting all instance registry info from the eureka server
2017-03-25T11:32:29.156-04:00 [APP/PROC/WEB/0] [OUT] 2017-03-25 15:32:29.155 INFO 13 --- [ main] com.netflix.discovery.DiscoveryClient : The response status is 200
2017-03-25T11:32:29.160-04:00 [APP/PROC/WEB/0] [OUT] 2017-03-25 15:32:29.160 INFO 13 --- [ main] com.netflix.discovery.DiscoveryClient : Starting heartbeat executor: renew interval is: 30
2017-03-25T11:32:29.165-04:00 [APP/PROC/WEB/0] [OUT] 2017-03-25 15:32:29.164 INFO 13 --- [ main] c.n.discovery.InstanceInfoReplicator : InstanceInfoReplicator onDemand update allowed rate per min is 4
2017-03-25T11:32:29.170-04:00 [APP/PROC/WEB/0] [OUT] 2017-03-25 15:32:29.169 INFO 13 --- [ main] com.netflix.discovery.DiscoveryClient : Discovery Client initialized at timestamp 1490455949169 with initial instances count: 23
2017-03-25T11:32:29.227-04:00 [APP/PROC/WEB/0] [OUT] 2017-03-25 15:32:29.227 INFO 13 --- [ main] c.n.e.EurekaDiscoveryClientConfiguration : Registering application gym-service with eureka with status UP
2017-03-25T11:32:29.230-04:00 [APP/PROC/WEB/0] [OUT] 2017-03-25 15:32:29.229 INFO 13 --- [ main] com.netflix.discovery.DiscoveryClient : Saw local status change event StatusChangeEvent [timestamp=1490455949229, current=UP, previous=STARTING]
2017-03-25T11:32:29.236-04:00 [APP/PROC/WEB/0] [OUT] 2017-03-25 15:32:29.236 INFO 13 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_GYM-SERVICE/3a2f6286-096c-4a2a-782a-03bab8f020a9: registering service...
2017-03-25T11:32:29.349-04:00 [APP/PROC/WEB/0] [OUT] 2017-03-25 15:32:29.348 INFO 13 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_GYM-SERVICE/3a2f6286-096c-4a2a-782a-03bab8f020a9 - registration status: 204
2017-03-25T11:32:29.570-04:00 [APP/PROC/WEB/0] [OUT] 2017-03-25 15:32:29.569 INFO 13 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-03-25T11:32:29.575-04:00 [APP/PROC/WEB/0] [OUT] 2017-03-25 15:32:29.574 INFO 13 --- [ main] c.n.e.EurekaDiscoveryClientConfiguration : Updating port to 8080
2017-03-25T11:32:29.582-04:00 [APP/PROC/WEB/0] [OUT] 2017-03-25 15:32:29.582 INFO 13 --- [ main] com.campbellonsoftware.orka.GymServer : Started GymServer in 22.159 seconds (JVM running for 24.923)
2017-03-25T11:32:31.135-04:00 [CELL/0] [OUT] Container became healthy
2017-03-25T11:37:28.751-04:00 [APP/PROC/WEB/0] [OUT] 2017-03-25 15:37:28.751 INFO 13 --- [trap-executor-0] c.n.d.s.r.aws.ConfigClusterResolver : Resolving eureka endpoints via configuration

管理-gym.yml 文件:

# Spring properties
spring:
  application:
     name: gym-service  # Service registers under this name
  freemarker:
    enabled: false           # Ignore Eureka dashboard FreeMarker templates
  thymeleaf:
    cache: false             # Allow Thymeleaf templates to be reloaded at runtime
    prefix: classpath:/manage-gym/templates/    # Trailing / mandatory
                             # Template location for this application only

# Map the error path to error template (for Thymeleaf)
error:
  path: /error

# HTTP Server
server: 
  port: 2222   # HTTP (Tomcat) port

# Discovery Server Access
#  1. DEV ONLY: Reduce the lease renewal interval to speed up registration
#  2. Define URL of registration server (defaultZone)
eureka:
  client:
    serviceUrl:
      defaultZone: ${vcap.services.${PREFIX:}eureka.credentials.uri:http://user:${eureka.password:}@${PREFIX:}eureka.${application.domain:cfapps.io}}/eureka/
  instance:
    hostname: ${vcap.application.uris[0]}
    leaseRenewalIntervalInSeconds: 30   # DO NOT DO THIS IN PRODUCTION

主要Class:

package com.campbellonsoftware.orka;

import java.util.logging.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;


@SpringBootApplication
@EnableAutoConfiguration
@EnableDiscoveryClient
public class GymServer {


    protected Logger logger = Logger.getLogger(GymServer.class.getName());

    public static void main(String[] args) {
        System.setProperty("spring.config.name", "manage-gym");
        SpringApplication.run(GymServer.class, args);
    }
}

服务注册表:

我以为服务注册表是一个预设置的尤里卡服务器,就像我在本地 运行 一样,但事实并非如此。

它在幕后使用了eureka,但完全是一个单独的过程,需要添加下面的依赖。

pom.xml:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>io.pivotal.spring.cloud</groupId>
            <artifactId>spring-cloud-services-dependencies</artifactId>
            <version>1.2.0.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
      ...
    </dependencies>
</dependencyManagement>
<dependencies>
    ...
    <dependency>
        <groupId>io.pivotal.spring.cloud</groupId>
        <artifactId>spring-cloud-services-starter-service-registry</artifactId>
    </dependency>
    ...
</dependencies>

结果: