Spring Cloud构建微服务架构(一):服务注册与发现(Eureka、Consul)

导读:本篇文章讲解 Spring Cloud构建微服务架构(一):服务注册与发现(Eureka、Consul),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

服务注册与发现(Eureka、Consul)


今天博主开始更新SpringCloud构建微服务架构系列,有兴趣的可以持续关注,欢迎讨论!《陈永佳的博客》


概述:

Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中涉及的配置管理、服务发现、断路器、智能路由、微代理、控制总线、全局锁、决策竞选、分布式会话和集群状态管理等操作提供了一种简单的开发方式。

Spring Cloud包含了多个子项目(针对分布式系统中涉及的多个不同开源产品),比如:Spring Cloud Config、Spring Cloud Netflix、Spring Cloud0 CloudFoundry、Spring Cloud AWS、Spring Cloud Security、Spring Cloud Commons、Spring Cloud Zookeeper、Spring Cloud CLI等项目。


微服务架构:

“微服务架构”在这几年非常的火热,以至于关于微服务架构相关的开源产品被反复的提及(比如:netflix、dubbo),Spring Cloud也因Spring社区的强大知名度和影响力也被广大架构师与开发者备受关注。

那么什么是“微服务架构”呢?简单的说,微服务架构就是将一个完整的应用从数据存储开始垂直拆分成多个不同的服务,每个服务都能独立部署、独立维护、独立扩展,服务与服务间通过诸如《RESTful API的方式》互相调用。


服务治理:

在简单介绍了Spring Cloud和微服务架构之后,下面回归本文的主旨内容,如何使用Spring Cloud来实现服务治理。

由于Spring Cloud为服务治理做了一层抽象接口,所以在Spring Cloud应用中可以支持多种不同的服务治理框架,比如:Netflix Eureka、Consul、Zookeeper。在Spring Cloud服务治理抽象层的作用下,我们可以无缝地切换服务治理实现,并且不影响任何其他的服务注册、服务发现、服务调用等逻辑。

所以,下面我们通过介绍服务治理的实现来体会Spring Cloud这一层抽象所带来的好处。(暂时介绍Eureka,Alibba系列和Consul后续更新)


Spring Cloud Eureka:

首先,我们来尝试使用Spring Cloud Eureka来实现服务治理。

Spring Cloud Eureka是Spring Cloud Netflix项目下的服务治理模块。而Spring Cloud Netflix项目是Spring Cloud的子项目之一,主要内容是对Netflix公司一系列开源产品的包装,它为Spring Boot应用提供了自配置的Netflix OSS整合。通过一些简单的注解,开发者就可以快速的在应用中配置一下常用模块并构建庞大的分布式系统。它主要提供的模块包括:服务发现(Eureka),断路器(Hystrix),智能路由(Zuul),客户端负载均衡(Ribbon)等。

下面,就来具体看看如何使用Spring Cloud Eureka实现服务治理。

_ 首先创建SpringBoot聚合项目,创建SpringCloud-Eureka“服务注册中心”子项目:_

_ 来看一下博主的pom.xml文件:(都有注释哦,Maven可以看博主以前的文章!)_

<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.cxb</groupId>
	<artifactId>SpringCloud-Eurekas</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
	<name>SpringCloud-Eurekas</name>
	<url>https://repo.spring.io/milestone</url>
	<description>CYJ:SpringCloud Eureka 注册中心</description>

	<!-- SpringBoot 项目 统一 父依赖 -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.6.RELEASE</version>
		<relativePath />
	</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>

		<!-- SpringCloud Eureka 注册中心依赖 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
		</dependency>
		
		<!-- SpringCloud Hystrix 微服务容错监控组件:断路器,依赖隔离,服务降级,服务监控 依赖 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
		</dependency>

		<!-- 权限控制依赖暂时不用 -->
		<!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> 
			</dependency> -->

	</dependencies>

	<!-- SpringCloud 所有子项目 版本集中管理.MS:统一所有SpringCloud依赖项目的版本 依赖 -->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<!-- SpringBoot 2.0 以上版本这样配置 -->
				<version>${spring-cloud.version}</version>
				<!-- <version>Dalston.RC1</version> -->
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<!-- SpringBoot 项目打jar包的Maven插件 -->
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
		<!-- SpringBoot项目打包jar名称 -->
		<finalName>eureka</finalName>
	</build>

	<!-- SpringCloud 官方远程仓库.MS:部分本地仓库和镜像仓库没有SpringCloud子项目依赖。 -->
	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>

