引用jar包java
<dependency> <groupId>com.netflix.archaius</groupId> <artifactId>archaius-core</artifactId> <version>0.7.5</version> </dependency> <dependency> <groupId>com.netflix.eureka</groupId> <artifactId>eureka-client</artifactId> <version>1.7.0</version> </dependency> <dependency> <groupId>com.netflix.eureka</groupId> <artifactId>eureka-core</artifactId> <version>1.7.0</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>4.4</version> </dependency>
基礎配置文件config.propertiesgit
eureka.region=default eureka.name=my_project eureka.vipAddress=service.io eureka.port=8001 eureka.preferSameZone=true eureka.shouldUseDns=false eureka.serviceUrl.default=http://localhost:5111/eureka/
基礎代碼類 github
ExampleServiceBase
代碼:express
import com.netflix.appinfo.ApplicationInfoManager; import com.netflix.appinfo.InstanceInfo; import com.netflix.config.DynamicPropertyFactory; import com.netflix.discovery.EurekaClient; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.inject.Inject; import javax.inject.Singleton; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; import java.util.Date; /** * An example service (that can be initialized in a variety of ways) that registers with eureka * and listens for REST calls on port 8001. */ @Singleton public class ExampleServiceBase { private final ApplicationInfoManager applicationInfoManager; private final EurekaClient eurekaClient; private final DynamicPropertyFactory configInstance; @Inject public ExampleServiceBase(ApplicationInfoManager applicationInfoManager, EurekaClient eurekaClient, DynamicPropertyFactory configInstance) { this.applicationInfoManager = applicationInfoManager; this.eurekaClient = eurekaClient; this.configInstance = configInstance; } @PostConstruct public void start() { // A good practice is to register as STARTING and only change status to UP // after the service is ready to receive traffic System.out.println("Registering service to eureka with STARTING status"); applicationInfoManager.setInstanceStatus(InstanceInfo.InstanceStatus.STARTING); System.out.println("Simulating service initialization by sleeping for 2 seconds..."); try { Thread.sleep(2000); } catch (InterruptedException e) { // Nothing } // Now we change our status to UP System.out.println("Done sleeping, now changing status to UP"); applicationInfoManager.setInstanceStatus(InstanceInfo.InstanceStatus.UP); waitForRegistrationWithEureka(eurekaClient); System.out.println("Service started and ready to process requests.."); try { int myServingPort = applicationInfoManager.getInfo().getPort(); // read from my registered info ServerSocket serverSocket = new ServerSocket(myServingPort); final Socket s = serverSocket.accept(); System.out.println("Client got connected... processing request from the client"); processRequest(s); } catch (IOException e) { e.printStackTrace(); } System.out.println("Simulating service doing work by sleeping for " + 5 + " seconds..."); try { Thread.sleep(5 * 1000); } catch (InterruptedException e) { // Nothing } } @PreDestroy public void stop() { if (eurekaClient != null) { System.out.println("Shutting down server. Demo over."); eurekaClient.shutdown(); } } private void waitForRegistrationWithEureka(EurekaClient eurekaClient) { // my vip address to listen on String vipAddress = configInstance.getStringProperty("eureka.vipAddress", "sampleservice.mydomain.net").get(); InstanceInfo nextServerInfo = null; while (nextServerInfo == null) { try { nextServerInfo = eurekaClient.getNextServerFromEureka(vipAddress, false); } catch (Throwable e) { System.out.println("Waiting ... verifying service registration with eureka ..."); try { Thread.sleep(10000); } catch (InterruptedException e1) { e1.printStackTrace(); } } } } private void processRequest(final Socket s) { try { BufferedReader rd = new BufferedReader(new InputStreamReader(s.getInputStream())); String line = rd.readLine(); if (line != null) { System.out.println("Received a request from the example client: " + line); } String response = "BAR " + new Date(); System.out.println("Sending the response to the client: " + response); PrintStream out = new PrintStream(s.getOutputStream()); out.println(response); } catch (Throwable e) { System.err.println("Error processing requests"); } finally { if (s != null) { try { s.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
註冊服務代碼apache
ExampleEurekaService
* 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. */ import com.netflix.appinfo.EurekaInstanceConfig; import com.netflix.appinfo.InstanceInfo; import com.netflix.appinfo.providers.EurekaConfigBasedInstanceInfoProvider; import com.netflix.discovery.DiscoveryClient; import com.netflix.discovery.EurekaClient; import com.netflix.appinfo.ApplicationInfoManager; import com.netflix.appinfo.MyDataCenterInstanceConfig; import com.netflix.config.DynamicPropertyFactory; import com.netflix.discovery.DefaultEurekaClientConfig; import com.netflix.discovery.EurekaClientConfig; /** * Sample Eureka service that registers with Eureka to receive and process requests. * This example just receives one request and exits once it receives the request after processing it. * */ public class ExampleEurekaService { private static ApplicationInfoManager applicationInfoManager; private static EurekaClient eurekaClient; private static synchronized ApplicationInfoManager initializeApplicationInfoManager(EurekaInstanceConfig instanceConfig) { if (applicationInfoManager == null) { InstanceInfo instanceInfo = new EurekaConfigBasedInstanceInfoProvider(instanceConfig).get(); applicationInfoManager = new ApplicationInfoManager(instanceConfig, instanceInfo); } return applicationInfoManager; } private static synchronized EurekaClient initializeEurekaClient(ApplicationInfoManager applicationInfoManager, EurekaClientConfig clientConfig) { if (eurekaClient == null) { eurekaClient = new DiscoveryClient(applicationInfoManager, clientConfig); } return eurekaClient; } public static void main(String[] args) { DynamicPropertyFactory configInstance = com.netflix.config.DynamicPropertyFactory.getInstance(); ApplicationInfoManager applicationInfoManager = initializeApplicationInfoManager(new MyDataCenterInstanceConfig()); EurekaClient eurekaClient = initializeEurekaClient(applicationInfoManager, new DefaultEurekaClientConfig()); ExampleServiceBase exampleServiceBase = new ExampleServiceBase(applicationInfoManager, eurekaClient, configInstance); try { exampleServiceBase.start(); } finally { // the stop calls shutdown on eurekaClient exampleServiceBase.stop(); } } }
以上信息參考項目:app
https://github.com/Netflix/eureka/tree/master/eureka-examplesless