服务治理是微服务架构中最核心和基础的模块

首先我们创建一个springCloud eureka service的springboot 工程,该工程提供一个服务中心,用来注册服务,第二个工程是client需要选择eureka discovery

点击完成后,pom.xml代码

 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.zxy</groupId>
<artifactId>eureka-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>eureka-service</name>
<description>Forward project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

配置yml的属性文件

 #本项目端口
server:
port: 8888
#定义应用名称为order
spring:
application:
name: order
#服务注册中心实例主机名
eureka:
instance:
hostname: host
client:
#是否向注册中心注册自己
register-with-eureka: true
#是否获取注册表
fetch-registry: false
#服务地址,向哪里注册的地址,如果没有下面的注册地址但是开启了上面的
#想注册中心注册自己就会报错,因为没有说明注册中心地址,不知道注册到哪里
service-url:
defaultZone: http://127.0.0.1:8888/eureka/
#通过引用上面的定义来声明注册地址,有一定局限性主要在hostname这块,上面写死的方式比较灵活,如果一定要用下面这种方法,必要时hostname的映射一定要匹配地址,需要在host文件中做映射
# defaultZone: http://${eureka.instance.hostname}:{server.port}/eureka/

在程序入口添加注解@EnableEurekaServer,使得该项目成为eureka服务端生效

 package com.sharp.forward;

 import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.context.annotation.ComponentScan; @SpringBootApplication
@EnableEurekaServer
@ComponentScan("com.sharp.forward.*")
public class EurekaServerApplication { public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}

启动显示成功

 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
