分布式之Eureka

1、微服务架构的介绍

1.1 微服务的提出

在2014年马丁福勒提出了微服务架构的设计理念

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

1.2 微服务的概念

In short, the microservice architectural style [1] is an approach to developing a single application as a suite of small services, each running in its own process and communicating with lightweight mechanisms, often an HTTP resource API. These services are built around business capabilities and independently deployable by fully automated deployment machinery. There is a bare minimum of centralized management of these services, which may be written in different programming languages and use different data storage technologies.

简而言之,微服务体系结构样式[1]是一种将单个应用程序开发为一组小服务的方法,每个小服务都在自己的进程中运行并与轻量级机制(通常是HTTP资源API)进行通信。这些服务围绕业务功能构建,并且可以由全自动部署机制独立部署。这些服务的集中管理几乎没有,可以用不同的编程语言编写并使用不同的数据存储技术。

1、微服务是一个样式,一种设计风格而不是一种硬性规定

2、微服务是将一个单体项目拆分为多模块,拆分的颗粒度(一般推荐是以功能进行拆分)

3、每个模块都有属于自己的容器(tomcat等)

4、模块之间需要相互通信 REST、RPC 、MQ

5、每一个模块之间是独立的,没有依赖关系可以自由部署

6、可以使用不同的编程语言来配合开发以及不同的数据存储方式 mysql、redis等

2、SpringCloud的介绍
2.1 SpringCloud的百度百科

百度百科中:

Spring Cloud是一系列框架的有序集合。它利用[Spring Boot](https://baike.baidu.com/item/Spring Boot/20249767)的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等,都可以用Spring Boot的开发风格做到一键启动和部署。Spring Cloud并没有重复制造轮子,它只是将各家公司开发的比较成熟、经得起实际考验的服务框架组合起来,通过Spring Boot风格进行再封装屏蔽掉了复杂的配置和实现原理,最终给开发者留出了一套简单易懂、易部署和易维护的分布式系统开发工具包。

1、SpringCloud是基于SpringBoot的分布式开发的一系列框架集成

2、SpringCloud是集各个成熟的服务框架的组合,以SpringBoot的风格进行封装

3、SpringCloud就是微服务架构落地的技术栈

2.2 SpringCloud的技术点

中文技术网站

https://www.springcloud.cc/

1、Eureka 服务注册与发现中心

2、Ribbon 服务之间的负载均衡

3、Feign 服务发现与通信

4、Hystrix 服务降级和熔断机制 + DashBoard仪表盘

5、Zuul 网关路由(安全性、简便性)

6、Sleuth 服务追踪

7、config 服务的配置中心

8、Stream 实现MQ的使用

2.3 SpringCloud的版本与SpringBoot的版本约束

官方网址:https://spring.io/projects/spring-cloud

Release TrainBoot Version
Hoxton2.2.x
Greenwich2.1.x
Finchley2.0.x
Edgware1.5.x
Dalston1.5.x
3、Eureka使用
1、引入Eureka

假设服务A正在直接调用服务B,那么A和B之间的耦合度就很高,势必有如下的问题

1、当服务B的ip或者port发生改变的时候,服务A也必须去修改

2、当服务B搭建了集群,服务A也需要去维护多个ip和port

这种服务调用在微服务架构中是非常常见的一种现象,为了解决情况带来的维护困难,

就引入了Eureka服务注册与发现中心,服务B或者集群都是交付给Eureka管理,服务A

向Eureka调用服务B(通过的是服务名),这样就无需知道服务B的ip和port

在这里插入图片描述

总结: Eureka就是帮助我们管理服务,简化了服务之间的调用和后期的维护

2、Eureka-Server的使用

Eureka-server的官网:https://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/2.2.2.RELEASE/reference/html/#spring-cloud-eureka-server

2.1 导入依赖

spring-cloud-starter-netflix-eureka-server

2.2 yml配置
# 设置端口
server:
  port: 8761  
# 当前主机的实例名称
eureka:
  instance:
    hostname: localhost
  client:
  # 单机模式全部为false
    registerWithEureka: false
    fetchRegistry: false
    # 设置服务的注册路径
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
2.3 开启注解
@SpringBootApplication
@EnableEurekaServer
public class Application {
    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(true).run(args);
    }

}
2.4 测试启动

访问 http://locahost:8761/

在这里插入图片描述

3、Eureka-Client的使用
3.1 添加依赖

spring-cloud-starter-netflix-eureka-client

3.2 yml配置
# 指定端口
server:
  port: 8001
# 配置项目名
spring:
  application:
    name: # 当前项目就可以,起到唯一标识的作用
# 配置Eureka
eurake:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka  # 注册地址
3.3 开启注解
@SpringBootApplication
@EnableEurekaClient
public class Application {
    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(true).run(args);
    }

}
3.4 在Eureka-Server中查看

访问 http://locahost:8761/

在这里插入图片描述

4、Eureka-DiscoverClient的创建
4.1 导入依赖–相同
4.2 yml配置相同 端口不同
4.3 添加注解
@SpringBootApplication
@EnableDiscoveryClient
public class Demo02Application {

    public static void main(String[] args) {
        SpringApplication.run(Demo02Application.class, args);
    }
}
4.4 测试

在这里插入图片描述

5、实现Client之间的通信
5.1 demo01是服务提供
    @RequestMapping("/test")
    public String test(){
        return "来自demo01";
    }
