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. jenkins的sonarqube之代码检测的两种方法

    #:sonarqube下载地址,我们安装6.7  高版本已经不支持MySQL和Mariadb(最小3G内存) https://www.sonarqube.org/downloads/ #:安装文档 h ...

  2. Linux学习 - 分区与文件系统

    一.分区类型 1 主分区:总共最多只能分四个 2 扩展分区:只能有一个(主分区中的一个分区),不能存储数据和格式化,必须再划分成逻辑分区                               才 ...

  3. 在调用系统相册时,UIIMagePickerController使用中偷换StatusBar颜色的问题

    在调用系统相册时,UIIMagePickerController使用中偷换StatusBar颜色的问题 此时解决办法是 #pragma mark - UIImagePickerController D ...

  4. LVS nat模型+dr模型

    nat模型 在 rs1 和 rs2  安装httpd  并配置测试页,启动 [root@rs1 ~]# yum install httpd -y[root@rs1 ~]# echo "thi ...

  5. css实现项目中的加载动画

    // =========================================第一个动画 <template> <!-- 这个组件用于 页面下滑到底部时 展示加载动画 -- ...

  6. Cnblog博客美化

    具体的使用教程文档在这里 BNDong/Cnblogs-Theme-SimpleMemory 简要的操作如下: 博客园 - 管理 - 设置 值得注意得是: 要想JS代码要申请才可以使用 博客侧边栏 可 ...

  7. 【Java 基础】Java Enum

    概览 在本文中,我们将看到什么是 Java 枚举,它们解决了哪些问题以及如何在实践中使用 Java 枚举实现一些设计模式. enum关键字在 java5 中引入,表示一种特殊类型的类,其总是继承jav ...

  8. JSP 文字乱码、${}引用无效

    问题: 代码:<form action="/test/requestPost.do" method="post"> <input type=& ...

  9. Linux服务加入systemctl|service管理

    一.加入systemctl 1.添加 vim /usr/lib/systemd/system/user_timejob.service # copy to /usr/lib/systemd/syste ...

  10. Sentry 开发者贡献指南 - 前端(ReactJS生态)

    内容整理自官方开发文档 系列 1 分钟快速使用 Docker 上手最新版 Sentry-CLI - 创建版本 快速使用 Docker 上手 Sentry-CLI - 30 秒上手 Source Map ...