</project>

_ 来看一下application.yml文件:_

server:
#项目端口号
  port: 8888

eureka:
  instance:
  #eureka实例主机名称
    hostname: localhost
  client:
  #关闭eureka的客户端行为:注册服务
    registerWithEureka: false
    #关闭eureka的客户端行为:订阅服务
    fetchRegistry: false
    serviceUrl:
    #eureka注册中心地址,其他注册项目要注册到这个地址(http://localhost:8888/eureka/)
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

_ 想要注册到注册中心的项目需要加入如下配置:_

#debug: true
eureka:
  client:
    serviceUrl:
    #eureka注册中心地址固定就这样写
      defaultZone: http://localhost:8888/eureka/

_ 通过@EnableEurekaServer注解启动一个服务注册中心提供给其他应用进行对话。_

上代码,博主的项目哦!(日志什么的没有写不要在意,只是提供一定的教学!)

package com.cxb.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
 * 
 * @Description:
 * @ClassName: Application.java
 * @author ChenYongJia
 * @Date 2018年12月01日 晚上22:54
 * @Email chen87647213@163.com 
 */
@SpringBootApplication// 也可以放@SpringCloudApplication注解,后续将讲解!
@EnableEurekaServer
public class Application {
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}

到这里可以启动我们的项目了,看到如下提示,项目启动成功!

