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服务注册的更多相关文章

  1. 【微服务架构】SpringCloud之Eureka(服务注册和服务发现基础篇)(二)

    上篇文章讲解了SpringCloud组件和概念介绍,接下来讲解一下SpringCloud组件相关组件使用.原理和每个组件的作用的,它主要提供的模块包括:服务发现(Eureka),断路器(Hystrix ...

  2. SpringCloud之Eureka(服务注册和服务发现基础篇)(二)

    一:Eureka简介 Eureka是Spring Cloud Netflix的一个子模块,也是核心模块之一.用于云端服务发现,一个基于REST的服务,用于定位服务,以实现云端中间层服务发现和故障转移. ...

  3. SpringCloud Netflix Eureka(服务注册/发现)

    ⒈Eureka是什么? Eureka是Netflix的一个子模块,也是核心模块之一,Eureka是一个基于REST的服务,用于定位服务以实现云端中间层服务发现和故障转移,服务注册与发现对于微服务架构来 ...

  4. SpringCloud2.0 Eureka Client 服务注册 基础教程(三)

    1.创建[服务提供者],即 Eureka Client 1.1.新建 Spring Boot 工程,工程名称:springcloud-eureka-client 1.2.工程 pom.xml 文件添加 ...

  5. 最简单的懒人springcloud之Eureka(服务注册与发现)

    本文开发软件是STS,是eclipse为springboot项目而生的一个软件,用这个软件开发spring的项目版本都会自己对应的,话不多说直接上代码 springboot版本2.1.8.RELEAS ...

  6. SpringCloud创建Config Client通过Eureka访问Config

    1.说明 本文详细介绍配置中心客户端使用方法, 即Config Client到Config Server读取配置. 读取配置的方式有两种, 第一种是直接配置Configer Server的URL, 第 ...

  7. 【SpringCloud Eureka源码】从Eureka Client发起注册请求到Eureka Server处理的整个服务注册过程(下)

    目录 一.Spring Cloud Eureka Server自动配置及初始化 @EnableEurekaServer EurekaServerAutoConfiguration - 注册服务自动配置 ...

  8. SpringCloud(二)- 服务注册与发现Eureka

    离上一篇微服务的基本概念已经过去了几个月,在写那篇博客之前,自己还并未真正的使用微服务架构,很多理解还存在概念上.后面换了公司,新公司既用了SpringCloud也用了Dubbo+Zookeeper, ...

  9. 学习一下 SpringCloud (二)-- 服务注册中心 Eureka、Zookeeper、Consul、Nacos

    (1) 相关博文地址: 学习一下 SpringCloud (一)-- 从单体架构到微服务架构.代码拆分(maven 聚合): https://www.cnblogs.com/l-y-h/p/14105 ...

随机推荐

  1. IOS_UIButton去掉系统的按下高亮置灰效果

    setAdjustsImageWhenHighlighted   // default is YES. if YES, image is drawn darker when highlighted(p ...

  2. jquery datatable真实示例

    1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncodin ...

  3. 【Linux】【Services】【Cache】使用Sentinel搭建高可用Redis

    1. 简介 1.1. 一些基础概念请参考 http://www.cnblogs.com/demonzk/p/7453494.html 1.2. 几种常用的集群方式. -- Redis Sentinel ...

  4. my39_InnoDB锁机制之Gap Lock、Next-Key Lock、Record Lock解析

    MySQL InnoDB支持三种行锁定方式: 行锁(Record Lock):锁直接加在索引记录上面,锁住的是key. 间隙锁(Gap Lock): 锁定索引记录间隙,确保索引记录的间隙不变.间隙锁是 ...

  5. maven 是什么?

    在了解maven的概念之前,我一直都在项目中使用maven,但是对于maven的了解,只能说连个皮毛都算不上,一直到项目中,自己机械化的deploy项目的时候,发现报错,赶紧报告开发组长,这私服是不是 ...

  6. 使用Modbus批量读取寄存器地址

    使用modbus单点读取地址是轮询可能会导致效率很低,频繁发送读取报文会导致plc响应时间拉长,批量读取可大大减少数据通信的过程,每次读取完成后,在内存中异步处理返回来的数据数组. modbus 功能 ...

  7. HDC2021技术分论坛:如何高效完成HarmonyOS分布式应用测试?

    作者:liuxun,HarmonyOS测试架构师 HarmonyOS是新一代的智能终端操作系统,给开发者提供了设备发现.设备连接.跨设备调用等丰富的分布式API.随着越来越多的开发者投入到Harmon ...

  8. shell脚本 mysqldump方式全备份mysql

    一.简介 源码地址 日期:2018/10/8 介绍:mysqldump方式全备份脚本,并保存固定天数的全备份 效果图: 二.使用 适用:centos6+ 语言:中文 注意:使用前先查看脚本,修改对应变 ...

  9. 为什么要用urlencode()函数进行url编码

    URLEncode就是将URL中特殊部分进行编码.URLDecoder就是对特殊部分进行解码. 因为当字符串数据以url的形式传递给web服务器时,字符串中是不允许出现空格和特殊字符的 譬如:你要传的 ...

  10. 华为HMS Core图形引擎服务携手三七游戏打造移动端实时DDGI技术

    在2021年HDC大会的主题演讲中提到,华为HMS Core图形引擎服务(Scene Kit)正协同三七游戏一起打造实时DDGI(动态漫反射全局光照:Dynamic Diffuse Global Il ...