5.2 demo02是服务调用
  • 通过RestTemplate来进行服务之间的通信
  • 通过EurekaClient来通过服务名获取连接(可以直接手动输入)
//配置RestTemplate
 @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
//调用服务
 @Resource
    private RestTemplate restTemplate;
    @Resource
    private EurekaClient eurekaClient;

    @RequestMapping("/test")
    public String test(){
        InstanceInfo info = eurekaClient.getNextServerFromEureka("demo01",false);
        String url = info.getHomePageUrl();
        String template = restTemplate.getForObject(url + "/test", String.class);
        return template;

    }
6、EurekaServer的安全性

当前情况下,可以无限制的情况下访问eureka-server的监控界面,并且获取到

ip地址和端口等信息是非常不安全的,所以为server提供了security配置。

6.1 导入依赖
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-security</artifactId>
</dependency>
6.2 配置springSecurity
spring:
  security:
    user:
      name: root
      password: 123456
6.3 忽略/eureka/**

官方:

You can secure your Eureka server simply by adding Spring Security to your server’s classpath via spring-boot-starter-security. By default when Spring Security is on the classpath it will require that a valid CSRF token be sent with every request to the app. Eureka clients will not generally possess a valid cross site request forgery (CSRF) token you will need to disable this requirement for the /eureka/** endpoints.

创建config配置:

//忽略路径下 /eureka/** 的CSRF认证
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().ignoringAntMatchers("/eureka/**");
        super.configure(http);
    }
}

//什么是CSRF? (百度百科)
/**
CSRF(Cross-site request forgery)跨站请求伪造,是一种挟制用户在当前已登录的Web应用程序上执行非本意的操作的攻击方式
实现原理:
欺骗用户的浏览器去访问一个自己曾经认证过的网站并运行一些操作。
简单的身份验证只能保证请求发自某个用户的浏览器,却不能保证请求本身是用户自愿发出的。
过程:
用户A访问页面1执行正常的操作
用户A在没有关闭浏览器的情况下访问页面2,页面2隐蔽的包含页面1的执行操作
此时浏览器已经获得认证,那么cookie中会存储认证信息
在访问页面2的时候就会正常访问,但此时并非用户A的自愿操作
/*
6.4 启动测试

在这里插入图片描述

6.5 EurekaClient修改

在eureka-server上开启了安全认证,client在进行注册的同时需要传递认证

eureka:
  client:
    service-url:
      defaultZone: http://root:123456@localhost:8761/eureka
7、Eureka的高可用性

当程序在运行过程中,Eureka突然宕机,分为两种情况

1、当消费者已经调用过了服务,那么会通过缓存直接调用服务

2、当消费者还没有调用过服务,无法通过eureka获取服务

为了解决Eureka的宕机问题,通过配置Eureka的集群

7.1 搭建Eureka服务

创建一个相同的Eureka服务

7.2 配置yml
# 在8762端口
eureka:
  client:      
    fetch-registry: true   # 从Eureka中获取注册信息
    register-with-eureka: true  # 注册到Eureka,同步信息  
    service-url:
      defaultZone: http://localhost:8761/eureka
# 在8761端口
eureka:
  client:
    fetch-registry: true
    register-with-eureka: true
    service-url:
      defaultZone: http://localhost:8762/eureka
7.3 配置EurekaClient
eureka:
  client:
    service-url:
      defaultZone: http://root:123456@localhost:8761/eureka,http://root:123456@localhost:8762/eureka
8、Eureka的总结

1、当EurekaClient在启动时,会将自己的信息注册进EurekaServer

2、当EurekaClient在调用服务时,如果已经有缓存的情况下,就不需要从

EurekaServer中获取注册信息了,直接调用即可。

3、EurekaClient会通过心跳与EurekaServer进行连接

Being an instance also involves a periodic heartbeat to the registry (through the client’s serviceUrl) with a default duration of 30 seconds. A service is not available for discovery by clients until the instance, the server, and the client all have the same metadata in their local cache (so it could take 3 heartbeats). You can change the period by setting eureka.instance.leaseRenewalIntervalInSeconds. Setting it to a value of less than 30 speeds up the process of getting clients connected to other services. In production, it is probably better to stick with the default, because of internal computations in the server that make assumptions about the lease renewal period.

默认的是30s进行一次心跳请求,当超过90s的时候还没有发送心跳请求

EurekaServer就会认为你已经宕机,将你从注册表中删除

4、EurekaClient每30s会与EurekaServer进行注册表的更新

eureka:  
  client: 
    service-url: 
      registry-fetch-interval-seconds: 30

5、EurekaServer的自我保护机制

eureka:
  server:  enable-self-preservation: true

Eureka会统计在15分钟内,服务的心跳比例,如果小于85%,那么Eureka就会开启自我保护机制

此时EurekaServer仍然是可以用的

但是不会从EurekaServer中去消除那些未达标的注册表

4、EurekaClient每30s会与EurekaServer进行注册表的更新

eureka:  
  client: 
    service-url: 
      registry-fetch-interval-seconds: 30

5、EurekaServer的自我保护机制

eureka:
  server:  enable-self-preservation: true

Eureka会统计在15分钟内,服务的心跳比例,如果小于85%,那么Eureka就会开启自我保护机制

此时EurekaServer仍然是可以用的

但是不会从EurekaServer中去消除那些未达标的注册表

当稳定(比如网络的问题)之后,EurekaServer的注册信息才会进行一个同步处理

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值