1. 程式人生 > >springCloud 之 Eureka服務治理

springCloud 之 Eureka服務治理

  服務治理是微服務架構中最核心和基礎的模組

首先我們建立一個springCloud eureka service的springboot 工程,該工程提供一個服務中心,用來註冊服務,第二個工程是client需要選擇eureka discovery

 

點選完成後,pom.xml程式碼

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:schemaLocation
="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <groupId>com.zxy</groupId> 7 <artifactId>eureka-service</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <packaging
>jar</packaging> 10 11 <name>eureka-service</name> 12 <description>Forward project for Spring Boot</description> 13 14 <parent> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-parent</
artifactId> 17 <version>2.0.5.RELEASE</version> 18 <relativePath/> <!-- lookup parent from repository --> 19 </parent> 20 21 <properties> 22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 23 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 24 <java.version>1.8</java.version> 25 <spring-cloud.version>Finchley.SR1</spring-cloud.version> 26 </properties> 27 28 <dependencies> 29 <dependency> 30 <groupId>org.springframework.cloud</groupId> 31 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> 32 </dependency> 33 34 <dependency> 35 <groupId>org.springframework.boot</groupId> 36 <artifactId>spring-boot-starter-test</artifactId> 37 <scope>test</scope> 38 </dependency> 39 </dependencies> 40 41 <dependencyManagement> 42 <dependencies> 43 <dependency> 44 <groupId>org.springframework.cloud</groupId> 45 <artifactId>spring-cloud-dependencies</artifactId> 46 <version>${spring-cloud.version}</version> 47 <type>pom</type> 48 <scope>import</scope> 49 </dependency> 50 </dependencies> 51 </dependencyManagement> 52 53 <build> 54 <plugins> 55 <plugin> 56 <groupId>org.springframework.boot</groupId> 57 <artifactId>spring-boot-maven-plugin</artifactId> 58 </plugin> 59 </plugins> 60 </build> 61 62 63 </project>

配置yml的屬性檔案

 1 #本專案埠
 2 server:
 3   port: 8888
 4 #定義應用名稱為order
 5 spring:
 6   application:
 7     name: order
 8 #服務註冊中心例項主機名
 9 eureka:
10   instance:
11     hostname: host
12   client:
13 #是否向註冊中心註冊自己
14     register-with-eureka: true
15 #是否獲取登錄檔
16     fetch-registry: false
17 #服務地址,向哪裡註冊的地址,如果沒有下面的註冊地址但是開啟了上面的
18 #想註冊中心註冊自己就會報錯,因為沒有說明註冊中心地址,不知道註冊到哪裡
19     service-url: 
20       defaultZone: http://127.0.0.1:8888/eureka/
21 #通過引用上面的定義來宣告註冊地址,有一定侷限性主要在hostname這塊,上面寫死的方式比較靈活,如果一定要用下面這種方法,必要時hostname的對映一定要匹配地址,需要在host檔案中做對映
22 #      defaultZone: http://${eureka.instance.hostname}:{server.port}/eureka/    

在程式入口添加註解@EnableEurekaServer,使得該專案成為eureka服務端生效

 1 package com.sharp.forward;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
 6 import org.springframework.context.annotation.ComponentScan;
 7 
 8 @SpringBootApplication
 9 @EnableEurekaServer
10 @ComponentScan("com.sharp.forward.*")
11 public class EurekaServerApplication {
12 
13     public static void main(String[] args) {
14         SpringApplication.run(EurekaServerApplication.class, args);
15     }
16 }

