基于Spring Boot 2.0.3版本——Eureka的使用(一)

Eureka 是Netflix 开源的服务发现组件,本身是一个基于REST的服务。它包含了Server和Client两部分。Spring cloud 将它集成在即项目Spring Cloud Netflix中,从而实现微服务的注册和发现

接下来的代码是基于SpringBoot2.0.3基础上实现的,分Server和Client两部分来说明

==========Server部分==========

启动类

@SpringBootApplication
//启动一个服务注册中心提供给其他应用进行对话
@EnableEurekaServer
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

关键部分的依赖


        
            org.springframework.cloud
            spring-cloud-starter-eureka-server
            1.3.4.RELEASE
        

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Finchley.RELEASE
                pom
                import
            
        
    

    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

application.yml部分

server:
  port: 8761
spring:
  application:
    name: server
eureka:
  instance:
    hostname: localhost
    # 使用IP注册
    preferIpAddress: true
    # 心跳间隔
    lease-renewal-interval-in-seconds: 3
    # 服务失效时间: 如果多久没有收到请求,则可以删除服务
    lease-expiration-duration-in-seconds: 7
  client:
    # 关闭eureka client
    # enabled: false
    # 注册自身到eureka服务器
    register-with-eureka: true
    # 表示是否从eureka服务器获取注册信息
    fetch-registry: false
    # 客户端从Eureka Server集群里更新Eureka Server信息的频率
    eureka-service-url-poll-interval-seconds: 60
    # 定义从注册中心获取注册服务的信息
    registry-fetch-interval-seconds: 5
    # 设置eureka服务器所在的地址,查询服务和注册服务都需要依赖这个地址
    service-url:
      defaultZone: http://${eureka.instance.hostname}:8761/eureka/
  server:
    # renewal-percent-threshold: 0.1
    # 关闭自我保护模式
    enable-self-preservation: false
    # Eureka Server 自我保护系数,当enable-self-preservation=true时,启作用
    # renewal-percent-threshold:
    # 设置清理间隔,单位为毫秒,默认为0
    eviction-interval-timer-in-ms: 3000
    # 设置如果Eureka Server启动时无法从临近Eureka Server节点获取注册信息,它多久不对外提供注册服务
    wait-time-in-ms-when-sync-empty: 6000000
    # 集群之间相互更新节点信息的时间频率
    peer-eureka-nodes-update-interval-ms: 6000

启动Eureka Server
可以从控制台看到 Eureka 服务器已经建立成功,接下来Eureka Client会定时向EurekaServer端进行服务续约(心跳) :


image.png

访问 http://localhost:8761/ 可以看到以下图片,

image.png

==========Client部分==========

启动类

@SpringBootApplication
//声明这是一个Eureka Client
@EnableDiscoveryClient
public class EurekaClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApplication.class, args);
    }
}
server:
  port: 9092
spring:
  application:
    name: eureka-client
eureka:
  instance:
    hostname: localhost
    # 使用IP注册
    preferIpAddress: true
    # 心跳间隔
    lease-renewal-interval-in-seconds: 3
    # 服务失效时间: 如果多久没有收到请求,则可以删除服务
    lease-expiration-duration-in-seconds: 7
  client:
    # 关闭eureka client
    # enabled: false
    # 注册自身到eureka服务器
    register-with-eureka: true
    # 表示是否从eureka服务器获取注册信息
    fetch-registry: false
    # 客户端从Eureka Server集群里更新Eureka Server信息的频率
    eureka-service-url-poll-interval-seconds: 60
    # 定义从注册中心获取注册服务的信息
    registry-fetch-interval-seconds: 5
    # 设置eureka服务器所在的地址,查询服务和注册服务都需要依赖这个地址
    service-url:
      defaultZone: http://${eureka.instance.hostname}:8761/eureka/

pom文件



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.3.RELEASE
         
    
    com.example
    eureka
    0.0.1-SNAPSHOT
    eureka
    Demo project for Spring Boot
    
        1.8
    
    
        
            com.basil.common
            common
            0.0.1-SNAPSHOT
        
        
            org.springframework.boot
            spring-boot-starter-web-services
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
        
            org.springframework.cloud
            spring-cloud-starter-eureka-server
            1.3.4.RELEASE
        
    
    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Finchley.RELEASE
                pom
                import
            
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



启动 Eureka-Client 后可以在 Eureka-Server 控制台中看到 ,Client 会通过启动时初始化的定时任务定时向EurekaServer端进行服务续约(心跳)

image.png

同时 访问http://localhost:8761/,可以看到如下图,可以知道 Eureka-Client服务已被注册到 Eureka-Server上
image.png

你可能感兴趣的:(基于Spring Boot 2.0.3版本——Eureka的使用(一))