###Eureka Client configuration for Sample Eureka Client #Properties based configuration for eureka client. The properties specified here is mostly what the users #need to change. All of these can be specified as a java system property with -D option (eg)-Deureka.region=us-east-1 #For additional tuning options refer <url to go here> #Region where eureka is deployed -For AWS specify one of the AWS regions, for other datacenters specify a arbitrary string #indicating the region.This is normally specified as a -D option (eg) -Deureka.region=us-east-1 eureka.region=default #Name of the application to be identified by other services eureka.name=sampleEurekaClient #Virtual host name by which the clients identifies this service #eureka.vipAddress=eureka.mydomain.net #The port where the service will be running and servicing requests #eureka.port=80 #For eureka clients running in eureka server, it needs to connect to servers in other zones eureka.preferSameZone=true #Change this if you want to use a DNS based lookup for determining other eureka servers. For example #of specifying the DNS entries, check the eureka-client-test.properties, eureka-client-prod.properties eureka.shouldUseDns=false eureka.us-east-1.availabilityZones=default eureka.serviceUrl.default=http://serverIP:8080/eureka/v2/
log4j.rootCategory=INFO,stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %-5p %C:%L [%t] [%M] %m%n log4j.logger.com.netflix.eureka=DEBUG
// Register with Eureka
DiscoveryManager.getInstance().initComponent(
new MyDataCenterInstanceConfig(),
new DefaultEurekaClientConfig());
ApplicationInfoManager.getInstance().setInstanceStatus(
InstanceStatus.UP);
String vipAddress = configInstance.getStringProperty(
"eureka.vipAddress", "sampleservice.mydomain.net").get();
InstanceInfo nextServerInfo = DiscoveryManager.getInstance()
.getDiscoveryClient()
.getNextServerFromEureka(vipAddress, false); Socket s = new Socket();
int serverPort = nextServerInfo.getPort();
try {
s.connect(new InetSocketAddress(nextServerInfo.getHostName(),
serverPort));
} catch (IOException e) {
System.err.println("Could not connect to the server :"
+ nextServerInfo.getHostName() + " at port " + serverPort);
}
try {
System.out.println("Connected to server. Sending a sample request");
PrintStream out = new PrintStream(s.getOutputStream());
out.println("Sample request " + new Date());
String str = null;
System.out.println("Waiting for server response..");
BufferedReader rd = new BufferedReader(new InputStreamReader(
s.getInputStream()));
str = rd.readLine();
if (str != null) {
System.out
.println("Received response from server. Communication all fine using Eureka :");
System.out.println("Exiting the client. Demo over..");
}
rd.close();
} catch (IOException e) {
e.printStackTrace();
} public void unRegisterWithEureka() {
// Un register from eureka.
DiscoveryManager.getInstance().shutdownComponent();
}/*
* Copyright 2012 Netflix, Inc.
*
* 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 com.netflix.eureka;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.netflix.appinfo.ApplicationInfoManager;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.appinfo.InstanceInfo.InstanceStatus;
import com.netflix.appinfo.MyDataCenterInstanceConfig;
import com.netflix.config.DynamicPropertyFactory;
import com.netflix.discovery.DefaultEurekaClientConfig;
import com.netflix.discovery.DiscoveryManager;
/**
* Sample Eureka client that discovers the service using Eureka and sends
* requests.
*
* @author Karthik Ranganathan
*
*/
public class SampleEurekaClient {
private static final DynamicPropertyFactory configInstance = com.netflix.config.DynamicPropertyFactory
.getInstance();
private static final Logger logger = LoggerFactory
.getLogger(SampleEurekaClient.class);
public void sendRequestToServiceUsingEureka() {
// Register with Eureka
DiscoveryManager.getInstance().initComponent(
new MyDataCenterInstanceConfig(),
new DefaultEurekaClientConfig());
ApplicationInfoManager.getInstance().setInstanceStatus(
InstanceStatus.UP);
String vipAddress = configInstance.getStringProperty(
"eureka.vipAddress", "sampleservice.mydomain.net").get();
InstanceInfo nextServerInfo = DiscoveryManager.getInstance()
.getDiscoveryClient()
.getNextServerFromEureka(vipAddress, false);
Socket s = new Socket();
int serverPort = nextServerInfo.getPort();
try {
s.connect(new InetSocketAddress(nextServerInfo.getHostName(),
serverPort));
} catch (IOException e) {
System.err.println("Could not connect to the server :"
+ nextServerInfo.getHostName() + " at port " + serverPort);
}
try {
System.out.println("Connected to server. Sending a sample request");
PrintStream out = new PrintStream(s.getOutputStream());
out.println("Sample request " + new Date());
String str = null;
System.out.println("Waiting for server response..");
BufferedReader rd = new BufferedReader(new InputStreamReader(
s.getInputStream()));
str = rd.readLine();
if (str != null) {
System.out
.println("Received response from server. Communication all fine using Eureka :");
System.out.println("Exiting the client. Demo over..");
}
rd.close();
} catch (IOException e) {
e.printStackTrace();
}
this.unRegisterWithEureka();
}
public void unRegisterWithEureka() {
// Un register from eureka.
DiscoveryManager.getInstance().shutdownComponent();
}
public static void main(String[] args) {
SampleEurekaClient sampleEurekaService = new SampleEurekaClient();
sampleEurekaService.sendRequestToServiceUsingEureka();
}
} logger.debug("nextServerInfo.getAppGroupName()-" + nextServerInfo.getAppGroupName());
logger.debug("nextServerInfo.getAppName()-" + nextServerInfo.getAppName());
logger.debug("nextServerInfo.getASGName()-" + nextServerInfo.getASGName());
logger.debug("nextServerInfo.getHomePageUrl()-" + nextServerInfo.getHomePageUrl());
logger.debug("nextServerInfo.getHostName()-" + nextServerInfo.getHostName());
logger.debug("nextServerInfo.getId()-" + nextServerInfo.getId());
logger.debug("nextServerInfo.getIPAddr()-" + nextServerInfo.getIPAddr());
logger.debug("nextServerInfo.getLastUpdatedTimestamp()-" + nextServerInfo.getLastUpdatedTimestamp());
logger.debug("nextServerInfo.getPort()-" + nextServerInfo.getPort());
logger.debug("nextServerInfo.getSecureVipAddress()-" + nextServerInfo.getSecureVipAddress());
logger.debug("nextServerInfo.getStatusPageUrl()-" + nextServerInfo.getStatusPageUrl());
logger.debug("nextServerInfo.getVIPAddress()-" + nextServerInfo.getVIPAddress());
logger.debug("nextServerInfo.getLastDirtyTimestamp()-" + nextServerInfo.getLastDirtyTimestamp());
logger.debug("nextServerInfo.getHealthCheckUrls()-" + nextServerInfo.getHealthCheckUrls());
logger.debug("nextServerInfo.getStatus()-" + nextServerInfo.getStatus());Eureka 的 Application Client 客户端的运行示例,布布扣,bubuko.com
Eureka 的 Application Client 客户端的运行示例
原文地址:http://blog.csdn.net/defonds/article/details/37652111