啟動顯示成功

 1 2018-09-20 08:48:21.883  INFO 4068 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.spring[email protected]5c5eefef: startup date [Thu Sep 20 08:48:21 CST 2018]; root of context hierarchy
 2 2018-09-20 08:48:22.150  INFO 4068 --- [           main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
 3 2018-09-20 08:48:22.171  INFO 4068 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'configurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$a5b28d28] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
 4 
 5   .   ____          _            __ _ _
 6  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
 7 ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 8  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
 9   '  |____| .__|_| |_|_| |_\__, | / / / /
10  =========|_|==============|___/=/_/_/_/
11  :: Spring Boot ::        (v2.0.5.RELEASE)
12 
13 2018-09-20 08:48:22.386  INFO 4068 --- [           main] c.sharp.forward.EurekaServerApplication  : No active profile set, falling back to default profiles: default
14 2018-09-20 08:48:22.398  INFO 4068 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.ser[email protected]2796aeae: startup date [Thu Sep 20 08:48:22 CST 2018]; parent: org.spring[email protected]5c5eefef
15 2018-09-20 08:48:23.239  INFO 4068 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=86aa96a4-8d41-33fd-af49-92950d537d77
16 2018-09-20 08:48:23.254  INFO 4068 --- [           main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
17 2018-09-20 08:48:23.331  INFO 4068 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$a5b28d28] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
18 2018-09-20 08:48:23.769  INFO 4068 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8888 (http)
19 2018-09-20 08:48:23.789  INFO 4068 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
20 2018-09-20 08:48:23.789  INFO 4068 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.34
21 2018-09-20 08:48:23.796  INFO 4068 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [D:\topbandSoft\java\jre1.8\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;D:/topbandSoft/java/jre1.8/bin/server;D:/topbandSoft/java/jre1.8/bin;D:/topbandSoft/java/jre1.8/lib/amd64;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Java\jre7;D:\topbandSoft\java\jdk1.8\bin;D:\topbandSoft\java\jdk1.8\jre\bin;D:\topbandSoft\git\Git\cmd;D:\topbandSoft\svn\bin;D:\topbandSoft\maven\apache-maven-3.3.9-bin\apache-maven-3.3.9\bin;C:\Program Files\bin;D:\topbandSoft\zookeeper/bin;D:\topbandSoft\zookeeper/conf;;D:\topbandSoft\eclipse4.8\eclipse;;.]
22 2018-09-20 08:48:23.901  INFO 4068 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
23 2018-09-20 08:48:23.901  INFO 4068 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1503 ms
24 2018-09-20 08:48:24.047  WARN 4068 --- [ost-startStop-1] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
25 2018-09-20 08:48:24.048  INFO 4068 --- [ost-startStop-1] c.n.c.sources.URLConfigurationSource     : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
26 2018-09-20 08:48:24.059  INFO 4068 --- [ost-startStop-1] c.netflix.config.DynamicPropertyFactory  : DynamicPropertyFactory is initialized with configuration sources: [email protected]
27 2018-09-20 08:48:24.916  INFO 4068 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
28 2018-09-20 08:48:24.917  INFO 4068 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'webMvcMetricsFilter' to: [/*]
29 2018-09-20 08:48:24.917  INFO 4068 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
30 2018-09-20 08:48:24.917  INFO 4068 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
31 2018-09-20 08:48:24.917  INFO 4068 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
32 2018-09-20 08:48:24.917  INFO 4068 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpTraceFilter' to: [/*]
33 2018-09-20 08:48:24.917  INFO 4068 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'servletContainer' to urls: [/eureka/*]
34 2018-09-20 08:48:24.917  INFO 4068 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
35 2018-09-20 08:48:24.982  INFO 4068 --- [ost-startStop-1] c.s.j.s.i.a.WebApplicationImpl           : Initiating Jersey application, version 'Jersey: 1.19.1 03/11/2016 02:08 PM'
36 2018-09-20 08:48:25.055  INFO 4068 --- [ost-startStop-1] c.n.d.provider.DiscoveryJerseyProvider   : Using JSON encoding codec LegacyJacksonJson
37 2018-09-20 08:48:25.056  INFO 4068 --- [ost-startStop-1] c.n.d.provider.DiscoveryJerseyProvider   : Using JSON decoding codec LegacyJacksonJson
38 2018-09-20 08:48:25.150  INFO 4068 --- [ost-startStop-1] c.n.d.provider.DiscoveryJerseyProvider   : Using XML encoding codec XStreamXml
39 2018-09-20 08:48:25.150  INFO 4068 --- [ost-startStop-1] c.n.d.provider.DiscoveryJerseyProvider   : Using XML decoding codec XStreamXml
40 2018-09-20 08:48:25.353  WARN 4068 --- [           main] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
41 2018-09-20 08:48:25.353  INFO 4068 --- [           main] c.n.c.sources.URLConfigurationSource     : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
42 2018-09-20 08:48:25.407  INFO 4068 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
43 2018-09-20 08:48:25.539  INFO 4068 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.ser[email protected]2796aeae: startup date [Thu Sep 20 08:48:22 CST 2018]; parent: org.spring[email protected]5c5eefef
44 2018-09-20 08:48:25.594  INFO 4068 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
45 2018-09-20 08:48:25.595  INFO 4068 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
46 2018-09-20 08:48:25.599  INFO 4068 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/],methods=[GET]}" onto public java.lang.String org.springframework.cloud.netflix.eureka.server.EurekaController.status(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.Object>)
47 2018-09-20 08:48:25.599  INFO 4068 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/lastn],methods=[GET]}" onto public java.lang.String org.springframework.cloud.netflix.eureka.server.EurekaController.lastn(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.Object>)
48 2018-09-20 08:48:25.618  INFO 4068 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
49 2018-09-20 08:48:25.618  INFO 4068 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
50 2018-09-20 08:48:25.933  INFO 4068 --- [           main] o.s.ui.freemarker.SpringTemplateLoader   : SpringTemplateLoader for FreeMarker: using resource loader [org.springframework.boot.web.ser[email protected]2796aeae: startup date [Thu Sep 20 08:48:22 CST 2018]; parent: org.spring[email protected]5c5eefef] and template loader path [classpath:/templates/]
51 2018-09-20 08:48:25.934  INFO 4068 --- [           main] o.s.w.s.v.f.FreeMarkerConfigurer         : ClassTemplateLoader for Spring macros added to FreeMarker configuration
52 2018-09-20 08:48:26.081  INFO 4068 --- [           main] o.s.c.n.eureka.InstanceInfoFactory       : Setting initial instance status as: STARTING
53 2018-09-20 08:48:26.110  INFO 4068 --- [           main] com.netflix.discovery.DiscoveryClient    : Initializing Eureka in region us-east-1
54 2018-09-20 08:48:26.110  INFO 4068 --- [           main] com.netflix.discovery.DiscoveryClient    : Client configured to neither register nor query for data.
55 2018-09-20 08:48:26.119  INFO 4068 --- [           main] com.netflix.discovery.DiscoveryClient    : Discovery Client initialized at timestamp 1537404506118 with initial instances count: 0
56 2018-09-20 08:48:26.165  INFO 4068 --- [           main] c.n.eureka.DefaultEurekaServerContext    : Initializing ...
57 2018-09-20 08:48:26.167  WARN 4068 --- [           main] c.n.eureka.cluster.PeerEurekaNodes       : The replica size seems to be empty. Check the route 53 DNS Registry
58 2018-09-20 08:48:26.183  INFO 4068 --- [           main] c.n.e.registry.AbstractInstanceRegistry  : Finished initializing remote region registries. All known remote regions: []
59 2018-09-20 08:48:26.184  INFO 4068 --- [           main] c.n.eureka.DefaultEurekaServerContext    : Initialized
60 2018-09-20 08:48:26.201  INFO 4068 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 2 endpoint(s) beneath base path '/actuator'
61 2018-09-20 08:48:26.211  INFO 4068 --- [           main] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/actuator/health],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)
62 2018-09-20 08:48:26.212  INFO 4068 --- [           main] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/actuator/info],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)
63 2018-09-20 08:48:26.212  INFO 4068 --- [           main] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/actuator],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto protected java.util.Map<java.lang.String, java.util.Map<java.lang.String, org.springframework.boot.actuate.endpoint.web.Link>> org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping.links(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
64 2018-09-20 08:48:26.270  INFO 4068 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
65 2018-09-20 08:48:26.280  INFO 4068 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Bean with name 'environmentManager' has been autodetected for JMX exposure
66 2018-09-20 08:48:26.281  INFO 4068 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Bean with name 'refreshScope' has been autodetected for JMX exposure
67 2018-09-20 08:48:26.282  INFO 4068 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Bean with name 'configurationPropertiesRebinder' has been autodetected for JMX exposure
68 2018-09-20 08:48:26.285  INFO 4068 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Located managed bean 'environmentManager': registering with JMX server as MBean [org.springframework.cloud.context.environment:name=environmentManager,type=EnvironmentManager]
69 2018-09-20 08:48:26.292  INFO 4068 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Located managed bean 'refreshScope': registering with JMX server as MBean [org.springframework.cloud.context.scope.refresh:name=refreshScope,type=RefreshScope]
70 2018-09-20 08:48:26.299  INFO 4068 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Located managed bean 'configurationPropertiesRebinder': registering with JMX server as MBean [org.springframework.cloud.context.properties:name=configurationPropertiesRebinder,context=2796aeae,type=ConfigurationPropertiesRebinder]
71 2018-09-20 08:48:26.307  INFO 4068 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 0
72 2018-09-20 08:48:26.312  INFO 4068 --- [           main] o.s.c.n.e.s.EurekaServiceRegistry        : Registering application order with eureka with status UP
73 2018-09-20 08:48:26.316  INFO 4068 --- [      Thread-13] o.s.c.n.e.server.EurekaServerBootstrap   : Setting the eureka configuration..
74 2018-09-20 08:48:26.316  INFO 4068 --- [      Thread-13] o.s.c.n.e.server.EurekaServerBootstrap   : Eureka data center value eureka.datacenter is not set, defaulting to default
75 2018-09-20 08:48:26.318  INFO 4068 --- [      Thread-13] o.s.c.n.e.server.EurekaServerBootstrap   : Eureka environment value eureka.environment is not set, defaulting to test
76 2018-09-20 08:48:26.332  INFO 4068 --- [      Thread-13] o.s.c.n.e.server.EurekaServerBootstrap   : isAws returned false
77 2018-09-20 08:48:26.332  INFO 4068 --- [      Thread-13] o.s.c.n.e.server.EurekaServerBootstrap   : Initialized server context
78 2018-09-20 08:48:26.332  INFO 4068 --- [      Thread-13] c.n.e.r.PeerAwareInstanceRegistryImpl    : Got 1 instances from neighboring DS node
79 2018-09-20 08:48:26.332  INFO 4068 --- [      Thread-13] c.n.e.r.PeerAwareInstanceRegistryImpl    : Renew threshold is: 1
80 2018-09-20 08:48:26.333  INFO 4068 --- [      Thread-13] c.n.e.r.PeerAwareInstanceRegistryImpl    : Changing status to UP
81 2018-09-20 08:48:26.349  INFO 4068 --- [      Thread-13] e.s.EurekaServerInitializerConfiguration : Started Eureka Server
82 2018-09-20 08:48:26.361  INFO 4068 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8888 (http) with context path ''
83 2018-09-20 08:48:26.365  INFO 4068 --- [           main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8888
84 2018-09-20 08:48:26.367  INFO 4068 --- [           main] c.sharp.forward.EurekaServerApplication  : Started EurekaServerApplication in 4.969 seconds (JVM running for 5.36)
View Code

控制檯

二、構建服務提供方

pom.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5 
 6     <groupId>com.sharp</groupId>
 7     <artifactId>eureka-client</artifactId>
 8     <version>0.0.1-SNAPSHOT</version>
 9     <packaging>jar</packaging>
10 
11     <name>eureka-client</name>
12     <description>Demo project for Spring Boot</description>
13 
14     <parent>
15         <groupId>org.springframework.boot</groupId>
16         <artifactId>spring-boot-starter-parent</artifactId>
17         <version>2.0.5.RELEASE</version>
18         <relativePath/> <!-- lookup parent from repository -->
19     </parent>
20 
21     <properties>
22         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24         <java.version>1.8</java.version>
25         <spring-cloud.version>Finchley.SR1</spring-cloud.version>
26     </properties>
27 
28     <dependencies>
29         <dependency>
30         <groupId>org.springframework.boot</groupId>
31         <artifactId>spring-boot-starter-web</artifactId>    
32         </dependency>
33         <dependency>
34             <groupId>org.springframework.cloud</groupId>
35             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
36         </dependency>
37 
38         <dependency>
39             <groupId>org.springframework.boot</groupId>
40             <artifactId>spring-boot-starter-test</artifactId>
41             <scope>test</scope>
42         </dependency>
43         
44         
45     </dependencies>
46 
47     <dependencyManagement>
48         <dependencies>
49             <dependency>
50                 <groupId>org.springframework.cloud</groupId>
51                 <artifactId>spring-cloud-dependencies</artifactId>
52                 <version>${spring-cloud.version}</version>
53                 <type>pom</type>
54                 <scope>import</scope>
55             </dependency>
56         </dependencies>
57     </dependencyManagement>
58 
59     <build>
60         <plugins>
61             <plugin>
62                 <groupId>org.springframework.boot</groupId>
63                 <artifactId>spring-boot-maven-plugin</artifactId>
64             </plugin>
65         </plugins>
66     </build>
67 
68 
69 </project>

不同點是引入的是eureka-client而不是eureka-server

yml配置

 1 spring:
 2   application:
 3     name: client
 4 server:
 5   port: 8889
 6 eureka:
 7   client:
 8     eureka-server-port: 8889
 9     register-with-eureka: true
10     service-url:
11       defaultZone: http://127.0.0.1:8888/eureka/
12       

程式入口

 1 package com.sharp.forward;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
 6 import org.springframework.context.annotation.ComponentScan;
 7 
 8 @SpringBootApplication
 9 @EnableEurekaClient   //使該專案成為eureka客戶端,提供服務
10 @ComponentScan("com.sharp.forward.*")
11 public class EurekaClientApplication {
12 
13     public static void main(String[] args) {
14         SpringApplication.run(EurekaClientApplication.class, args);
15     }
16 }

執行

 1 2018-09-20 08:54:43.068  INFO 6012 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.spring[email protected]323b36e0: startup date [Thu Sep 20 08:54:43 CST 2018]; root of context hierarchy
 2 2018-09-20 08:54:43.291  INFO 6012 --- [           main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
 3 2018-09-20 08:54:43.310  INFO 6012 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'configurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$28eee863] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
 4 
 5   .   ____          _            __ _ _
 6  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
 7 ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 8  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
 9   '  |____| .__|_| |_|_| |_\__, | / / / /
10  =========|_|==============|___/=/_/_/_/
11  :: Spring Boot ::        (v2.0.5.RELEASE)
12 
13 2018-09-20 08:54:43.518  INFO 6012 --- [