spring-cloud 源码 使用(一)

spring-cloud 基本用法

1 分布式微服务架构

spring-cloud 是一个分布式微服务架构,类似这样的架构还有阿里的dubbo。在分析eureka之前,先了解一下分布式微服务架构。大型系统架构中,会拆分多个子系统。简单来说,这些子系统有两个功能:提供接口、调用接口,在微服务架构中,将每一个这样的子系统称为一个“微服务”;每一个服务会部署多个实例(就是多台机器,且会动态扩容,IP不固定);这种情况下,应需要对服务进行管理,那么就需要似类eureka这样的注册中心对服务进行管理。
《spring-cloud 源码 使用(一)》

2 注册中心服务 的依赖和配置信息

erureka 依赖

    <parent>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-parent</artifactId>
        <version>Dalston.SR1</version>
        <relativePath />
    </parent>
    <groupId>com.dongnaoedu.springcloud</groupId>
    <artifactId>eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <!-- spring-boot-starter-actuator 管理工具/web 查看堆栈,动态刷新配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- cloud eureka组件 注册中心(已集成了web容器,eureka-client) -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <!-- 日志格式化组件 -->
        <dependency>
            <groupId>net.logstash.logback</groupId>
            <artifactId>logstash-logback-encoder</artifactId>
            <version>4.11</version>
        </dependency>
    </dependencies>

eureka 配置

# 上下文初始化加载
info:
  name: Eureka server
  contact: 注册中心

management:
  security:
    enabled: false

spring:
  profiles:
    active: dev

#profile数组 ---
---
spring:
  profiles: dev
server:
  port: 8761
eureka:
  #作为客户端(相当于一个普通服务端),用于注册到注册中心的客户端
  client:
    # 是否注册到eurekaserver
    registerWithEureka: false
    # 是否拉注册取信息
    fetchRegistry: false
    # eureka server地址
    serviceUrl:
      # 指定连接到的注册中心
      defaultZone: http://127.0.0.1:8761/eureka/

  #这里的服务指的是作为注册中心服务,用于接收其它普通服务注册进来的
  #上面的client 就是用于作为一个普通服务连接注册中的客户端
  server:
    waitTimeInMsWhenSyncEmpty: 0
    # false 关闭自我保护,不管如何都要剔除心跳检测异常的服务
    enableSelfPreservation: false
    #服务踢除时间(默认60000ms)
    eviction-interval-timer-in-ms: 60000
    #eureka 集群刷新间隔
    peer-eureka-nodes-update-interval-ms: 10000

  #当前服务实例信息(这里相于看作一个普通服务)
  instance:
    hostname: eureka1
    #实例心跳时间(默认30s)
    lease-renewal-interval-in-seconds: 30
    #超过上次心跳30s 认为宕机(默认90s)
    lease-expiration-duration-in-seconds: 30
---
spring:
  profiles: prod2
server:
  port: 8763
