配置文件基本采用《Eureka 的 Application Client 客户端的运行示例》Eureka Application Client 客户端配置。另外增添以下配置项:
myclient.ribbon.NIWSServerListClassName=com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList # refresh every minute myclient.ribbon.ServerListRefreshInterval=60000 # movieservice is the virtual address that the target server(s) uses to register with Eureka server myclient.ribbon.DeploymentContextBasedVipAddresses=movieservice 配置文件的初始化仍然采用《Eureka 的 Application Client 客户端的运行示例》中的配置初始化方法: // Register with Eureka DiscoveryManager.getInstance().initComponent( new MyDataCenterInstanceConfig(), new DefaultEurekaClientConfig()); ApplicationInfoManager.getInstance().setInstanceStatus( InstanceStatus.UP);
// get LoadBalancer instance from configuration, properties file DynamicServerListLoadBalancer lb = (DynamicServerListLoadBalancer) ClientFactory.getNamedLoadBalancer("myclient"); // use RandomRule ‘s RandomRule algorithm to get a random server from lb ‘s server list RandomRule randomRule = new RandomRule(); Server randomAlgorithmServer = randomRule.choose(lb, null); logger.debug("random algorithm server host:" + randomAlgorithmServer.getHost() + ";port:" + randomAlgorithmServer.getPort());
/* * 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 java.util.Iterator; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.netflix.appinfo.ApplicationInfoManager; import com.netflix.appinfo.InstanceInfo.InstanceStatus; import com.netflix.appinfo.MyDataCenterInstanceConfig; import com.netflix.client.ClientFactory; import com.netflix.discovery.DefaultEurekaClientConfig; import com.netflix.discovery.DiscoveryManager; import com.netflix.loadbalancer.DynamicServerListLoadBalancer; import com.netflix.loadbalancer.RandomRule; import com.netflix.loadbalancer.Server; /** * Sample Eureka client that discovers the service using Eureka and sends * requests. * * @author Karthik Ranganathan * */ public class SampleEurekaRibbonClient { private static final Logger logger = LoggerFactory .getLogger(SampleEurekaRibbonClient.class); public void sendRequestToServiceUsingEureka() { // Register with Eureka DiscoveryManager.getInstance().initComponent( new MyDataCenterInstanceConfig(), new DefaultEurekaClientConfig()); ApplicationInfoManager.getInstance().setInstanceStatus( InstanceStatus.UP); // get LoadBalancer instance from configuration, properties file DynamicServerListLoadBalancer lb = (DynamicServerListLoadBalancer) ClientFactory.getNamedLoadBalancer("myclient"); // show all servers in the list List<Server> list = lb.getServerList(false); Iterator<Server> it = list.iterator(); while (it.hasNext()) { Server server = it.next(); logger.debug("application service host:" + server.getHost() + ";port=" + server.getPort()); } // use RandomRule ‘s RandomRule algorithm to get a random server from lb ‘s server list RandomRule randomRule = new RandomRule(); Server randomAlgorithmServer = randomRule.choose(lb, null); logger.debug("random algorithm server host:" + randomAlgorithmServer.getHost() + ";port:" + randomAlgorithmServer.getPort()); // communicate with the server Socket s = new Socket(); try { s.connect(new InetSocketAddress(randomAlgorithmServer.getHost(), randomAlgorithmServer.getPort())); } catch (IOException e) { logger.error("Could not connect to the server :" + randomAlgorithmServer.getHost() + " at port " + randomAlgorithmServer.getPort()); } try { logger.debug("Connected to server. Sending a sample request"); PrintStream out = new PrintStream(s.getOutputStream()); out.println("Sample request " + new Date()); String str = null; logger.debug("Waiting for server response.."); BufferedReader rd = new BufferedReader(new InputStreamReader( s.getInputStream())); str = rd.readLine(); if (str != null) { logger.debug("Received response from server. Communication all fine using Eureka :"); logger.debug("Exiting the client. Demo over.."); } rd.close(); } catch (IOException e) { e.printStackTrace(); logger.error(e.getMessage(), e); } this.unRegisterWithEureka(); } public void unRegisterWithEureka() { // Un register from eureka. DiscoveryManager.getInstance().shutdownComponent(); } public static void main(String[] args) { SampleEurekaRibbonClient sampleEurekaRibbonClient = new SampleEurekaRibbonClient(); sampleEurekaRibbonClient.sendRequestToServiceUsingEureka(); } }
原文:http://blog.csdn.net/defonds/article/details/38016301