公众号:Java全栈架构师
分享Java技术干货,包含多线程JVM、Spring Boot、Spring Cloud、IDEA、Dubbo、Zookeeper、Redis、架构设计、微服务、消息队列、Git、面试、程序员攻略、最新动态等。回复【资料包】领取大量Java学习资料。
环境:
IDEA
JDK1.8
Spring Cloud Hoxton.M3
Spring Boot 2.2.0
一、Eureka简介
Eureka是Netflix开发的,一个基于REST服务的,服务注册与发现的组件。它主要包括两个组件:Eureka Server 和 Eureka Client
Eureka Client:一个Java客户端,用于简化与 Eureka Server 的交互(通常就是微服务中的客户端和服务端)
Eureka Server:提供服务注册和发现的能力(通常就是微服务中的注册中心)
Eureka:是纯正的servlet应用,需构建成war包部署,其使用了 Jersey 框架实现自身的 RESTful HTTP接口,peer之间的同步与服务的注册全部通过 HTTP 协议实现,Eureka定时任务(发送心跳、定时清理过期服务、节点同步等)通过 JDK 自带的 Timer 实现,内存缓存使用Google的guava包实现。
eureka-core 模块包含了功能的核心实现:
com.netflix.eureka.cluster - 与peer节点复制(replication)相关的功能。
com.netflix.eureka.lease - 即”租约”, 用来控制注册信息的生命周期(添加、清除、续约)。
com.netflix.eureka.registry - 存储、查询服务注册信息。
com.netflix.eureka.resources - RESTful风格中的”R”, 即资源。相当于SpringMVC中的Controller。
com.netflix.eureka.transport - 发送HTTP请求的客户端,如发送心跳。
com.netflix.eureka.aws - 与amazon AWS服务相关的类。
二、创建项目
File ----- New -----Project
Spring Initializr ----- Next
等待创建项目中
输入 Group 和 Artifact,点击 Next
如果出现如下提示,则artifact 禁止大写,否则保存会提示“Cannot Save Settings”
选择Spring cloud Discovery -----Eureka Server ---- Next
创建完成 点击 Finish 则在新窗口打开新建的工程
Spring Cloud 创建成功,目录结构如下:
三、完善项目
由于刚刚创建的文件是初始化默认的项目,需要添加相应注解和配置文件。具体详情如下:
application.properties格式:
pom.xml
生成的pom.xml如下:
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.2.0.RELEASE
cn.mcus
eurekaserver
0.0.1-SNAPSHOT
eurekaserver
Demo project for Spring Boot
UTF-8
UTF-8
1.8
Hoxton.M3
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
2.
EurekaserverApplication.java启动类添加@EnableEurekaServer注解,Eureka作为服务注册中心的注解。
设置配置文件application.properties,SpringCloud支持application.properties 和 )appication.yml两种格式分别如下。
#注册中心默认端口就是8761,也可通过下面的方式定义其他端口,defultZone:设置eureka服务器所在地址,注册服务和查询服务都依靠这个地址。
server.port=8761
#eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
#是否注册到eureka服务器,由于自己就是eureka服务器,没有必要注册自身。
eureka.client.registerWithEureka=false
#是否从eureka服务器获取注册信息,这里也没必要。
eureka.client.fetchRegistry=false
#是否开启自我保护模式,默认为true。
eureka.server.enable-self-preservation=true
#续期时间,即扫描失效服务的间隔时间(缺省为60*1000ms)
eureka.server.eviction-interval-timer-in-ms=10000
appication.yml格式:
eureka.client.fetchRegistry=false
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
*本项目以application.properties格式为例。
四、运行项目
1.进入EurekaserverApplication.java 启动类,右击选择Run。
2.如下,项目已启动
3.在浏览器中输入 http://localhost:8761/ 服务注册中心启动,出现如下页面
五、项目代码
pom.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.2.0.RELEASE
cn.scpro
eurekaserver
0.0.1-SNAPSHOT
eurekaserver
Demo project for Spring Boot
UTF-8
UTF-8
1.8
Hoxton.M3
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
application.properties文件
#注册中心默认端口就是8761,也可通过下面的方式定义其他端口,defultZone:设置eureka服务器所在地址,注册服务和查询服务都依靠这个地址。
server.port=8761
#eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
#是否注册到eureka服务器,由于自己就是eureka服务器,没有必要注册自身。
eureka.client.registerWithEureka=false
#是否从eureka服务器获取注册信息,这里也没必要。
eureka.client.fetchRegistry=false
#是否开启自我保护模式,默认为true。
eureka.server.enable-self-preservation=true
#续期时间,即扫描失效服务的间隔时间(缺省为60*1000ms)
eureka.server.eviction-interval-timer-in-ms=10000
EurekaserverApplication.java
package cn.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaserverApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaserverApplication.class, args);
}
}
以上就是一个Spring Cloud 的服务注册中心的创建过程。欢迎交流学习。
推荐阅读
☆ 2021年开工大吉
☆ 《Java 面经手册》PDF,全书 417 页 11.5 万字,完稿&发版!
☆ 如何在Java POJO转JSON时忽略掉一些属性
☆ 《重学 Java 设计模式》PDF 出炉了 - 小傅哥,肝了50天写出18万字271页的实战编程资料
☆ 【Spring Cloud 系列】一、Spring Cloud 入门前章:初识Spring Cloud
☆ 半年招聘筛选了400+份简历,告诉你怎么写容易被撩!
感谢阅读
如果觉得这篇文章不错,来个分享、点赞、在看三连吧,让更多的人也看到。
关注公众号Java全栈架构师,领取大量全栈学习资料,定期推送新鲜干货好文,不定期发放福利。
扫描下方添加微信,进技术交流群和大厂的同学一起学习分享、交流成长。
喜欢这篇文章的话,分享给你朋友哈!
Java全栈架构师
ID:JavaZhan
▲长按二维码“识别”关注