博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringCloud - Eureka服务管理
阅读量:4600 次
发布时间:2019-06-09

本文共 2707 字,大约阅读时间需要 9 分钟。

Eureka Server

启动多个服务集群,集群之间相互注册
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }
}
spring:
  application:
    name: microservice-discovery-eureka-ha
 
---
spring:
  profiles: peer1                                 # 指定profile=peer1
server:
  port: 8761
eureka:
  instance:
    hostname: peer1                               # 指定当profile=peer1时,主机名是peer1
  client:
    serviceUrl:
      defaultZone:       # 将自己注册到peer2这个Eureka上面去
#  server:
#    enable-self-preservation: false
 
---
spring:
  profiles: peer2
server:
  port: 8762
eureka:
  instance:
    hostname: peer2
  client:
    serviceUrl:
      defaultZone:
#  server:
#    enable-self-preservation: false
 
java -jar microservice-discovery-eureka-ha-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
java -jar microservice-discovery-eureka-ha-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2

 

Eureka Client

将服务提供者注册到服务集群
@EnableDiscoveryClient
@SpringBootApplication
public class ProviderUserApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProviderUserApplication.class, args);
    }
}
server:
  port: 8000
spring:
  application:
    name: microservice-provider-user
  jpa:
    generate-ddl: false
    show-sql: true
    hibernate:
      ddl-auto: none
  datasource:                           # 指定数据源
    platform: h2                        # 指定数据源类型
    schema: classpath:schema.sql        # 指定h2数据库的建表脚本
    data: classpath:data.sql            # 指定h2数据库的数据脚本
logging:                                # 配置日志级别,让hibernate打印出执行的SQL
  level:
    root: INFO
    org.hibernate: INFO
    org.hibernate.type.descriptor.sql.BasicBinder: TRACE
    org.hibernate.type.descriptor.sql.BasicExtractor: TRACE
eureka:
  client:
    serviceUrl:
      defaultZone:
  instance:
    prefer-ip-address: true //注册IP到Server,默认为host name
    lease-renewal-interval-in-seconds: 1
    lease-expiration-duration-in-seconds: 2

 

小结

  • 新版本开启Actuator功能需要配置:management.security.enable=false
  • Eureka包含两个组件,Eureka server和Eureka client,他们的作用是:
        (1)Server提供服务发现的功能,微服务再启动后,会向Server注册自己的信息,包括ip,port,微服务名称,Server会存储这些信息;
        (2)Client是Java客户端,启动后会周期性(默认30s)向Server发送心跳以续约自己的“租期”;
        (3)Server在一定时间内没有接收到某个服务实例的心跳,Server会注销该实例(默认90s);
        (4)默认情况下,Server也是Client,多个Server实例,互相之间通过复制的方式,来实现服务注册表中的数据同步;
        (5)Client会缓存服务注册表中的信息,从而无需每次请求都查询Server,从而降低了Server的压力,其次即使Server所有的节点都已经宕机,服务消费者依然可以是使用缓存中的信息找到服务提供者并完成调用。
  • 如果Server为单个,可以使用eureka.client.registerWithRegister=false和eureka.client.fetchRegistry=false,即不注册信息到Eureka同时也不获取注册表的信息。
  • Server可以通过Security添加账户密码,Client端访问的格式为,Server的application.xml配置如下:
security.basic.enabled=true //开启基于HTTP basic的认证
security.user.name=xxx
security.user.password=xxx
  •  自我保护模式:Server在一段时间内没有接受到某个服务实例的心跳,Server会注销该实例,但是这样会比较危险,Server使用自我保护的模式进行处理,一旦进入该模式,Sever就会保护服务注册表的信息,不再删除服务注册表的数据(也就是不注销任何微服务),当故障恢复后,Server节点会自动退出自我保护模式。
  • Server集群后,为了避免极端的情况,在Client端配置多个eureka.client.serviceUrl.defaultZone
 
 
 

转载于:https://www.cnblogs.com/liguochun/p/8433465.html

你可能感兴趣的文章
现实世界的 Windows Azure: IT 公司提高其旗舰产品,为更多客户提供云解决方案
查看>>
梦断代码读后感之开始篇
查看>>
Javascript常用对象的属性和方法
查看>>
Arduino live weather broadcasting 实时天气站
查看>>
了解Vue.js
查看>>
利用SQL Profiler处理开销较大的查询
查看>>
html中offsetTop、clientTop、scrollTop、offsetTop各属性
查看>>
项目优化中关于ob系列函数中的常用函数的讲解
查看>>
Oracle学习 第1、2天之高级查询
查看>>
素数筛选法
查看>>
数据探索
查看>>
Android Studio & SDK & JDK & setting path
查看>>
HDU 4639 Hehe
查看>>
shell if 语句
查看>>
NVelocity标签使用详解
查看>>
HTML第九章
查看>>
导出insert语句
查看>>
结对作业实验报告
查看>>
java设计模式----单例模式
查看>>
静态链接库(lib)、动态链接库(dll)与动态链接库的导入库(lib)
查看>>