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 ...
随机推荐
- Linux学习 - shell脚本执行
一.shell概述 shell是一个命令行解释器,为用户提供一个向Linux内核发送请求以便运行程序的界面系统级程序,用户可以用shell来启动.挂起.停止甚至是编写一些程序 shell还是一个功能强 ...
- 【编程思想】【设计模式】【行为模式Behavioral】迭代器模式iterator
Python版 https://github.com/faif/python-patterns/blob/master/behavioral/iterator.py #!/usr/bin/env py ...
- 如何使用table布局静态网页
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 利用Windbg分析Magicodes.IE一次错误编写导致内存剧增
由于这近一年时间一直忙于写书和工作,一直没有水文,但是近期有几位朋友使用我们的Magicodes.IE反馈在导出过程中内存暴涨...好吧,不管怎样,不能苦了我们朋友,接下来我们通过windbg来看一下 ...
- 培训班输出的大量学员,会对IT行业产生哪些影响?
先说下会有哪些影响呢? 1 可能也就是些大城市的,规模比较大的,口碑比较好的培训学校输出的码农才能入行,而且能做长久.一些线上的所谓培训机构,或者小城市的培训学校,输出的能入行的码农,其实规模很有 ...
- linux重启后JDk环境变量配置失效最终解决方案
最终解决方案:https://bbs.deepin.org/forum.php?mod=viewthread&tid=147762 其实这个修改可能也存在问题,如果有耐心的可以每次打开终端 ...
- <转>android 解析json数据格式
json数据格式解析我自己分为两种:一种是普通的,一种是带有数组形式的: 普通形式的:服务器端返回的json数据格式如下:{"userbean":{"Uid": ...
- 主流微服务一站式解决方案Spring Cloud Alibaba入门看这篇就足够了
学习路线 **本人博客网站 **IT小神 www.itxiaoshen.com 生态概述 架构演进 什么是微服务 https://martinfowler.com/microservices/ Mic ...
- XGBoost特征选择
1. 特征选择的思维导图 2. XGBoost特征选择算法 (1) XGBoost算法背景 2016年,陈天奇在论文< XGBoost:A Scalable Tree Boosting Sys ...
- Windows11下载地址
10月5日微软推出了Win11正式版本,但是仍然不支持apk格式,不免让人失望.下面是Win11商业版本下载地址: 下载地址1: ed2k://|file|zh-cn_windows_11_busin ...