SpringCloud学习(一):EurekaClient

1.参考资料

https://cloud.spring.io/spring-cloud-static/Brixton.SR7/#_spring_cloud_netflix
https://github.com/Netflix/eureka/wiki
https://docs.spring.io/spring-boot/docs/1.3.5.RELEASE/reference/html/production-ready.html

2.eureka是netflix的一部分,作用是服务注册、发现

With a few simple annotations you can quickly enable and configure the common patterns inside your application 
and build large distributed systems with battle-tested Netflix components. 
The patterns provided include Service Discovery (Eureka), Circuit Breaker (Hystrix), Intelligent Routing (Zuul) and Client Side Load Balancing (Ribbon).

通过一些简单的注释,您可以快速启用和配置应用程序内部的通用模式,并使用经过测试的Netflix组件构建大型分布式系统。
提供的模式包括服务发现(Eureka),断路器(Hystrix),智能路由(Zuul)和客户端负载平衡(Ribbon)。

3.为什么使用eureka

尝试手动配置每个客户端或某种形式的约定可能非常困难并且非常脆弱。
Eureka是Netflix Service Discovery服务器和客户端。可以将服务器配置和部署为高可用性,每个服务器将有关已注册服务的状态复制到其他服务器。

4.服务注册

When a client registers with Eureka, it provides meta-data about itself such as host and port, health indicator URL, home page etc.
Eureka receives heartbeat messages from each instance belonging to a service. If the heartbeat fails over a configurable timetable,
the instance is normally removed from the registry.
当客户端向Eureka注册时,它将提供有关其自身的元数据,例如主机和端口,运行状况指示器URL,主页等。
Eureka从属于服务的每个实例接收心跳消息。如果心跳在可配置的时间表上进行故障转移,则通常会将实例从注册表中删除。

Example eureka client:

@SpringBootApplication
@EnableEurekaClient
@RestController
public class Application {

    @RequestMapping("/")
    public String home() {
        return "Hello world";
    }

    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(true).run(args);
    }

}

application.yml

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

The default application name (service ID), virtual host and non-secure port are ${spring.application.name},
${spring.application.name} and s e r v e r . p o r t r e s p e c t i v e l y . 默 认 的 应 用 程 序 名 称 ( 服 务 I D ) , 虚 拟 主 机 和 非 安 全 端 口 是 , {server.port} respectively. 默认的应用程序名称(服务ID),虚拟主机和非安全端口 是, server.portrespectively.ID{spring.application.name}, s p r i n g . a p p l i c a t i o n . n a m e 和 {spring.application.name}和 spring.application.name{server.port}。

@EnableEurekaClient makes the app into both a Eureka “instance” (i.e. it registers itself)
and a “client” (i.e. it can query the registry to locate other services).
The instance behaviour is driven by eureka.instance.* configuration keys,
but the defaults will be fine if you ensure that your application has a spring.application.name (this is the default for the Eureka service ID, or VIP).
See EurekaInstanceConfigBean and EurekaClientConfigBean for more details of the configurable options.
@EnableEurekaClient使应用程序同时进入Eureka的“实例”(即,它自己注册)和“客户端”(即,它可以查询注册表以定位其他服务)。
实例行为由eureka.instance.*配置键驱动,但是如果确保您的应用程序具有spring.application.name默认值(这是Eureka服务ID或VIP 的默认值),则默认值会很好 。
有关可配置选项的更多详细信息,请参见EurekaInstanceConfigBean和EurekaClientConfigBean。

5.Health Indicator

The health indicators for a Eureka instance default to “/health” respectively,
which are the default locations of useful endpoints in a Spring Boot Actuator application.
You need to change these, even for an Actuator application if you use a non-default context path or servlet path (e.g. server.servletPath=/foo)
or management endpoint path (e.g. management.contextPath=/admin). Example:

Eureka实例的运行状况指示器默认为“ / health”,它们是Spring Boot Actuator应用程序中有用端点的默认位置。
如果您使用非默认上下文路径或servlet路径(例如server.servletPath=/foo)或管理端点路径(例如management.contextPath=/admin),
则即使对于Actuator应用程序,也需要更改它们。例:

application.yml
eureka:
  instance:
    healthCheckUrlPath: ${server.servletPath}/health

6.健康检查

默认情况下,Eureka使用客户端心跳来确定客户端是否启动。除非另有说明,否则发现客户端不会根据Spring Boot Actuator传播应用程序的当前运行状况检查状态。
这意味着成功注册后,尤里卡将始终宣布该应用程序处于“up”状态。可以通过启用Eureka运行状况检查来更改此行为,这会导致应用程序状态传播到Eureka。
结果,除“ UP”状态外,其他所有应用程序都不会将流量发送到处于其他状态的应用程序。

application.yml

eureka:
  client:
    healthcheck:
      enabled: true
@Configuration
@ConditionalOnProperty(value = "eureka.client.healthcheck.enabled", matchIfMissing = false)
@ConditionalOnBean(HealthIndicator.class)
protected static class EurekaHealthCheckHandlerConfiguration {

	@Autowired(required = false)
	private HealthAggregator healthAggregator = new OrderedHealthAggregator();

	@Bean
	@ConditionalOnMissingBean(HealthCheckHandler.class)
	public EurekaHealthCheckHandler eurekaHealthCheckHandler() {
		return new EurekaHealthCheckHandler(this.healthAggregator);
	}
}

如果您需要对健康检查进行更多控制,则可以考虑实施自己的com.netflix.appinfo.HealthCheckHandler。

6.1扩展Actuator的health endPoints

https://docs.spring.io/spring-boot/docs/1.3.5.RELEASE/reference/html/production-ready.html

6.2eureka sidecar

https://cloud.spring.io/spring-cloud-static/Brixton.SR7/
https://cloud.spring.io/spring-cloud-static/Dalston.SR5/multi/multi__polyglot_support_with_sidecar.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值