2018-09-20 08:48:22.150 INFO 4068 --- [ main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
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) . ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.5.RELEASE) 2018-09-20 08:48:22.386 INFO 4068 --- [ main] c.sharp.forward.EurekaServerApplication : No active profile set, falling back to default profiles: default
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
2018-09-20 08:48:23.239 INFO 4068 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=86aa96a4-8d41-33fd-af49-92950d537d77
2018-09-20 08:48:23.254 INFO 4068 --- [ main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
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)
2018-09-20 08:48:23.769 INFO 4068 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8888 (http)
2018-09-20 08:48:23.789 INFO 4068 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-09-20 08:48:23.789 INFO 4068 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.34
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;;.]
2018-09-20 08:48:23.901 INFO 4068 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-09-20 08:48:23.901 INFO 4068 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1503 ms
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.
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.
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
2018-09-20 08:48:24.916 INFO 4068 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-09-20 08:48:24.917 INFO 4068 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'webMvcMetricsFilter' to: [/*]
2018-09-20 08:48:24.917 INFO 4068 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-09-20 08:48:24.917 INFO 4068 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-09-20 08:48:24.917 INFO 4068 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2018-09-20 08:48:24.917 INFO 4068 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpTraceFilter' to: [/*]
2018-09-20 08:48:24.917 INFO 4068 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'servletContainer' to urls: [/eureka/*]
2018-09-20 08:48:24.917 INFO 4068 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
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'
2018-09-20 08:48:25.055 INFO 4068 --- [ost-startStop-1] c.n.d.provider.DiscoveryJerseyProvider : Using JSON encoding codec LegacyJacksonJson
2018-09-20 08:48:25.056 INFO 4068 --- [ost-startStop-1] c.n.d.provider.DiscoveryJerseyProvider : Using JSON decoding codec LegacyJacksonJson
2018-09-20 08:48:25.150 INFO 4068 --- [ost-startStop-1] c.n.d.provider.DiscoveryJerseyProvider : Using XML encoding codec XStreamXml
2018-09-20 08:48:25.150 INFO 4068 --- [ost-startStop-1] c.n.d.provider.DiscoveryJerseyProvider : Using XML decoding codec XStreamXml
2018-09-20 08:48:25.353 WARN 4068 --- [ main] c.n.c.sources.URLConfigurationSource : No URLs will be polled as dynamic configuration sources.
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.
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]
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
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)
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)
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>)
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>)
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]
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]
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/]
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
2018-09-20 08:48:26.081 INFO 4068 --- [ main] o.s.c.n.eureka.InstanceInfoFactory : Setting initial instance status as: STARTING
2018-09-20 08:48:26.110 INFO 4068 --- [ main] com.netflix.discovery.DiscoveryClient : Initializing Eureka in region us-east-1
2018-09-20 08:48:26.110 INFO 4068 --- [ main] com.netflix.discovery.DiscoveryClient : Client configured to neither register nor query for data.
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
2018-09-20 08:48:26.165 INFO 4068 --- [ main] c.n.eureka.DefaultEurekaServerContext : Initializing ...
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
2018-09-20 08:48:26.183 INFO 4068 --- [ main] c.n.e.registry.AbstractInstanceRegistry : Finished initializing remote region registries. All known remote regions: []
2018-09-20 08:48:26.184 INFO 4068 --- [ main] c.n.eureka.DefaultEurekaServerContext : Initialized
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'
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>)
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>)
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)
2018-09-20 08:48:26.270 INFO 4068 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
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
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
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
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]
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]
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]
2018-09-20 08:48:26.307 INFO 4068 --- [ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 0
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
2018-09-20 08:48:26.316 INFO 4068 --- [ Thread-13] o.s.c.n.e.server.EurekaServerBootstrap : Setting the eureka configuration..
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
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
2018-09-20 08:48:26.332 INFO 4068 --- [ Thread-13] o.s.c.n.e.server.EurekaServerBootstrap : isAws returned false
2018-09-20 08:48:26.332 INFO 4068 --- [ Thread-13] o.s.c.n.e.server.EurekaServerBootstrap : Initialized server context
2018-09-20 08:48:26.332 INFO 4068 --- [ Thread-13] c.n.e.r.PeerAwareInstanceRegistryImpl : Got 1 instances from neighboring DS node
2018-09-20 08:48:26.332 INFO 4068 --- [ Thread-13] c.n.e.r.PeerAwareInstanceRegistryImpl : Renew threshold is: 1
2018-09-20 08:48:26.333 INFO 4068 --- [ Thread-13] c.n.e.r.PeerAwareInstanceRegistryImpl : Changing status to UP
2018-09-20 08:48:26.349 INFO 4068 --- [ Thread-13] e.s.EurekaServerInitializerConfiguration : Started Eureka Server
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 ''
2018-09-20 08:48:26.365 INFO 4068 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8888
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

 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.sharp</groupId>
<artifactId>eureka-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>eureka-client</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> </dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

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

yml配置

 spring:
application:
name: client
server:
port: 8889
eureka:
client:
eureka-server-port: 8889
register-with-eureka: true
service-url:
defaultZone: http://127.0.0.1:8888/eureka/

程序入口

 package com.sharp.forward;

 import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.ComponentScan; @SpringBootApplication
@EnableEurekaClient //使该项目成为eureka客户端,提供服务
@ComponentScan("com.sharp.forward.*")
public class EurekaClientApplication { public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}

运行

 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
2018-09-20 08:54:43.291 INFO 6012 --- [ main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
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) . ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.5.RELEASE) 2018-09-20 08:54:43.518 INFO 6012 --- [ main] c.sharp.forward.EurekaClientApplication : No active profile set, falling back to default profiles: default
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
2018-09-20 08:54:43.999 INFO 6012 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=3654aace-518c-3640-a52e-4652f0d1edda
2018-09-20 08:54:44.011 INFO 6012 --- [ main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
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)
2018-09-20 08:54:44.411 INFO 6012 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8889 (http)
2018-09-20 08:54:44.438 INFO 6012 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-09-20 08:54:44.438 INFO 6012 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.34
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:\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;;.]
2018-09-20 08:54:44.565 INFO 6012 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-09-20 08:54:44.565 INFO 6012 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1035 ms
2018-09-20 08:54:44.615 INFO 6012 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
2018-09-20 08:54:44.619 INFO 6012 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-09-20 08:54:44.619 INFO 6012 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-09-20 08:54:44.619 INFO 6012 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-09-20 08:54:44.619 INFO 6012 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2018-09-20 08:54:44.682 WARN 6012 --- [ main] c.n.c.sources.URLConfigurationSource : No URLs will be polled as dynamic configuration sources.
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.
2018-09-20 08:54:44.687 WARN 6012 --- [ main] c.n.c.sources.URLConfigurationSource : No URLs will be polled as dynamic configuration sources.
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.
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]
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
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()
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)
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)
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]
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]
2018-09-20 08:54:45.585 INFO 6012 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
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
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
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
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]
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]
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]
2018-09-20 08:54:45.617 INFO 6012 --- [ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 0
2018-09-20 08:54:45.629 INFO 6012 --- [ main] o.s.c.n.eureka.InstanceInfoFactory : Setting initial instance status as: STARTING
2018-09-20 08:54:45.660 INFO 6012 --- [ main] com.netflix.discovery.DiscoveryClient : Initializing Eureka in region us-east-1
2018-09-20 08:54:45.901 INFO 6012 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using JSON encoding codec LegacyJacksonJson
2018-09-20 08:54:45.902 INFO 6012 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using JSON decoding codec LegacyJacksonJson
2018-09-20 08:54:46.006 INFO 6012 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using XML encoding codec XStreamXml
2018-09-20 08:54:46.006 INFO 6012 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using XML decoding codec XStreamXml
2018-09-20 08:54:46.140 INFO 6012 --- [ main] c.n.d.s.r.aws.ConfigClusterResolver : Resolving eureka endpoints via configuration
2018-09-20 08:54:46.183 INFO 6012 --- [ main] com.netflix.discovery.DiscoveryClient : Disable delta property : false
2018-09-20 08:54:46.183 INFO 6012 --- [ main] com.netflix.discovery.DiscoveryClient : Single vip registry refresh property : null
2018-09-20 08:54:46.183 INFO 6012 --- [ main] com.netflix.discovery.DiscoveryClient : Force full registry fetch : false
2018-09-20 08:54:46.183 INFO 6012 --- [ main] com.netflix.discovery.DiscoveryClient : Application is null : false
2018-09-20 08:54:46.183 INFO 6012 --- [ main] com.netflix.discovery.DiscoveryClient : Registered Applications size is zero : true
2018-09-20 08:54:46.184 INFO 6012 --- [ main] com.netflix.discovery.DiscoveryClient : Application version is -1: true
2018-09-20 08:54:46.184 INFO 6012 --- [ main] com.netflix.discovery.DiscoveryClient : Getting all instance registry info from the eureka server
2018-09-20 08:54:46.282 INFO 6012 --- [ main] com.netflix.discovery.DiscoveryClient : The response status is 200
2018-09-20 08:54:46.283 INFO 6012 --- [ main] com.netflix.discovery.DiscoveryClient : Starting heartbeat executor: renew interval is: 30
2018-09-20 08:54:46.285 INFO 6012 --- [ main] c.n.discovery.InstanceInfoReplicator : InstanceInfoReplicator onDemand update allowed rate per min is 4
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
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
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]
2018-09-20 08:54:46.293 INFO 6012 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_CLIENT/hh-PC:client:8889: registering service...
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 ''
2018-09-20 08:54:46.355 INFO 6012 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8889
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)
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中已经引入了

下面是代码

 package com.sharp.forward.order;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OrderController { @GetMapping("/hello")
public String get() {
System.out.println("hello world!");
return "hello world!";
}
}

在地址栏输入服务提供方地址运行如下:

ok!

小结一下:

@EnableEurekaServer 的是服务注册中心

@EnableDiscoveryClient 的是服务提供方和服务消费方用的

springCloud 之 Eureka服务治理的更多相关文章

  1. springCloud 之 Eureka服务治理机制及代码运行

    服务提供者 服务注册: 服务提供者在启动的时候通过发送Rest请求的方式将自己注册到Eureka Server上,同时带上了自身服务的一些元数据信息.Eureka Server在收到这个请求后,将元数 ...

  2. 1 Spring Cloud Eureka服务治理

    注:此随笔为读书笔记.<Spring Cloud微服务实战> 什么是微服务? 微服务是将一个原本独立的系统拆分成若干个小型服务(一般按照功能模块拆分),这些小型服务都在各自独立的进程中运行 ...

  3. Spring cloud Eureka 服务治理(注册服务提供者)

    搭建完成服务注册中心,下一步可以创建服务提供者并向注册中心注册服务. 接下来我们创建Spring Boot 应用将其加入Eureka服务治理体系中去. 直接使用签名章节创建hello服务项目改造: 1 ...

  4. 浅谈SpringCloud (二) Eureka服务发现组件

    上面学习到了如何由一个程序访问另一个程序,那么如果使用SpringCloud来进行访问,该如何访问呐? 可以借助Eureka服务发现组件进行访问. 可以借助官方文档:https://spring.io ...

  5. 1 Spring Cloud Eureka服务治理(上)

    注:此随笔为读书笔记.<Spring Cloud微服务实战>,想学习Spring Cloud的同伴们可以去看看此书,里面对源码有详细的解读. 什么是微服务? 微服务是将一个原本独立的系统拆 ...

  6. 微服务之SpringCloud实战(二):SpringCloud Eureka服务治理

    服务治理 SpringCloud Eureka是SpringCloud Netflix微服务套件的一部分,它基于Netflix Eureka做了二次封装,主要完成微服务的服务治理功能,SpringCl ...

  7. springCloud eureka服务治理集群增加安全认证

    做为SpringCloud Netflix服务套件中的一部分,springCloud eureka基于Netflix Eureka做了二次封装,默认提供WEB管理页面及服务治理. 为了确保在生产环境中 ...

  8. 笔记:Spring Cloud Eureka 服务治理

    Spring Cloud Eureka 是 Spring Cloud Netflix 微服务套件的一部分,基于 Netflix Eureka 做了二次封装,主要负责完成微服务架构中的服务治理功能,服务 ...

  9. Spring Cloud Eureka 服务治理

    Spring Cloud Eureka 是 Spring Cloud Netflix 微服务套件的一部分,基于 Netflix Eureka 做了二次封装,主要负责完成微服务架构中的服务治理功能,服务 ...

随机推荐

  1. device supports x86 but apk only supports armeabi-v7a问题解决

    我们可以在build.gradle中有ndk这段代码,只要在后面加上“x86”,再sync now一下,就发现可以运行了. ndk { abiFilters "armeabi-v7a&quo ...

  2. Linux下安装 boost 库

    1. 先去官网下载压缩包: https://www.boost.org/ 2. 解压 tar -zvxf boost_1_70_0.tar.gz 2. cd 进入根目录,然后执行: ./bootstr ...

  3. 工具 - VS Code

    杂项 1. 主题 brackets light pro, One Monokai theme 2. directory tree indent guidelines, directory vertic ...

  4. 虚拟机与ubuntu系统的安装与基础操作

    1.虚拟机的下载: 常见的虚拟机软件有:VMware  VirtuaIBOX  Virtual PC  等. 这里主要介绍VMware ,VMware目前已经有很多个版本,可以根据个人情况进行选择.安 ...

  5. 学习笔记(11)- 文本生成RNNLG

    https://github.com/shawnwun/RNNLG 数据集 给出了4个行业的语料,餐馆.酒店.电脑.电视,及其组合数据. 数据格式 任务 根据给定格式的命令,生成自然语言. 方法.模型 ...

  6. Duilib热键

    转载:https://www.zhaokeli.com/article/8288.html 在initwindow中注册热键 //生成热键标识,需要保存起来退出时销毁使用 int m_HotKeyId ...

  7. JS 一次性事件问题

    项目的需求就是不能重复提交 是通过click点击事件提交数据,到这里的时候想了很多办法,比如说销毁这个点击事件, 但是这个实现起来是相对比较麻烦的,后面想到了一个相对比较简单的办法 通过推荐判断 JS ...

  8. 为Docker Desktop安装kubernet-dashboard

    在上一篇,在windows上,用最简方法(比其他的脚本法,提前拉取镜像简便太多了)安装好了docker desktop,并启用了内置的kubernetes. 这种安装方法实际上是在Hyper-v虚拟机 ...

  9. How to recover if NMC cound not connect

    Some times we suddently find that the NMC can not login,. You would see the sybase database error if ...

  10. JS垂直落体回弹原理

    /* *JS垂直落体回弹原理 */ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...