Browse Source

Initial version with client autoconfig

pull/6/head
Dave Syer 10 years ago
commit
5ee8c8c846
  1. 10
      .gitignore
  2. 51
      pom.xml
  3. 61
      src/main/java/org/springframework/platform/netflix/eureka/EurekaClientAutoConfiguration.java
  4. 170
      src/main/java/org/springframework/platform/netflix/eureka/EurekaClientConfigBean.java
  5. 120
      src/main/java/org/springframework/platform/netflix/eureka/EurekaInstanceConfigBean.java
  6. 2
      src/main/resources/META-INF/spring.factories
  7. 25
      src/test/java/org/springframework/platform/netflix/eureka/sample/Application.java
  8. 18
      src/test/java/org/springframework/platform/netflix/eureka/sample/ApplicationTests.java
  9. 21
      src/test/resources/application.yml

10
.gitignore vendored

@ -0,0 +1,10 @@ @@ -0,0 +1,10 @@
*~
#*
*#
.*#
.classpath
.project
.settings/
.springBeans
target/

51
pom.xml

@ -0,0 +1,51 @@ @@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>org.springframework.platform</groupId>
<artifactId>spring-platform-netflix</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
<name>spring-platform-netflix</name>
<description>Eureka integration project</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.5.BUILD-SNAPSHOT</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.netflix.eureka</groupId>
<artifactId>eureka-client</artifactId>
<version>1.1.135</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.12.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
</properties>
</project>

61
src/main/java/org/springframework/platform/netflix/eureka/EurekaClientAutoConfiguration.java

@ -0,0 +1,61 @@ @@ -0,0 +1,61 @@
/*
* Copyright 2013-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.platform.netflix.eureka;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import com.netflix.appinfo.ApplicationInfoManager;
import com.netflix.appinfo.InstanceInfo.InstanceStatus;
import com.netflix.discovery.DiscoveryManager;
/**
* @author Dave Syer
*
*/
@Configuration
@EnableConfigurationProperties({EurekaClientConfigBean.class, EurekaInstanceConfigBean.class})
public class EurekaClientAutoConfiguration implements ApplicationListener<ContextRefreshedEvent> {
@Autowired
private EurekaClientConfigBean clientConfig;
@Autowired
private EurekaInstanceConfigBean instanceConfig;
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
ApplicationInfoManager.getInstance().setInstanceStatus(InstanceStatus.UP);
}
@PostConstruct
public void init() {
DiscoveryManager.getInstance().initComponent(
instanceConfig,
clientConfig);
}
@PreDestroy
public void close() {
DiscoveryManager.getInstance().shutdownComponent();
}
}

170
src/main/java/org/springframework/platform/netflix/eureka/EurekaClientConfigBean.java

@ -0,0 +1,170 @@ @@ -0,0 +1,170 @@
/*
* Copyright 2013-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.platform.netflix.eureka;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;
import lombok.Data;
import com.netflix.discovery.EurekaClientConfig;
/**
* @author Dave Syer
*
*/
@Data
@ConfigurationProperties("eureka.client")
public class EurekaClientConfigBean implements EurekaClientConfig {
public static final String DEFAULT_ZONE = "defaultZone";
private static final int MINUTES = 60;
private int registryFetchIntervalSeconds = 30;
private int instanceInfoReplicationIntervalSeconds = 30;
private int initialInstanceInfoReplicationIntervalSeconds = 40;
private int eurekaServiceUrlPollIntervalSeconds = 5 * MINUTES;
private String proxyPort;
private String proxyHost;
private int eurekaServerReadTimeoutSeconds = 8;
private int eurekaServerConnectTimeoutSeconds = 5;
private String backupRegistryImpl;
private int eurekaServerTotalConnections = 200;
private int eurekaServerTotalConnectionsPerHost = 50;
private String eurekaServerURLContext;
private String eurekaServerPort;
private String eurekaServerDNSName;
private String region = "us-east-1";
private int eurekaConnectionIdleTimeoutSeconds = 30;
private String registryRefreshSingleVipAddress;
private int heartbeatExecutorThreadPoolSize = 2;
private int cacheRefreshExecutorThreadPoolSize = 2;
private Map<String,String> serviceUrl = new HashMap<String, String>();
private boolean gZipContent = true;
private boolean useDnsForFetchingServiceUrls = false;
private boolean registerWithEureka = true;
private boolean preferSameZoneEureka = true;
private boolean logDeltaDiff;
private boolean disableDelta;
private String fetchRemoteRegionsRegistry;
private Map<String, String> availabilityZones = new HashMap<String, String>();
private boolean filterOnlyUpInstances = true;
private boolean fetchRegistry = true;
@Override
public boolean shouldGZipContent() {
return gZipContent;
}
@Override
public boolean shouldUseDnsForFetchingServiceUrls() {
return useDnsForFetchingServiceUrls;
}
@Override
public boolean shouldRegisterWithEureka() {
return registerWithEureka;
}
@Override
public boolean shouldPreferSameZoneEureka() {
return preferSameZoneEureka;
}
@Override
public boolean shouldLogDeltaDiff() {
return logDeltaDiff;
}
@Override
public boolean shouldDisableDelta() {
return disableDelta;
}
@Override
public String fetchRegistryForRemoteRegions() {
return fetchRemoteRegionsRegistry;
}
@Override
public String[] getAvailabilityZones(String region) {
String value = availabilityZones.get(region);
if (value==null) {
value = DEFAULT_ZONE;
}
return value.split(",");
}
@Override
public List<String> getEurekaServerServiceUrls(String myZone) {
String serviceUrls = serviceUrl .get(myZone);
if (serviceUrls == null || serviceUrls.isEmpty()) {
serviceUrls = serviceUrl.get("default" + myZone);
}
if (serviceUrls != null) {
return Arrays.asList(serviceUrls.split(","));
}
return new ArrayList<String>();
}
@Override
public boolean shouldFilterOnlyUpInstances() {
return filterOnlyUpInstances;
}
@Override
public boolean shouldFetchRegistry() {
return fetchRegistry;
}
}

