SpringCloud Eureka整合使用与配置

  1. 首先搭建Eureka Server
  • 首先导入Eureka的依赖
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
  • 设置application.yml 参考地址https://www.cnblogs.com/Sky0914/p/11717413.html
#端口号
spring:
  application:
    name: eureka
# 详见EurekaServerConfigBean,需要注意与Client和Instance在client的jar包不同,Server是在server的jar包。
# eureka的各项配置可见EurekaXXXConfigBean。
eureka:
  datacenter: cloud           # 修改Eureka监控页面的System Status Data center
  environment: test            # 修改Eureka监控页面的System Status Environment
  instance:
    hostname: localhost
    prefer-ip-address: true
    leaseRenewalIntervalInSeconds:  5 # 心跳间隔,5秒
    leaseExpirationDurationInSeconds: 10  # 没有心跳的淘汰时间,10秒
    instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${spring.application.instance_id:${server.port}} #SpringCloud 2.0 已经改成 ${spring.cloud.client.ip-address} 了,于是修改
  client:
    healthcheck:
      enabled: true
    # 默认情况下,eureka server同时也是eureka client,用于相互注册形成高可用eureka服务。
    # 单点时,如果registerWithEureka配置为true,则eureka server会报错Cannot execute request on any known server
    registerWithEureka: false # 是否注册到eureka服务,默认为true,当前已为eureka server,且单点eureka,故配置为false
    fetchRegistry: false # eureka之间如果网络不稳定,客户端一般也会缓存了注册列表,因此eureka服务可以不缓存,我觉得更能确保eureka之间的一致。
    serviceUrl:
      # registerWithEureka关闭后,defaultZone没有配置的必要。如果打开,即使配置为本机一样报错。
      # 也就是说defaultZone任何时候都没有配置为localhost的必要。这点上John的配置更好,永超和周立包括志朋的配置有点多余。
      # 但是周立说的对,这个属性默认配置是http://localhost:8761/eureka,也就是当你没有用户名密码安全认证时,本机调试时,客户端可以不配置,
      # 但对于server来说,这个默认没有什么作用。对于client来说,也只有调试的时候有点作用。
      # 但有一点很奇怪,既然默认了8761端口,为什么eureka server的默认端口要用8080而不是8761呢?
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #应用的主机名称
  #      defaultZone: http://${security.user.name}:${security.user.password}@localhost:${server.port}/eureka # 本配置应删除。
  server:
    # 自我保护机制,默认true。打开后,心跳失败在15分钟内低于85%(renewalPercentThreshold)的服务,也不进行剔除。
    # 关闭后,主页提示:RENEWALS ARE LESSER THAN THE THRESHOLD. THE SELF PRESERVATION MODE IS TURNED OFF.
    # THIS MAY NOT PROTECT INSTANCE EXPIRY IN CASE OF NETWORK/OTHER PROBLEMS.
    enableSelfPreservation: true # 本地调试时可fasle关闭。但生产建议打开,可防止因网络不稳定等原因导致误剔除服务。
    renewalPercentThreshold: 0.85 # 默认85%
    # 在服务器接收请求之前等待的初始时间,默认等待5min(John Carnell)
    waitTimeInMsWhenSyncEmpty: 5 # John说开发时最好注释此配置,服务注册需要3次心跳,每次10s,也就是30s才能显示在eureka。但是为什么我这里马上就显示呢?
    # eureka server刷新readCacheMap的时间,注意,client读取的是readCacheMap,这个时间决定了多久会把readWriteCacheMap的缓存更新到readCacheMap上
    # 默认30秒,eclipse提示默认0应该是错误的,源代码中responseCacheUpdateIntervalMs = 30 * 1000。
    response-cache-update-interval-ms: 3000 # 网上很多专家的博客错误写成responseCacheUpdateInvervalMs,请注意。这里配置为3秒。
    # eureka server缓存readWriteCacheMap失效时间,这个只有在这个时间过去后缓存才会失效,失效前不会更新,
    # 过期后从registry重新读取注册服务信息,registry是一个ConcurrentHashMap。
    # 由于启用了evict其实就用不太上改这个配置了,默认180s
    responseCacheAutoExpirationInSeconds: 180
    # 启用主动失效,并且每次主动失效检测间隔为3s。源码evictionIntervalTimerInMs = 60 * 1000,默认一分钟。
    # 需要注意的是该配置会打印INFO日志,增加info日志量,修改后从每60秒打印一次变成3秒打印一次。
    evictionIntervalTimerInMs: 3000 # 注意不要写成EvictionIntervalTimerInMs,yml大小写敏感。
  • 在主启动类中加入注解
@SpringBootApplication
//开启服务注册中心
@EnableEurekaServer
public class EurekaMain7001 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaMain7001.class,args);
    }
}
  • 通过ip地址加上端口号访问
    在这里插入图片描述
  1. 搭建Eureka Client
  • 首先导入Eureka的依赖
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

-设置application配置文件

server.port=8001
#服务名称
spring.application.name=cloud-payment-service

#eureka实例名称
eureka.instance.hostname=localhost
#表示向注册中心注册自己
eureka.client.register-with-eureka=true
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
eureka.client.fetch-registry=true
#设置与Eureka server交互的地址查询服务和注册服务都需要依赖这个地址
eureka.client.service-url.defaultZone=http://localhost:7001/eureka/
#设置别名
eureka.instance.instance-id=payment8001
#显示ip地址
eureka.instance.prefer-ip-address=true
  • 主启动类上添加注解
@SpringBootApplication
@MapperScan(basePackages = {"com.pers.springcloud.mapper"})
//客户端
@EnableEurekaClient
//服务发现
@EnableDiscoveryClient
public class PaymentMain8001 {

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

-再次访问localhost:7001,服务注册成功!
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值