目录
一、eureka是什么?
二、构建项目
1.引入jar包
2.完整pom内容
3.yml配置文件
4.Application,main方法
三、方法运行
一、eureka是什么?
Spring-Cloud Euraka是Spring Cloud集合中一个组件,它是对Euraka的集成,用于服务注册和发现。Eureka是Netflix中的一个开源框架。它和 zookeeper、Consul一样,都是用于服务注册管理的,同样,Spring-Cloud 还集成了Zookeeper和Consul。
在项目中使用Spring Cloud Euraka的原因是它可以利用Spring Cloud Netfilix中其他的组件,如zull等,因为Euraka是属于Netfilix的。
eureka详细划分可分为三个部分:
- Eureka Server 提供服务注册和发现
- Service Provider 服务提供方,将自身服务注册到Eureka,从而使服务消费方能够找到
- Service Consumer服务消费方,从Eureka获取注册服务列表,从而能够消费服务
此文章仅针对Eureka Server服务注册和发现进行学习。
二、构建项目
1.引入jar包
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
2.完整pom内容
com.springcloud
com.sc2020
1.0-SNAPSHOT
4.0.0
cloud-eureka-server7001
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
com.sc2020
cloud-api-commons
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-devtools
runtime
org.springframework.boot
spring-boot-devtools
runtime
true
org.springframework.boot
spring-boot-starter-test
test
3.yml配置文件
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com #eureka服务端的实例名称
client:
register-with-eureka: false #false表示不向注册中心注册自己
fetch-registry: false #false表示自己端就是注册中心
service-url:
defaultZone: http://eureka7001.com:7001/eureka/ #单机
# defaultZone: http://eureka7001.com:7001/eureka/, http://eureka7002.com:7002/eureka/ #集群, eureka7001.com在配置hosts文件时生效,未配置时使用localhost代替
server:
# 关闭自我保护机制,保证不可用服务被及时剔除
enable-self-preservation: false
eviction-interval-timer-in-ms: 2000
spring:
application:
name: cloud-eureka-service7001
1.引入jar包
org.springframework.cloud spring-cloud-starter-netflix-eureka-server
2.完整pom内容
com.springcloud
com.sc2020
1.0-SNAPSHOT
4.0.0
cloud-eureka-server7001
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
com.sc2020
cloud-api-commons
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-devtools
runtime
org.springframework.boot
spring-boot-devtools
runtime
true
org.springframework.boot
spring-boot-starter-test
test
3.yml配置文件
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com #eureka服务端的实例名称
client:
register-with-eureka: false #false表示不向注册中心注册自己
fetch-registry: false #false表示自己端就是注册中心
service-url:
defaultZone: http://eureka7001.com:7001/eureka/ #单机
# defaultZone: http://eureka7001.com:7001/eureka/, http://eureka7002.com:7002/eureka/ #集群, eureka7001.com在配置hosts文件时生效,未配置时使用localhost代替
server:
# 关闭自我保护机制,保证不可用服务被及时剔除
enable-self-preservation: false
eviction-interval-timer-in-ms: 2000
spring:
application:
name: cloud-eureka-service7001
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com #eureka服务端的实例名称
client:
register-with-eureka: false #false表示不向注册中心注册自己
fetch-registry: false #false表示自己端就是注册中心
service-url:
defaultZone: http://eureka7001.com:7001/eureka/ #单机
# defaultZone: http://eureka7001.com:7001/eureka/, http://eureka7002.com:7002/eureka/ #集群, eureka7001.com在配置hosts文件时生效,未配置时使用localhost代替
server:
# 关闭自我保护机制,保证不可用服务被及时剔除
enable-self-preservation: false
eviction-interval-timer-in-ms: 2000
spring:
application:
name: cloud-eureka-service7001
注意:eureka7001.com在配置hosts文件时生效,未配置时使用localhost代替
4.Application,main方法
@SpringBootApplication
@EnableEurekaServer
public class CloudEurekaServer7001Application {
public static void main(String[] args) {
SpringApplication.run(CloudEurekaServer7001Application.class, args);
}
}
在main方法中声明使用@EnableEurekaServer组件。
三、方法运行
输入地址 :http://localhost:7001/ 出现此页面则成功配置服务注册中心。



