服务治理是微服务架构中最核心和基础的模块
首先我们创建一个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.springframework.context.annotation.AnnotationConfigApplicationContext@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.servlet.context.AnnotationConfigServletWebServerApplicationContext@2796aeae: startup date [Thu Sep 20 08:48:22 CST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@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: opbandSoftjavajre1.8in;C:WindowsSunJavain;C:Windowssystem32;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 FilesOracleJavajavapath;C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;C:WindowsSystem32WindowsPowerShellv1.0;C:Program Files (x86)Javajre7;D: opbandSoftjavajdk1.8in;D: opbandSoftjavajdk1.8jrein;D: opbandSoftgitGitcmd;D: opbandSoftsvnin;D: opbandSoftmavenapache-maven-3.3.9-binapache-maven-3.3.9in;C:Program Filesin;D: opbandSoftzookeeper/bin;D: opbandSoftzookeeper/conf;;D: opbandSofteclipse4.8eclipse;;.] 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: com.netflix.config.ConcurrentCompositeConfiguration@59caa884 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.servlet.context.AnnotationConfigServletWebServerApplicationContext@2796aeae: startup date [Thu Sep 20 08:48:22 CST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@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.servlet.context.AnnotationConfigServletWebServerApplicationContext@2796aeae: startup date [Thu Sep 20 08:48:22 CST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@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)
控制台
二、构建服务提供方
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.springframework.context.annotation.AnnotationConfigApplicationContext@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 --- [ main] c.sharp.forward.EurekaClientApplication : No active profile set, falling back to default profiles: default 14 2018-09-20 08:54:43.530 INFO 6012 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@7922d892: startup date [Thu Sep 20 08:54:43 CST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@323b36e0 15 2018-09-20 08:54:43.999 INFO 6012 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=3654aace-518c-3640-a52e-4652f0d1edda 16 2018-09-20 08:54:44.011 INFO 6012 --- [ main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring 17 2018-09-20 08:54:44.083 INFO 6012 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.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) 18 2018-09-20 08:54:44.411 INFO 6012 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8889 (http) 19 2018-09-20 08:54:44.438 INFO 6012 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 20 2018-09-20 08:54:44.438 INFO 6012 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.34 21 2018-09-20 08:54:44.446 INFO 6012 --- [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: opbandSoftjavajre1.8in;C:WindowsSunJavain;C:Windowssystem32;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 FilesOracleJavajavapath;C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;C:WindowsSystem32WindowsPowerShellv1.0;C:Program Files (x86)Javajre7;D: opbandSoftjavajdk1.8in;D: opbandSoftjavajdk1.8jrein;D: opbandSoftgitGitcmd;D: opbandSoftsvnin;D: opbandSoftmavenapache-maven-3.3.9-binapache-maven-3.3.9in;C:Program Filesin;D: opbandSoftzookeeper/bin;D: opbandSoftzookeeper/conf;;D: opbandSofteclipse4.8eclipse;;.] 22 2018-09-20 08:54:44.565 INFO 6012 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 23 2018-09-20 08:54:44.565 INFO 6012 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1035 ms 24 2018-09-20 08:54:44.615 INFO 6012 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/] 25 2018-09-20 08:54:44.619 INFO 6012 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 26 2018-09-20 08:54:44.619 INFO 6012 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 27 2018-09-20 08:54:44.619 INFO 6012 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*] 28 2018-09-20 08:54:44.619 INFO 6012 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*] 29 2018-09-20 08:54:44.682 WARN 6012 --- [ main] c.n.c.sources.URLConfigurationSource : No URLs will be polled as dynamic configuration sources. 30 2018-09-20 08:54:44.682 INFO 6012 --- [ 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. 31 2018-09-20 08:54:44.687 WARN 6012 --- [ main] c.n.c.sources.URLConfigurationSource : No URLs will be polled as dynamic configuration sources. 32 2018-09-20 08:54:44.687 INFO 6012 --- [ 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. 33 2018-09-20 08:54:44.763 INFO 6012 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 34 2018-09-20 08:54:45.004 INFO 6012 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@7922d892: startup date [Thu Sep 20 08:54:43 CST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@323b36e0 35 2018-09-20 08:54:45.059 INFO 6012 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello],methods=[GET]}" onto public java.lang.String com.sharp.forward.order.OrderController.get() 36 2018-09-20 08:54:45.061 INFO 6012 --- [ 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) 37 2018-09-20 08:54:45.061 INFO 6012 --- [ 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) 38 2018-09-20 08:54:45.084 INFO 6012 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 39 2018-09-20 08:54:45.084 INFO 6012 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 40 2018-09-20 08:54:45.585 INFO 6012 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 41 2018-09-20 08:54:45.590 INFO 6012 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Bean with name 'refreshScope' has been autodetected for JMX exposure 42 2018-09-20 08:54:45.591 INFO 6012 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Bean with name 'configurationPropertiesRebinder' has been autodetected for JMX exposure 43 2018-09-20 08:54:45.592 INFO 6012 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Bean with name 'environmentManager' has been autodetected for JMX exposure 44 2018-09-20 08:54:45.594 INFO 6012 --- [ 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] 45 2018-09-20 08:54:45.602 INFO 6012 --- [ 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] 46 2018-09-20 08:54:45.609 INFO 6012 --- [ 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=7922d892,type=ConfigurationPropertiesRebinder] 47 2018-09-20 08:54:45.617 INFO 6012 --- [ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 0 48 2018-09-20 08:54:45.629 INFO 6012 --- [ main] o.s.c.n.eureka.InstanceInfoFactory : Setting initial instance status as: STARTING 49 2018-09-20 08:54:45.660 INFO 6012 --- [ main] com.netflix.discovery.DiscoveryClient : Initializing Eureka in region us-east-1 50 2018-09-20 08:54:45.901 INFO 6012 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using JSON encoding codec LegacyJacksonJson 51 2018-09-20 08:54:45.902 INFO 6012 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using JSON decoding codec LegacyJacksonJson 52 2018-09-20 08:54:46.006 INFO 6012 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using XML encoding codec XStreamXml 53 2018-09-20 08:54:46.006 INFO 6012 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using XML decoding codec XStreamXml 54 2018-09-20 08:54:46.140 INFO 6012 --- [ main] c.n.d.s.r.aws.ConfigClusterResolver : Resolving eureka endpoints via configuration 55 2018-09-20 08:54:46.183 INFO 6012 --- [ main] com.netflix.discovery.DiscoveryClient : Disable delta property : false 56 2018-09-20 08:54:46.183 INFO 6012 --- [ main] com.netflix.discovery.DiscoveryClient : Single vip registry refresh property : null 57 2018-09-20 08:54:46.183 INFO 6012 --- [ main] com.netflix.discovery.DiscoveryClient : Force full registry fetch : false 58 2018-09-20 08:54:46.183 INFO 6012 --- [ main] com.netflix.discovery.DiscoveryClient : Application is null : false 59 2018-09-20 08:54:46.183 INFO 6012 --- [ main] com.netflix.discovery.DiscoveryClient : Registered Applications size is zero : true 60 2018-09-20 08:54:46.184 INFO 6012 --- [ main] com.netflix.discovery.DiscoveryClient : Application version is -1: true 61 2018-09-20 08:54:46.184 INFO 6012 --- [ main] com.netflix.discovery.DiscoveryClient : Getting all instance registry info from the eureka server 62 2018-09-20 08:54:46.282 INFO 6012 --- [ main] com.netflix.discovery.DiscoveryClient : The response status is 200 63 2018-09-20 08:54:46.283 INFO 6012 --- [ main] com.netflix.discovery.DiscoveryClient : Starting heartbeat executor: renew interval is: 30 64 2018-09-20 08:54:46.285 INFO 6012 --- [ main] c.n.discovery.InstanceInfoReplicator : InstanceInfoReplicator onDemand update allowed rate per min is 4 65 2018-09-20 08:54:46.288 INFO 6012 --- [ main] com.netflix.discovery.DiscoveryClient : Discovery Client initialized at timestamp 1537404886287 with initial instances count: 0 66 2018-09-20 08:54:46.291 INFO 6012 --- [ main] o.s.c.n.e.s.EurekaServiceRegistry : Registering application client with eureka with status UP 67 2018-09-20 08:54:46.291 INFO 6012 --- [ main] com.netflix.discovery.DiscoveryClient : Saw local status change event StatusChangeEvent [timestamp=1537404886291, current=UP, previous=STARTING] 68 2018-09-20 08:54:46.293 INFO 6012 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_CLIENT/hh-PC:client:8889: registering service... 69 2018-09-20 08:54:46.354 INFO 6012 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8889 (http) with context path '' 70 2018-09-20 08:54:46.355 INFO 6012 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8889 71 2018-09-20 08:54:46.359 INFO 6012 --- [ main] c.sharp.forward.EurekaClientApplication : Started EurekaClientApplication in 3.698 seconds (JVM running for 4.086) 72 2018-09-20 08:54:46.414 INFO 6012 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_CLIENT/hh-PC:client:8889 - registration status: 204
运行后的控制台
看到client说明已经注册上了服务
然后我们写一个controller测试一下
因为controller用到web的注解,所以需要引入web包,在客户端的pom中已经引入了
下面是代码
1 package com.sharp.forward.order; 2 import org.springframework.web.bind.annotation.GetMapping; 3 import org.springframework.web.bind.annotation.RestController; 4 @RestController 5 public class OrderController { 6 7 @GetMapping("/hello") 8 public String get() { 9 System.out.println("hello world!"); 10 return "hello world!"; 11 } 12 }
在地址栏输入服务提供方地址运行如下:
ok!
小结一下:
@EnableEurekaServer 的是服务注册中心
@EnableDiscoveryClient 的是服务提供方和服务消费方用的