SpringCloud框架初探(四): Eureka Client客户端搭建

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013628152/article/details/82894546

源码地址:SpringCloud学习源码

1、Eureka客户端搭建

@SpringBootApplication
@EnableEurekaClient
public class ProviderUserApplication {

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

使用@EnableEurekaClient,启动Eureka客户端。

然后再看客户端的配置文件:
定义客户端的端口号,还是使用server,port

server:
  port: 7901
spring:
  application:
    name: provider-user

需要指明spring.application.name,这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name 。

配置Eureka Client其他参数:

eureka:
  client:
    healthcheck:
      enabled: true
    serviceUrl:
      defaultZone: http://user:password123@localhost:8761/eureka
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
    metadata-map:
      zone: ABC      # eureka可以理解的元数据
      lilizhou: BBC  # 不会影响客户端行为
    lease-renewal-interval-in-seconds: 5

eureka.client.healthcheck.enabled = true 开启Eureka客户端的健康检查
eureka.client.serviceUrl.defaultZone 指定客户端对应的注册中心服务器的地址
eureka.instance.prefer-ip-address = true 表示注册到注册中心的是IP
eureka.instance.instance-id 表示注册到注册中心实例的ID规则

eureka.instance.lease-renewal-interval-in-seconds 表示续约更新时间间隔,也就是客户端健康检查的心跳时间间隔。

2、启动Eureka客户端,并查看服务器状态

当client向server注册时,它会提供一些元数据,例如主机和端口,URL,主页等。Eureka server 从每个client实例接收心跳消息。 如果心跳超时,则通常将该实例从注册server中删除。

在这里插入图片描述

可以看到PROVIDER-USER(客户端的名称),也已经注册到了Eureka注册中心。

provider-user:10.1.18.161:7900 也就是instance-id:

 instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}

我在微信订阅号等你!
这里写图片描述

猜你喜欢

转载自blog.csdn.net/u013628152/article/details/82894546
今日推荐