SpringCloud

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: http://localhost:8762/eureka/      # 将自己注册到peer2这个Eureka上面去
#  server:
#    enable-self-preservation: false
 
---
spring:
  profiles: peer2
server:
  port: 8762
eureka:
  instance:
    hostname: peer2
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
#  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:
  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端访问的格式为http://name:password@url,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