1.说明

本文详细介绍Spring Cloud创建Eureka模块的方法,
基于已经创建好的Spring Cloud父工程,
请参考SpringCloud创建项目父工程,
在里面创建Eureka模块,
用于Spring Cloud的微服务注册。
这里介绍的是Eureka单机版。

2.创建eureka-server模块

这一步创建一个Maven Module,
作为Spring Cloud的父工程下的一个子工程:
在父工程spring-cloud-demo上右键 -> New -> Other... -> Maven -> Maven Project

勾选Create a simple project(skip archetype selection),
输入Module Name:eureka-server,
查看Parent Project:spring-cloud-demo,
如果不是自己选择的父工程,请重新选择。

点击Finish完成工程创建。

创建后可以看到pom.xml如下:

<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>
<parent>
<groupId>com.yuwen.spring</groupId>
<artifactId>spring-cloud-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>eureka-server</artifactId>
</project>

3.添加依赖

在pom.xml中增加eureka-server的依赖:

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>

4.新增配置文件

在src/main/resource目录下新增application.yml文件,
并且增加如下配置:

server:
port: 7001 eureka:
instance:
#eureka服务端的实例名字(主机名)
hostname: localhost
client:
#表示不向注册中心注册自己
register-with-eureka: false
#表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
#设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

5.新增主启动类

在src/main/java目录下新增主启动类,
Package:com.yuwen.spring.eureka
Name:EurekaServerApplication

然后修改EurekaServerApplication.java如下,
注意一定要有@EnableEurekaServer注解,
表示这是一个Eureka服务注册中心:

package com.yuwen.spring.eureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}

6.启动eureka-server

右键主启动类EurekaServerApplication.java,
Run As ... -> Java Application
成功启动日志如下,
可以看到对外提供的服务端口是7001:

  .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.2.RELEASE) 2020-07-09 14:49:10.959 INFO 8164 --- [ main] c.y.s.eureka.EurekaServerApplication : No active profile set, falling back to default profiles: default
