SpringCloud创建Eureka Client服务注册
1.说明
本文详细介绍微服务注册到Eureka的方法,
即Eureka Client注册到Eureka Server,
这里用任意一个Spring Cloud服务为例,
比如下面已经创建好的Config Server模块,
请参考SpringCloud创建Config模块,
在里面创建Eureka Client配置,
把Config Server微服务注册到Eureka Server。
2.添加依赖
在pom.xml中增加eureka-client的依赖:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
3.添加配置
在application.yml中配置自己的主机名,
以及注册的Eureka地址:
eureka:
instance:
#eureka客户端的实例名字(主机名)
hostname: config-service
client:
service-url:
#表示向注册中心注册自己
register-with-eureka: true
#表示需要去注册中心检索服务
fetch-registry: true
#与eureka server交互的地址,包括查询服务和注册服务
defaultZone: http://localhost:7001/eureka
4.添加注解
在客户端的主启动类上面加EnableEurekaClient注解:
package com.yuwen.spring.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
5.启动服务
Config Server服务启动日志如下,
注意要先启动Eureka Server服务。
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.1.RELEASE)
2020-09-09 16:43:54.699 INFO 11216 --- [ main] c.yuwen.spring.config.ConfigApplication : No active profile set, falling back to default profiles: default
2020-09-09 16:43:55.351 WARN 11216 --- [ main] o.s.boot.actuate.endpoint.EndpointId : Endpoint ID 'service-registry' contains invalid characters, please migrate to a valid format.
2020-09-09 16:43:55.473 INFO 11216 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=37338f68-d6d5-3a56-8d91-37021e6160dc
2020-09-09 16:43:56.076 INFO 11216 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 9009 (http)
2020-09-09 16:43:56.086 INFO 11216 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-09-09 16:43:56.086 INFO 11216 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.36]
2020-09-09 16:43:56.203 INFO 11216 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-09-09 16:43:56.204 INFO 11216 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1491 ms
2020-09-09 16:43:56.264 WARN 11216 --- [ main] c.n.c.sources.URLConfigurationSource : No URLs will be polled as dynamic configuration sources.
2020-09-09 16:43:56.264 INFO 11216 --- [ 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.
2020-09-09 16:43:56.278 INFO 11216 --- [ main] c.netflix.config.DynamicPropertyFactory : DynamicPropertyFactory is initialized with configuration sources: com.netflix.config.ConcurrentCompositeConfiguration@24534cb0
2020-09-09 16:43:56.350 WARN 11216 --- [ main] c.n.c.sources.URLConfigurationSource : No URLs will be polled as dynamic configuration sources.
2020-09-09 16:43:56.350 INFO 11216 --- [ 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.
2020-09-09 16:43:56.563 INFO 11216 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-09-09 16:43:57.929 WARN 11216 --- [ main] ockingLoadBalancerClientRibbonWarnLogger : You already have RibbonLoadBalancerClient on your classpath. It will be used by default. As Spring Cloud Ribbon is in maintenance mode. We recommend switching to BlockingLoadBalancerClient instead. In order to use it, set the value of `spring.cloud.loadbalancer.ribbon.enabled` to `false` or remove spring-cloud-starter-netflix-ribbon from your project.
2020-09-09 16:43:57.978 INFO 11216 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 2 endpoint(s) beneath base path '/actuator'
2020-09-09 16:43:58.005 INFO 11216 --- [ main] o.s.c.n.eureka.InstanceInfoFactory : Setting initial instance status as: STARTING
2020-09-09 16:43:58.059 INFO 11216 --- [ main] com.netflix.discovery.DiscoveryClient : Initializing Eureka in region us-east-1
2020-09-09 16:43:58.119 INFO 11216 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using JSON encoding codec LegacyJacksonJson
2020-09-09 16:43:58.119 INFO 11216 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using JSON decoding codec LegacyJacksonJson
2020-09-09 16:43:58.234 INFO 11216 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using XML encoding codec XStreamXml
2020-09-09 16:43:58.235 INFO 11216 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using XML decoding codec XStreamXml
2020-09-09 16:43:58.394 INFO 11216 --- [ main] c.n.d.s.r.aws.ConfigClusterResolver : Resolving eureka endpoints via configuration
2020-09-09 16:43:58.624 INFO 11216 --- [ main] com.netflix.discovery.DiscoveryClient : Disable delta property : false
2020-09-09 16:43:58.624 INFO 11216 --- [ main] com.netflix.discovery.DiscoveryClient : Single vip registry refresh property : null
2020-09-09 16:43:58.625 INFO 11216 --- [ main] com.netflix.discovery.DiscoveryClient : Force full registry fetch : false
2020-09-09 16:43:58.625 INFO 11216 --- [ main] com.netflix.discovery.DiscoveryClient : Application is null : false
2020-09-09 16:43:58.625 INFO 11216 --- [ main] com.netflix.discovery.DiscoveryClient : Registered Applications size is zero : true
2020-09-09 16:43:58.625 INFO 11216 --- [ main] com.netflix.discovery.DiscoveryClient : Application version is -1: true
2020-09-09 16:43:58.625 INFO 11216 --- [ main] com.netflix.discovery.DiscoveryClient : Getting all instance registry info from the eureka server
2020-09-09 16:43:58.825 INFO 11216 --- [ main] com.netflix.discovery.DiscoveryClient : The response status is 200
2020-09-09 16:43:58.826 INFO 11216 --- [ main] com.netflix.discovery.DiscoveryClient : Starting heartbeat executor: renew interval is: 30
2020-09-09 16:43:58.828 INFO 11216 --- [ main] c.n.discovery.InstanceInfoReplicator : InstanceInfoReplicator onDemand update allowed rate per min is 4
2020-09-09 16:43:58.831 INFO 11216 --- [ main] com.netflix.discovery.DiscoveryClient : Discovery Client initialized at timestamp 1599641038830 with initial instances count: 0
2020-09-09 16:43:58.832 INFO 11216 --- [ main] o.s.c.n.e.s.EurekaServiceRegistry : Registering application CONFIG-SERVER with eureka with status UP
2020-09-09 16:43:58.832 INFO 11216 --- [ main] com.netflix.discovery.DiscoveryClient : Saw local status change event StatusChangeEvent [timestamp=1599641038832, current=UP, previous=STARTING]
2020-09-09 16:43:58.834 INFO 11216 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_CONFIG-SERVER/yuwen-asiainfo:config-server:9009: registering service...
2020-09-09 16:43:58.859 INFO 11216 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 9009 (http) with context path ''
2020-09-09 16:43:58.859 INFO 11216 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 9009
2020-09-09 16:43:58.925 INFO 11216 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_CONFIG-SERVER/yuwen-asiainfo:config-server:9009 - registration status: 204
2020-09-09 16:43:59.150 INFO 11216 --- [ main] c.yuwen.spring.config.ConfigApplication : Started ConfigApplication in 5.648 seconds (JVM running for 6.129)
6.查看服务
这样客户端服务启动之后,
访问Eureka页面http://localhost:7001,
可以看到已经有CONFIG-SERVER注册了:
注意上面的两个红框,
分别对应微服务名称和主机名:
spring:
application:
# 微服务名称
name: config-server
eureka:
instance:
#eureka客户端的实例名字(主机名)
hostname: config-service
SpringCloud创建Eureka Client服务注册的更多相关文章
- 【微服务架构】SpringCloud之Eureka(服务注册和服务发现基础篇)(二)
上篇文章讲解了SpringCloud组件和概念介绍,接下来讲解一下SpringCloud组件相关组件使用.原理和每个组件的作用的,它主要提供的模块包括:服务发现(Eureka),断路器(Hystrix ...
- SpringCloud之Eureka(服务注册和服务发现基础篇)(二)
一:Eureka简介 Eureka是Spring Cloud Netflix的一个子模块,也是核心模块之一.用于云端服务发现,一个基于REST的服务,用于定位服务,以实现云端中间层服务发现和故障转移. ...
- SpringCloud Netflix Eureka(服务注册/发现)
⒈Eureka是什么? Eureka是Netflix的一个子模块,也是核心模块之一,Eureka是一个基于REST的服务,用于定位服务以实现云端中间层服务发现和故障转移,服务注册与发现对于微服务架构来 ...
- SpringCloud2.0 Eureka Client 服务注册 基础教程(三)
1.创建[服务提供者],即 Eureka Client 1.1.新建 Spring Boot 工程,工程名称:springcloud-eureka-client 1.2.工程 pom.xml 文件添加 ...
- 最简单的懒人springcloud之Eureka(服务注册与发现)
本文开发软件是STS,是eclipse为springboot项目而生的一个软件,用这个软件开发spring的项目版本都会自己对应的,话不多说直接上代码 springboot版本2.1.8.RELEAS ...
- SpringCloud创建Config Client通过Eureka访问Config
1.说明 本文详细介绍配置中心客户端使用方法, 即Config Client到Config Server读取配置. 读取配置的方式有两种, 第一种是直接配置Configer Server的URL, 第 ...
- 【SpringCloud Eureka源码】从Eureka Client发起注册请求到Eureka Server处理的整个服务注册过程(下)
目录 一.Spring Cloud Eureka Server自动配置及初始化 @EnableEurekaServer EurekaServerAutoConfiguration - 注册服务自动配置 ...
- SpringCloud(二)- 服务注册与发现Eureka
离上一篇微服务的基本概念已经过去了几个月,在写那篇博客之前,自己还并未真正的使用微服务架构,很多理解还存在概念上.后面换了公司,新公司既用了SpringCloud也用了Dubbo+Zookeeper, ...
- 学习一下 SpringCloud (二)-- 服务注册中心 Eureka、Zookeeper、Consul、Nacos
(1) 相关博文地址: 学习一下 SpringCloud (一)-- 从单体架构到微服务架构.代码拆分(maven 聚合): https://www.cnblogs.com/l-y-h/p/14105 ...
随机推荐
- ORACLE DBMS_ROWID包详解
这个包在11gR2中有11个函数或存储: 1. 根据给定参数返回一个rowid --根据给定参数返回一个rowid FUNCTION rowid_create(rowid_type IN NUMBER ...
- ORACLE CACHE BUFFER CHAINS原理
原理图如下: 一个cache buffer chains 管理多个hash bucket,受隐含参数:_db_block_hash_buckets(控制管理几个hash bucket)
- Xcode中匹配的配置包的存放目录
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport
- java 对 final 关键字 深度理解
基础理解 : 1.修饰类 当用final去修饰一个类的时候,表示这个类不能被继承.处于安全,在JDK中,被设计为final类的有String.System等,这些类不能被继承 .注意:被修饰的类的成员 ...
- 使用IntelliJ IDEA创建简单的Spring Boot项目
方法一: File - New -Project 创建结束后进行测试运行,修改代码如下: package com.springboot.testone; import org.springframew ...
- C#获取Windows10屏幕的缩放比例
现在1920x1080以上分辨率的高分屏电脑渐渐普及了.我们会在Windows的显示设置里看到缩放比例的设置.在Windows桌面客户端的开发中,有时会想要精确计算窗口的面积或位置.然而在默认情况下, ...
- 阿里云发布CloudOps白皮书,ECS自动化运维套件新升级
12月10 日,2021云上架构与运维峰会上,阿里云发布业界首部<云上自动化运维白皮书>(简称CloudOps白皮书),并在其中提出了CloudOps成熟度模型.同时,阿里云还宣布了ECS ...
- windows下更换MySql数据库数据文件夹位置
详细解决地址 ,感谢博主 :https://blog.csdn.net/u010953266/article/details/56499361 概述 由于更换硬盘,系统重新安装了一遍,原来的mysq ...
- SQLyog 社区免费版下载
SQLyog 是一个快速而简洁的图形化管理MYSQL数据库的工具,它能够在任何地点有效地管理你的数据库,由业界著名的Webyog公司出品.使用SQLyog可以快速直观地让您从世界的任何角落通过网络来维 ...
- Java on Visual Studio Code的更新 – 2021年11月
Nick zhu Senior Program Manager, Developer Division at Microsoft 大家好,欢迎来到 11 月版的 Visual Studio Code ...