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 用户自定义数据类型
用户自定义数据类型(User-defined Data Type)oracle支持对象类型(Object Type).嵌套类型(Nested Table Type)和可变数组类型(Varray Dat ...
- 为什么kafka和zk总是在一起?
一.概念 发布订阅,一个发布者发布到消息,所有订阅者都可以接收到 生产消费,一个消息对象只能被一个消费者消费 kafka是生产者,zookeeper是消费者 有3个微服务,聚合形成一个统一的业务层 但 ...
- Wireshark基本使用(1)
原文出处: EMC中文支持论坛 按照国际惯例,从最基本的说起. 抓取报文: 下载和安装好Wireshark之后,启动Wireshark并且在接口列表中选择接口名,然后开始在此接口上抓包.例如,如果想要 ...
- java 输入输出IO 转换流-字符编码
编码和其产生的问题: 计算机中储存的信息都是用二进制数表示的,而我们在屏幕上看到的数字.英文.标点符号.汉字等字符是二进制数转换之后的结果. 按照某种规则,将字符存储到计算机中,称为编码 .反之,将存 ...
- 开启ipv6支持
CentOS6 开启ipv6模块操作在/etc/sysconfig/modules 目录下创建一个脚本,比如叫做 ipv6.modules,脚本中内容如下:#!/bin/shif [ ! -c /p ...
- react 创建项目 sass router redux
创建项目第一步 基本搭建 在创建之前,需要有一个git 仓库,我们要把项目搭建到git 中 目录介绍 cd 到某个盘 mkdir workspace 创建workspace文件夹 cd works ...
- IDEA控制台中文显示乱码处理
加入 -Dfile.encoding=UTF-8 以及
- Excel转Json升级版-Python
Excel转Json升级版 将excel文件夹中所有xslx文件全部转换json文件,存放在data文件夹中: excel中的格式,从序号为2的行开始,2行为key:1行可以自由写注释: 使用时用双击 ...
- vc++ 调用winapi调节屏幕亮度
!!版权声明:本文为博主原创文章,版权归原文作者和博客园共有,谢绝任何形式的 转载!! 作者:mohist ---- 已经更正文章中错误的地方, 时间: 10/10/2020--------- 自己封 ...
- 【LeetCode】962. Maximum Width Ramp 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址:https://leetco ...