2020-07-09 14:49:11.474 WARN 8164 --- [ main] o.s.boot.actuate.endpoint.EndpointId : Endpoint ID 'service-registry' contains invalid characters, please migrate to a valid format.
2020-07-09 14:49:11.591 INFO 8164 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=ff22f1b9-a933-39aa-b63c-21b30208dbd7
2020-07-09 14:49:12.361 INFO 8164 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 7001 (http)
2020-07-09 14:49:12.370 INFO 8164 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-07-09 14:49:12.370 INFO 8164 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.29]
2020-07-09 14:49:12.474 INFO 8164 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-07-09 14:49:12.474 INFO 8164 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1503 ms
2020-07-09 14:49:12.591 WARN 8164 --- [ main] c.n.c.sources.URLConfigurationSource : No URLs will be polled as dynamic configuration sources.
2020-07-09 14:49:12.591 INFO 8164 --- [ 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-07-09 14:49:12.607 INFO 8164 --- [ main] c.netflix.config.DynamicPropertyFactory : DynamicPropertyFactory is initialized with configuration sources: com.netflix.config.ConcurrentCompositeConfiguration@2c7a8af2
2020-07-09 14:49:13.556 INFO 8164 --- [ main] c.s.j.s.i.a.WebApplicationImpl : Initiating Jersey application, version 'Jersey: 1.19.1 03/11/2016 02:08 PM'
2020-07-09 14:49:13.604 INFO 8164 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using JSON encoding codec LegacyJacksonJson
2020-07-09 14:49:13.605 INFO 8164 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using JSON decoding codec LegacyJacksonJson
2020-07-09 14:49:13.708 INFO 8164 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using XML encoding codec XStreamXml
2020-07-09 14:49:13.708 INFO 8164 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using XML decoding codec XStreamXml
2020-07-09 14:49:13.967 WARN 8164 --- [ main] o.s.c.n.a.ArchaiusAutoConfiguration : No spring.application.name found, defaulting to 'application'
2020-07-09 14:49:13.967 WARN 8164 --- [ main] c.n.c.sources.URLConfigurationSource : No URLs will be polled as dynamic configuration sources.
2020-07-09 14:49:13.967 INFO 8164 --- [ 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-07-09 14:49:14.160 INFO 8164 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-07-09 14:49:15.023 WARN 8164 --- [ 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-07-09 14:49:15.053 INFO 8164 --- [ main] o.s.c.n.eureka.InstanceInfoFactory : Setting initial instance status as: STARTING
2020-07-09 14:49:15.074 INFO 8164 --- [ main] com.netflix.discovery.DiscoveryClient : Initializing Eureka in region us-east-1
2020-07-09 14:49:15.075 INFO 8164 --- [ main] com.netflix.discovery.DiscoveryClient : Client configured to neither register nor query for data.
2020-07-09 14:49:15.081 INFO 8164 --- [ main] com.netflix.discovery.DiscoveryClient : Discovery Client initialized at timestamp 1594277355080 with initial instances count: 0
2020-07-09 14:49:15.110 INFO 8164 --- [ main] c.n.eureka.DefaultEurekaServerContext : Initializing ...
2020-07-09 14:49:15.112 WARN 8164 --- [ main] c.n.eureka.cluster.PeerEurekaNodes : The replica size seems to be empty. Check the route 53 DNS Registry
2020-07-09 14:49:15.147 INFO 8164 --- [ main] c.n.e.registry.AbstractInstanceRegistry : Finished initializing remote region registries. All known remote regions: []
2020-07-09 14:49:15.148 INFO 8164 --- [ main] c.n.eureka.DefaultEurekaServerContext : Initialized
2020-07-09 14:49:15.157 INFO 8164 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 2 endpoint(s) beneath base path '/actuator'
2020-07-09 14:49:15.179 INFO 8164 --- [ main] o.s.c.n.e.s.EurekaServiceRegistry : Registering application UNKNOWN with eureka with status UP
2020-07-09 14:49:15.180 INFO 8164 --- [ Thread-11] o.s.c.n.e.server.EurekaServerBootstrap : Setting the eureka configuration..
2020-07-09 14:49:15.180 INFO 8164 --- [ Thread-11] o.s.c.n.e.server.EurekaServerBootstrap : Eureka data center value eureka.datacenter is not set, defaulting to default
2020-07-09 14:49:15.181 INFO 8164 --- [ Thread-11] o.s.c.n.e.server.EurekaServerBootstrap : Eureka environment value eureka.environment is not set, defaulting to test
2020-07-09 14:49:15.195 INFO 8164 --- [ Thread-11] o.s.c.n.e.server.EurekaServerBootstrap : isAws returned false
2020-07-09 14:49:15.195 INFO 8164 --- [ Thread-11] o.s.c.n.e.server.EurekaServerBootstrap : Initialized server context
2020-07-09 14:49:15.195 INFO 8164 --- [ Thread-11] c.n.e.r.PeerAwareInstanceRegistryImpl : Got 1 instances from neighboring DS node
2020-07-09 14:49:15.196 INFO 8164 --- [ Thread-11] c.n.e.r.PeerAwareInstanceRegistryImpl : Renew threshold is: 1
2020-07-09 14:49:15.196 INFO 8164 --- [ Thread-11] c.n.e.r.PeerAwareInstanceRegistryImpl : Changing status to UP
2020-07-09 14:49:15.204 INFO 8164 --- [ Thread-11] e.s.EurekaServerInitializerConfiguration : Started Eureka Server
2020-07-09 14:49:15.216 INFO 8164 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 7001 (http) with context path ''
2020-07-09 14:49:15.216 INFO 8164 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 7001
2020-07-09 14:49:15.848 INFO 8164 --- [ main] c.y.s.eureka.EurekaServerApplication : Started EurekaServerApplication in 6.65 seconds (JVM running for 7.072)

7.查看Eureka服务

在浏览器中访问Eureka服务页面:

http://localhost:7001

可以看到如下页面:

8.客户端注册到Eureka

Eureka服务启动后,
客户端可以注册到Eureka,
在application.yml中配置自己的主机名,
以及连接的eureka地址:

eureka:
instance:
#eureka客户端的实例名字(主机名)
hostname: gateway-service
client:
service-url:
#表示向注册中心注册自己
register-with-eureka: true
#表示需要去注册中心检索服务
fetch-registry: true
#与eureka server交互的地址,包括查询服务和注册服务
defaultZone: http://localhost:7001/eureka

然后在客户端的主启动类上面加@EnableEurekaClient注解,
这样客户端服务启动之后,
可以看到Eureka页面已经有客户端注册了:

SpringCloud创建Eureka模块的更多相关文章

  1. SpringCloud创建Eureka模块集群

    1.说明 本文详细介绍Spring Cloud创建Eureka模块集群的方法, 基于已经创建好的Spring Cloud Eureka Server模块, 请参考SpringCloud创建Eureka ...

  2. SpringCloud创建Config模块

    1.说明 本文详细介绍Spring Cloud创建Config模块的方法, 基于已经创建好的Spring Cloud父工程, 请参考SpringCloud创建项目父工程, 创建Config模块这个子工 ...

  3. SpringCloud创建Gateway模块

    1.说明 本文详细介绍Spring Cloud创建Gateway模块的方法, 基于已经创建好的Spring Cloud父工程, 请参考SpringCloud创建项目父工程, 和已经创建好的Eureka ...

  4. SpringCloud创建Eureka Client服务注册

    1.说明 本文详细介绍微服务注册到Eureka的方法, 即Eureka Client注册到Eureka Server, 这里用任意一个Spring Cloud服务为例, 比如下面已经创建好的Confi ...

  5. SpringCloud创建Eureka

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

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

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

  7. SpringCloud创建Config读取本地配置

    1.说明 Config Server获取配置支持的方式很多, 包括Git仓库(github/gitee等),任何与JDBC兼容的数据库, Subversion,Hashicorp Vault,Cred ...

  8. SpringCloud创建Config Client配置读取

    1.说明 本文详细介绍配置中心客户端使用方法, 即Config Client到Config Server读取配置, 这里以创建Config Client服务为例, 基于已经创建好的Config Ser ...

  9. 创建多模块springcloud应用eureka server和client和消费端demo

    使用环境是 STS + maven 1 创建父级 项目,springcloud-demo1 new -> maven project -> 按照要求进行配置即可.然后删除 src目录,因为 ...

随机推荐

  1. Linux基础命令---nslookup查询域名工具

    nslookup nslookup是一个查询DNS域名的工具,它有交互和非交互两种工作模式. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedora.   1.语法     ...

  2. Ibatis中SqlMapClientTemplate和SqlMapClient的区别

    SqlMapClientTemplate是org.springframework.orm.ibatis下的 而SqlMapClient是ibatis的 SqlMapClientTemplate是Sql ...

  3. CentOS6设置Django开发环境

    今天在我的Centos6.5机器上安装 Django 开发环境,在安装完使用 "django-admin.py startproject myapp" 创建应用的时候报了下面的错误 ...

  4. Element-ui 中对表单进行验证

    Element-ui 中对表单(Form)绑定的对象中的对象属性进行校验 如果是直接绑定属性,是可以的,但是绑定对象中的属性就需要特别处理,需要在rules中添加双引号 " "或者 ...

  5. 【JAVA】【JVM】内存结构

    虽然jvm帮我们做了内存管理的工作,但是我们仍需要了解jvm到底做了什么,下面我们就一起去看一看 jvm启动时进行一系列的工作,其中一项就是开辟一块运行时内存.而这一块内存中又分为了五大区域,分别用于 ...

  6. java上传图片或文件

    转载至:http://www.xdx97.com/#/single?bid=8b351a73-922c-eadc-512e-9e248a3efde9 前端通过form表单用post方式提交文件,后台进 ...

  7. Linux 目录结构及详细操作

    目录 Linux 目录结构及详细操作 目录结构 目录结构的特点 目录结构挂载 目录结构发展 关闭selinux(了解) 重要目录说明(etc目录说明) 1.网卡配置文件 2.解析配置文件 3.主机名称 ...

  8. Linux进程操作

    查看进程启动时间 ps -eo pid,lstart | grep PID 查看进程的运行多久 ps -eo pid,etime |grep PID 查看进程中启动了哪些线程 top -H -p pi ...

  9. 选择…Select…(Power Query 之 M 语言)

    选择行: 筛选Table.SelectRows-文本与数值 筛选Table.SelectRows-日期与时间 保留错误行:= Table.SelectRowsWithErrors( 表, {" ...

  10. LuoguP4263 [Code+#3]投票统计 题解

    Content 有 \(t\) 组询问,每组询问给定一个长度为 \(n\) 的数列,请将出现次数最多的数按照从小到大的顺序输出,或者这些数在数列中出现的次数都相等. 数据范围:\(t\) 未知,\(n ...