0%

服务心跳流程分析

eureka client每隔一定的时间,会给eureka server发送心跳,保持心跳,让eureka server认为自己还活着。

心跳在代码里,叫做续约。

  1. 还是在DiscoveryClient初始化的时候,有一个心跳的定时任务,由HeartbeatThread执行。

  2. 默认值是每隔30秒去发送一个心跳。DEFAULT_LEASE_RENEWAL_INTERVAL

  3. 接下来是用jersy去给eureka server发送心跳的http请求。

    阅读全文 »

eureka client全量抓取注册表

eureka client第一次启动的时候,会从eureka server端抓取全量的注册表,在本地进行缓存。后续每隔30秒从eureka server端抓取增量的注册表信息,和本地缓存进行合并。

先找到第一次抓取全量注册表的源码,没记错的话应该是在创建DiscoveryClient的构造方法里。就是下面这几行代码:

1
2
3
if (clientConfig.shouldFetchRegistry() && !fetchRegistry(false)) {
fetchRegistryFromBackup();
}
阅读全文 »

eureka client启动流程

上一篇文章,我们分析了eureka server的启动流程,这一篇来分析一下eureka client的启动流程。我们先要找到启动入口在哪里。在eureka-examples里,有一个ExampleEurekaClient的测试类。要执行这个类,首先需要复制一段代码,设置一些基础属性,这是从eureka-server的单元测试里复制过来的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* This will be read by server internal discovery client. We need to salience it.
*/
private static void injectEurekaConfiguration() throws UnknownHostException {
String myHostName = InetAddress.getLocalHost().getHostName();
String myServiceUrl = "http://" + myHostName + ":8080/v2/";

System.setProperty("eureka.region", "default");
System.setProperty("eureka.name", "eureka");
System.setProperty("eureka.vipAddress", "eureka.mydomain.net");
System.setProperty("eureka.port", "8080");
System.setProperty("eureka.preferSameZone", "false");
System.setProperty("eureka.shouldUseDns", "false");
System.setProperty("eureka.shouldFetchRegistry", "false");
System.setProperty("eureka.serviceUrl.defaultZone", myServiceUrl);
System.setProperty("eureka.serviceUrl.default.defaultZone", myServiceUrl);
System.setProperty("eureka.awsAccessId", "fake_aws_access_id");
System.setProperty("eureka.awsSecretKey", "fake_aws_secret_key");
System.setProperty("eureka.numberRegistrySyncRetries", "0");
}
阅读全文 »

如何阅读一个开源框架的源码

如果要阅读一个开源框架的源码,我们应该从什么地方入手。首先应该想到的是,从框架启动的入口入手,比如Eureka,肯定是从Eureka服务本身启动的入口开始。或者我们也可以从框架的单元测试开始看,因为单元测试都包含了框架的核心流程和功能。所以我们通过这2个入口,可以通过打断点执行的方式阅读代码的逻辑。下面我将开始学习Eureka的源码,我将基于https://github.com/Netflix/eureka.git 的v1.7.2分支进行阅读,因为我目前所用的SpringCloud引入的就是这个版本。

阅读全文 »

SpringBoot日志处理

默认情况下,SpringBoot会引入spring-boot-starter-logging,也就是logback的日志实现。

在resources下新建logback-spring.xml可以自定义日志配置,这是约定的文件名字。

在spring-boot-starter-logging-2.1.4.RELEASE这个依赖中,在/org/springframework/boot/logging/logback/base.xml位置可以找到SpringBoot提供的一个默认配置,基于这个配置进行自定义

1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
<include resource="org/springframework/boot/logging/logback/base.xml" />
<logger name="com.test.springlecture" level="DEBUG"/>
<springProfile name="default">
<logger name="com.test.springlecture" level="INFO"/>
</springProfile>
</Configuration>
阅读全文 »

SpringApplication

Class that can be used to bootstrap and launch a Spring application from a Java main method. By default class will perform the following steps to bootstrap your application:

  • Create an appropriate ApplicationContext instance (depending on your classpath)
  • Register a CommandLinePropertySource to expose command line arguments as Spring properties
  • Refresh the application context, loading all singleton beans
  • Trigger any CommandLineRunner beans
阅读全文 »

微服务学习笔记

什么是微服务

https://martinfowler.com/articles/microservices.html

“Microservices” - yet another new term on the crowded streets of software architecture. Although our natural inclination is to pass such things by with a contemptuous glance, this bit of terminology describes a style of software systems that we are finding more and more appealing. We’ve seen many projects use this style in the last few years, and results so far have been positive, so much so that for many of our colleagues this is becoming the default style for building enterprise applications. Sadly, however, there’s not much information that outlines what the microservice style is and how to do it.

阅读全文 »