eureka:
  client:
    registerWithEureka: true
    fetchRegistry: true
    serviceUrl:
      defaultZone: ${eureka_serviceUrl:http://127.0.0.1:8761/eureka/}
  server:
    waitTimeInMsWhenSyncEmpty: 0
    # false 关闭自我保护,不管如何都要剔除心跳检测异常的服务
    enableSelfPreservation: false
    # 服务剔除任务执行周期
    evictionIntervalTimerInMs: 60000
    # 服务端缓存最长时间
    responseCacheUpdateIntervalMs: 30000
  instance:
    hostname: eureka2

普通服务配置信息

server:
  port: 9004

eureka:
  #配置连接eureka注册中心的客户端,用于注册当前服务,拉取服务列表,发布服务心跳请求到eureka注册中心
  client:
    # 是否
    registerWithEureka: true
    fetchRegistry: true
    # 服务列表缓存更新时间
    registryFetchIntervalSeconds: 10
    serviceUrl:
      defaultZone: ${eureka_serviceUrl:http://127.0.0.1:8761/eureka/}
  #普通服务实例信息配置
  instance:
    # 心跳间隔
    leaseRenewalIntervalInSeconds: 10
    hostname: ${HOSTNAME:localhost}

spring:
  application:
    name: uaa-interface
  cloud:
    client:
      hostname: ${HOSTNAME:localhost}

    #访问配置中心的配置信息
    config:
      discovery:
        # 使用eureka发现配置中心服务
        enabled: true
        # 配置中心服务名称
        serviceId: config-server
      # 登录用户名和密码
      username: ${config_server_security_name:tony}
      password: ${configserver_security_password:12345678}
      # 覆盖本地配置
      overrideNone: false
      failFast: true

启动代码

@SpringBootApplication
@EnableEurekaServer //开启eureka服务
public class EurekaApp {
    final static Logger logger = LoggerFactory.getLogger(EurekaApp.class);
    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder(EurekaApp.class).web(true)
                .run(args);
        logger.debug("eureka已经启动,当前host:{}", applicationContext.getEnvironment().getProperty("HOSTNAME"));
    }
}

启动结页面信息
《spring-cloud 源码 使用(一)》

3 普通服务 的依赖配置信息

这里用一个服务中心demo来分析
依赖

    <dependencies>
        <dependency>
            <groupId>com.dongnaoedu.springcloud</groupId>
            <artifactId>uaa-common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.xie.java</groupId>
            <artifactId>feign-test-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--用于连接到eureka注册中的组件-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <!-- spring-boot-starter-actuator 管理工具/web 查看堆栈,动态刷新配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- 日志格式化组件 -->
       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!-- spring cloud http调用封装工具 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
        <!-- 引入熔断组件。 不然feign注解上使用fallback=** 会不起作用 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>com.xie.java</groupId>
            <artifactId>feign-test-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

普通服务启动代码

@SpringBootApplication
@EnableEurekaClient //启用eureka客户端,用于注册到eureka注册中心,及拉取服务列表信息
@EnableFeignClients(basePackages = {"com.xie.java.api.service"},defaultConfiguration=FeignClientsConfiguration.class)

public class UAAApplication {
    final static Logger logger = LoggerFactory.getLogger(UAAApplication.class);

    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder(UAAApplication.class).web(true)
                .run(args);
        logger.debug(applicationContext.getId() + "已经启动,当前host:{}",
                applicationContext.getEnvironment().getProperty("HOSTNAME"));
    }
}

《spring-cloud 源码 使用(一)》

4 原理分析前 疑问

基本服务起来了,接下来可以用原理分析了;为了好更好的去分析,分析前先看下几个问题:
1. 服务提供者怎么注册到服务中心的?
2. 注册中心怎么接收注册请求?
3. 注册中心如何存储服务信息?
4. 注册中心高可用机制是什么?
5. Eureka集群信息同步机制?
6. 注册中心剔除服务的机制什么?
7. 服务消费者如何获取服务信息?
每当我们分析源码里,通常都会遇到怎么分析,代码入口在哪里?首选spring-cloud 基于spring-boot的配置,而spring-boot又是基于spring的自动配置;回顾之前对spring-boot及spring分析,大概应该有一定的思路了;那么还是先看一下eureka的依赖信息,然后对上面的疑问逐个分析;
《spring-cloud 源码 使用(一)》
从上面的截图可以看到eureka两关键块模spring-cloud-netflex-core
及spring-cloud-netflix-eureka-clkient,那么就先从这两模块入手;
开启自动配置
《spring-cloud 源码 使用(一)》
开启eureka服务端注解

@EnableDiscoveryClient
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(EurekaServerMarkerConfiguration.class)
public @interface EnableEurekaServer {

}

据上面信息大概知道怎么启动的了,实质的源码分析留在下编去分析

    原文作者:Spring Boot
    原文地址: https://blog.csdn.net/yangyangiud/article/details/79864531
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