2019-05-16 20:11:01.871  INFO 12296 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@16ec5519: startup date [Thu May 16 20:11:01 CST 2019]; root of context hierarchy
2019-05-16 20:11:03.107  INFO 12296 --- [           main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2019-05-16 20:11:03.204  INFO 12296 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'configurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$d1e743ab] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.6.RELEASE)

2019-05-16 20:11:03.880  INFO 12296 --- [           main] com.cxb.springcloud.Application          : No active profile set, falling back to default profiles: default
2019-05-16 20:11:03.896  INFO 12296 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@7d3d101b: startup date [Thu May 16 20:11:03 CST 2019]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@16ec5519
2019-05-16 20:11:05.312  INFO 12296 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=85190e93-3cc7-372a-b57e-d8d6857bfa35
2019-05-16 20:11:05.332  INFO 12296 --- [           main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2019-05-16 20:11:05.482  INFO 12296 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$d1e743ab] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-05-16 20:11:06.334  INFO 12296 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8888 (http)
2019-05-16 20:11:06.440  INFO 12296 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-05-16 20:11:06.440  INFO 12296 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.34
2019-05-16 20:11:06.450  INFO 12296 --- [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: [C:\Program Files\Java\jdk1.8.0_66\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jre1.8.0_66/bin/server;C:/Program Files/Java/jre1.8.0_66/bin;C:/Program Files/Java/jre1.8.0_66/lib/amd64;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Java\jdk1.8.0_66\bin;C:\Program Files\Java\jdk1.8.0_66\jre\bin;"C:\Program Files\Java\jdk1.8.0_66\bin;C:\Program Files\Java\jdk1.8.0_66\jre\bin;";D:\Git\Git\cmd;C:\Users\lenovo\AppData\Local\Microsoft\WindowsApps;;C:\Users\lenovo\AppData\Local\Programs\Microsoft VS Code\bin;G:\SpringBoot\spring-tool-suite-3.9.6.RELEASE-e4.9.0-win32-x86_64\sts-bundle\sts-3.9.6.RELEASE;;.]
2019-05-16 20:11:06.713  INFO 12296 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-05-16 20:11:06.713  INFO 12296 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2817 ms
2019-05-16 20:11:06.992  WARN 12296 --- [ost-startStop-1] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
2019-05-16 20:11:06.993  INFO 12296 --- [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.
2019-05-16 20:11:07.024  INFO 12296 --- [ost-startStop-1] c.netflix.config.DynamicPropertyFactory  : DynamicPropertyFactory is initialized with configuration sources: com.netflix.config.ConcurrentCompositeConfiguration@4ca6ecb5
2019-05-16 20:11:09.004  INFO 12296 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2019-05-16 20:11:09.005  INFO 12296 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'webMvcMetricsFilter' to: [/*]
2019-05-16 20:11:09.005  INFO 12296 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2019-05-16 20:11:09.005  INFO 12296 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2019-05-16 20:11:09.005  INFO 12296 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2019-05-16 20:11:09.005  INFO 12296 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpTraceFilter' to: [/*]
2019-05-16 20:11:09.005  INFO 12296 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'servletContainer' to urls: [/eureka/*]
2019-05-16 20:11:09.006  INFO 12296 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2019-05-16 20:11:09.221  INFO 12296 --- [ost-startStop-1] c.s.j.s.i.a.WebApplicationImpl           : Initiating Jersey application, version 'Jersey: 1.19.1 03/11/2016 02:08 PM'
2019-05-16 20:11:09.328  INFO 12296 --- [ost-startStop-1] c.n.d.provider.DiscoveryJerseyProvider   : Using JSON encoding codec LegacyJacksonJson
2019-05-16 20:11:09.329  INFO 12296 --- [ost-startStop-1] c.n.d.provider.DiscoveryJerseyProvider   : Using JSON decoding codec LegacyJacksonJson
2019-05-16 20:11:09.676  INFO 12296 --- [ost-startStop-1] c.n.d.provider.DiscoveryJerseyProvider   : Using XML encoding codec XStreamXml
2019-05-16 20:11:09.676  INFO 12296 --- [ost-startStop-1] c.n.d.provider.DiscoveryJerseyProvider   : Using XML decoding codec XStreamXml
2019-05-16 20:11:10.360  WARN 12296 --- [           main] o.s.c.n.a.ArchaiusAutoConfiguration      : No spring.application.name found, defaulting to 'application'
2019-05-16 20:11:10.361  WARN 12296 --- [           main] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
2019-05-16 20:11:10.361  INFO 12296 --- [           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.
2019-05-16 20:11:10.480  INFO 12296 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-05-16 20:11:10.829  INFO 12296 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@7d3d101b: startup date [Thu May 16 20:11:03 CST 2019]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@16ec5519
2019-05-16 20:11:10.883  INFO 12296 --- [           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)
2019-05-16 20:11:10.885  INFO 12296 --- [           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)
2019-05-16 20:11:10.894  INFO 12296 --- [           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>)
2019-05-16 20:11:10.894  INFO 12296 --- [           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>)
2019-05-16 20:11:10.918  INFO 12296 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-05-16 20:11:10.918  INFO 12296 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-05-16 20:11:11.420  INFO 12296 --- [           main] o.s.ui.freemarker.SpringTemplateLoader   : SpringTemplateLoader for FreeMarker: using resource loader [org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@7d3d101b: startup date [Thu May 16 20:11:03 CST 2019]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@16ec5519] and template loader path [classpath:/templates/]
2019-05-16 20:11:11.421  INFO 12296 --- [           main] o.s.w.s.v.f.FreeMarkerConfigurer         : ClassTemplateLoader for Spring macros added to FreeMarker configuration
2019-05-16 20:11:11.825  INFO 12296 --- [           main] o.s.c.n.eureka.InstanceInfoFactory       : Setting initial instance status as: STARTING
2019-05-16 20:11:11.887  INFO 12296 --- [           main] com.netflix.discovery.DiscoveryClient    : Initializing Eureka in region us-east-1
2019-05-16 20:11:11.887  INFO 12296 --- [           main] com.netflix.discovery.DiscoveryClient    : Client configured to neither register nor query for data.
2019-05-16 20:11:11.924  INFO 12296 --- [           main] com.netflix.discovery.DiscoveryClient    : Discovery Client initialized at timestamp 1558008671896 with initial instances count: 0
2019-05-16 20:11:12.032  INFO 12296 --- [           main] c.n.eureka.DefaultEurekaServerContext    : Initializing ...
2019-05-16 20:11:12.035  WARN 12296 --- [           main] c.n.eureka.cluster.PeerEurekaNodes       : The replica size seems to be empty. Check the route 53 DNS Registry
2019-05-16 20:11:12.058  INFO 12296 --- [           main] c.n.e.registry.AbstractInstanceRegistry  : Finished initializing remote region registries. All known remote regions: []
2019-05-16 20:11:12.059  INFO 12296 --- [           main] c.n.eureka.DefaultEurekaServerContext    : Initialized
2019-05-16 20:11:12.103  INFO 12296 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 2 endpoint(s) beneath base path '/actuator'
2019-05-16 20:11:12.130  INFO 12296 --- [           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>)
2019-05-16 20:11:12.131  INFO 12296 --- [           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>)
2019-05-16 20:11:12.133  INFO 12296 --- [           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)
2019-05-16 20:11:12.208  INFO 12296 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2019-05-16 20:11:12.217  INFO 12296 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Bean with name 'environmentManager' has been autodetected for JMX exposure
2019-05-16 20:11:12.218  INFO 12296 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Bean with name 'refreshScope' has been autodetected for JMX exposure
2019-05-16 20:11:12.220  INFO 12296 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Bean with name 'configurationPropertiesRebinder' has been autodetected for JMX exposure
2019-05-16 20:11:12.223  INFO 12296 --- [           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]
2019-05-16 20:11:12.239  INFO 12296 --- [           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]
2019-05-16 20:11:12.248  INFO 12296 --- [           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=7d3d101b,type=ConfigurationPropertiesRebinder]
2019-05-16 20:11:12.257  INFO 12296 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 0
2019-05-16 20:11:12.262  INFO 12296 --- [           main] o.s.c.n.e.s.EurekaServiceRegistry        : Registering application unknown with eureka with status UP
2019-05-16 20:11:12.267  INFO 12296 --- [      Thread-14] o.s.c.n.e.server.EurekaServerBootstrap   : Setting the eureka configuration..
2019-05-16 20:11:12.267  INFO 12296 --- [      Thread-14] o.s.c.n.e.server.EurekaServerBootstrap   : Eureka data center value eureka.datacenter is not set, defaulting to default
2019-05-16 20:11:12.268  INFO 12296 --- [      Thread-14] o.s.c.n.e.server.EurekaServerBootstrap   : Eureka environment value eureka.environment is not set, defaulting to test
2019-05-16 20:11:12.345  INFO 12296 --- [      Thread-14] o.s.c.n.e.server.EurekaServerBootstrap   : isAws returned false
2019-05-16 20:11:12.346  INFO 12296 --- [      Thread-14] o.s.c.n.e.server.EurekaServerBootstrap   : Initialized server context
2019-05-16 20:11:12.346  INFO 12296 --- [      Thread-14] c.n.e.r.PeerAwareInstanceRegistryImpl    : Got 1 instances from neighboring DS node
2019-05-16 20:11:12.346  INFO 12296 --- [      Thread-14] c.n.e.r.PeerAwareInstanceRegistryImpl    : Renew threshold is: 1
2019-05-16 20:11:12.346  INFO 12296 --- [      Thread-14] c.n.e.r.PeerAwareInstanceRegistryImpl    : Changing status to UP
2019-05-16 20:11:12.355  INFO 12296 --- [      Thread-14] e.s.EurekaServerInitializerConfiguration : Started Eureka Server
2019-05-16 20:11:12.440  INFO 12296 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8888 (http) with context path ''
2019-05-16 20:11:12.441  INFO 12296 --- [           main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8888
2019-05-16 20:11:12.460  INFO 12296 --- [           main] com.cxb.springcloud.Application          : Started Application in 12.492 seconds (JVM running for 16.89)

来张效果图:(还没有注册服务,来两个注册服务看一下)

在这里插入图片描述
注册两个健康的服务如下:

创建“服务提供方”

创建提供服务的客户端,并向服务注册中心注册自己。本文我们主要介绍服务的注册与发现,所以我们不妨在服务提供方中尝试着提供一个接口来获取当前所有的服务信息。命名为eureka-client

最后在应用主类中通过加上@EnableDiscoveryClient注解,该注解能激活Eureka中的DiscoveryClient实现,这样才能实现Controller中对服务信息的输出。

整理后将贴出GitHub地址!

在这里插入图片描述


到这里Spring Cloud构建微服务架构(一):服务注册与发现(Eureka、Consul)分享完毕了!下一波将分享Spring Cloud构建微服务架构(二):服务注册与发现Eureka的集群!


最后

  • 一个强有力的团队必定有一个强有力的,有逻辑的,有榜样力量的管理者!打造一流团队,博主在路上ing。。。。

  • 更多参考精彩博文请看这里:《陈永佳的博客》

  • 喜欢博主的小伙伴可以加个关注、点个赞哦,持续更新嘿嘿!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/97881.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!