Spring Cloud学习笔记【二】Eureka服务注册与发现

                     Spring Cloud学习笔记【二】Eureka服务注册与发现

一、Eureka简介

 

       Spring Cloud Eureka 是 Spring Cloud Netflix 微服务套件的一部分,基于 Netflix Eureka 做了二次封装,主要负责完成微服务架构中的服务治理功能,服务治理可以说是微服务架构中最为核心和基础的模块,他主要用来实现各个微服务实例的自动化注册与发现。功能类似于dubbo的注册中心,比如 Zookeeper。

服务注册:在服务治理框架中,通常都会构建一个注册中心,每个服务单元向注册中心登记自己提供的服务,将主机与端口号、版本号、通信协议等一些附加信息告知注册中心,注册中心按照服务名分类组织服务清单,服务注册中心还需要以心跳的方式去监控清单中的服务是否可用,若不可用需要从服务清单中剔除,达到排除故障服务的效果。

服务发现:由于在服务治理框架下运行,服务间的调用不再通过指定具体的实例地址来实现,而是通过向服务名发起请求调用实现。

Spring Cloud Eureka 使用 Netflix Eureka 来实现服务注册与发现,即包括了服务端组件,也包含了客户端组件,并且服务端和客户端均采用 Java 编写,所以 Eureka 主要适用与通过 Java 实现的分布式系统,或是与 JVM 兼容语言构建的系统,但是,由于 Eureka 服务端的服务治理机制提供了完备的 RESTful API,所以他也支持将非 Java 语言构建的微服务纳入 Eureka 的服务治理体系中来。

二、创建Eureka注册中心

依赖管理配置:指定spring cloud的版本,和添加eureka的server依赖

plugins {
    id 'org.springframework.boot' version '2.2.2.RELEASE'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
    mavenCentral()
}
ext {
    set('springCloudVersion', "Hoxton.SR1")
}
dependencies {
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}
dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}
test {
    useJUnitPlatform()
}

 application.properties配置文件配置

spring.application.name=eureka
#eureka默认端口
server.port=8761
#禁止自己当做服务去注册
eureka.client.register-with-eureka=false
#自我保护模式关闭
eureka.server.enable-self-preservation=false
# 清理无效节点的时间间隔(单位毫秒,默认是60*1000)
# 生产环境,不会频繁重启,所以,一定要把自我保护机制打开,否则网络一旦终端,就无法恢复。
eureka.server.eviction-interval-timer-in-ms=10000    

启动类配置 :添加@EnableEurekaServer注解即可

@SpringBootApplication
@EnableEurekaServer
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

启动访问http://localhost:8761/

三、创建客户端项目

依赖管理配置:指定spring cloud的版本,和添加eureka的client依赖

plugins {
    id 'org.springframework.boot' version '2.2.2.RELEASE'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
    mavenCentral()
}
ext {
    set('springCloudVersion', "Hoxton.SR1")
}
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}
dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}
test {
    useJUnitPlatform()
}

application.properties配置文件配置

spring.application.name=client
#往配置中心添加服务的地址
eureka.client.service-url.defaultZone:http://localhost:8761/eureka/

启动类配置:添加@EnableEurekaClient注解即可

@SpringBootApplication
@EnableEurekaClient
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

注册成功 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章