120
src/main/java/org/springframework/platform/netflix/eureka/EurekaInstanceConfigBean.java

@ -0,0 +1,120 @@ @@ -0,0 +1,120 @@
/*
* Copyright 2013-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.platform.netflix.eureka;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import com.netflix.appinfo.DataCenterInfo;
import com.netflix.appinfo.EurekaInstanceConfig;
/**
* @author Dave Syer
*
*/
@Data
@ConfigurationProperties("eureka.instance")
public class EurekaInstanceConfigBean implements EurekaInstanceConfig {
private static final Log logger = LogFactory.getLog(EurekaInstanceConfigBean.class);
@Getter(AccessLevel.PRIVATE) @Setter(AccessLevel.PRIVATE)
private String[] hostInfo = initHostInfo();
private String appname = "unknown";
private String appGroupName;
private boolean instanceEnabledOnit;
private int nonSecurePort = 80;
private int securePort = 443;
private boolean nonSecurePortEnabled = true;
private boolean securePortEnabled;
private int leaseRenewalIntervalInSeconds = 30;
private int leaseExpirationDurationInSeconds = 90;
private String virtualHostName;
private String secureVirtualHostName;
private String aSGName;
private Map<String, String> metadataMap = new HashMap<>();
private DataCenterInfo dataCenterInfo = new DataCenterInfo() {
@Getter @Setter
private Name name = Name.MyOwn;
};
private String ipAddress = hostInfo[0];
private String statusPageUrlPath = "/info";
private String statusPageUrl;
private String homePageUrlPath = "/";
private String homePageUrl;
private String healthCheckUrlPath = "/health";
private String healthCheckUrl;
private String secureHealthCheckUrl;
private String namespace = "eureka";
private String hostname = hostInfo[1];
@Override
public boolean getSecurePortEnabled() {
return securePortEnabled;
}
private String[] initHostInfo() {
String[] info = new String[2];
try {
info[0] = InetAddress.getLocalHost().getHostAddress();
info[1] = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
logger.error("Cannot get host info", e);
}
return info ;
}
@Override
public String getHostName(boolean refresh) {
return hostname;
}
}

2
src/main/resources/META-INF/spring.factories

@ -0,0 +1,2 @@ @@ -0,0 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.platform.netflix.eureka.EurekaClientAutoConfiguration

25
src/test/java/org/springframework/platform/netflix/eureka/sample/Application.java

@ -0,0 +1,25 @@ @@ -0,0 +1,25 @@
package org.springframework.platform.netflix.eureka.sample;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Configuration
@ComponentScan
@EnableAutoConfiguration
@RestController
public class Application {
@RequestMapping("/")
public String home() {
return "Hello world";
}
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}

18
src/test/java/org/springframework/platform/netflix/eureka/sample/ApplicationTests.java

@ -0,0 +1,18 @@ @@ -0,0 +1,18 @@
package org.springframework.platform.netflix.eureka.sample;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
public class ApplicationTests {
@Test
public void contextLoads() {
}
}

21
src/test/resources/application.yml

@ -0,0 +1,21 @@ @@ -0,0 +1,21 @@
server:
port: 9000
spring:
application:
name: client
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8080/v2/
default.defaultZone: http://localhost:8080/v2/
region: default
availabilityZones:
us-east-1: default
preferSameZoneEureka: true
useDnsForFetchingServiceUrls: false
instance:
nonSecurePort: ${server.port}
appname: ${spring.application.name}
virtualHostName: ${spring.application.name}.mydomain.net
statusPageUrlPath: /info
healthCheckUrlPath: /health
Loading…
Cancel
Save