SpringCloud classic interview questions

1. Spring Cloud and Dubbo

SpringCloud and Dubbo are now mainstream micro-service architecture
SpringCloud micro-service solutions under the Apache's Spring system
Dubbo is distributed service governance framework of Ali
from the technical dimension, in fact, far more than SpringCloud Dubbo, Dubbo itself is only Realized service governance, and SpringCloud now has 21 sub-projects and there will be more
in the future.So in fact, many people will say that Dubbo and SpringCloud are unfair,
but due to RPC and registry metadata and other reasons, we only We can choose one of the two, so we often use them to compare the
service call method. Dubbo uses RPC remote call, while Spring Cloud uses Rest API. In fact, it is more in line with the official microservice definition
service registry. , Dubbo uses a third-party ZooKeeper as its underlying registry to realize service registration and discovery. Spring Cloud uses Spring Cloud Netflix Eureka to implement the registry. Of course, Spring Cloud can also be implemented using ZooKeeper, but generally we don’t do this as a
service gateway. Dubbo does not have its own implementation, it can only be integrated through other third-party technologies, and SpringCloud has Zuul routing gateway, as a routing server, for consumer request distribution, SpringCloud also supports circuit breakers, and git perfectly integrates distributed configuration file support Version control, transaction bus to achieve configuration file update and service automatic assembly, etc. a series of microservice architecture elements

2. Technical selection

At present, the main domestic distributed system selection is Dubbo, after all, domestically, and the technical proficiency of domestic engineers is high, and the defects of Dubbo in other dimensions can be compensated by the integration of other third-party frameworks.
SpringCloud is currently more popular abroad, of course I think the domestic market will gradually be biased towards Spring Cloud. Even Liu Jun, as the person in charge of Dubbo's restart, has also expressed his views. Dubbo's development direction is to actively adapt to the Spring Cloud ecology, not a conflict.

3. Comparison of Rest and RPC

In fact, if you carefully read the paper by Martin Fowler, the author of microservices, you can find that the communication mechanism between services defined is Http Rest
RPC. The main defect is that the dependency between the service provider and the calling method is too strong. Microservices define interfaces and publish them through continuous inheritance. Strict version control is required to avoid conflicts between service provision and invocation due to different versions
. REST is a lightweight interface, and service provision and invocation are not There is coupling between codes, which is only regulated through an agreement, but there may also be service integration problems caused by inconsistencies in documents and interfaces, but it can be integrated through swagger tools, which is an integrated solution of code and documentation, so REST is in a distributed environment
Downstream is more flexible than RPC. This is why Dangdang’s DubboX has added REST support to the enhancements to Dubbo.

4. Document quality and community activity

The SpringCloud community is much more active than Dubbo. After all, due to Liang Fei’s team, Dubbo stopped updating and iterating for five years, and small and medium-sized companies could not afford the cost of technology development. This led to a serious decline in the Dubbo community, and SpringCloud suddenly emerged and quickly occupied the microservices. The market, backed by the spring mixed wind and water,
Dubbo has been quite mature after years of accumulated documents, and there is a stable status quo for the microservice architecture system of various companies.

Five. SpringBoot and SpringCloud

SpringBoot is a Maven-based solution launched by Spring to solve the redundancy of traditional framework configuration files and complicated assembly components. It aims to quickly build a single
microservice, while SpringCloud focuses on solving the coordination and configuration between various microservices. Communication, fuse, load balancing, etc., the technical dimensions are the same, and Spring Cloud relies on Spring Boot, and Spring Boot does not rely on Spring Cloud, and can even perform excellent integrated development with Dubbo

to sum up:

SpringBoot专注于快速方便的开发单个个体的微服务
SpringCloud是关注全局的微服务协调整理治理框架,整合并管理各个微服务,为各个微服务之间提供,
配置管理,服务发现,断路器,路由,事件总线等集成服务
SpringBoot不依赖于SpringCloud,SpringCloud依赖于SpringBoot,属于依赖关系
SpringBoot专注于快速,方便的开发单个的微服务个体,SpringCloud关注全局的服务治理框架

6. Both Eureka and ZooKeeper can provide service registration and discovery functions, please tell us the difference between the two

1.ZooKeeper guarantees CP, Eureka guarantees AP

ZooKeeper registration service is paralyzed during the election. Although the service will eventually recover, the
Eureka nodes that are not available during the election are equal. As long as there is an Eureka, the service can be guaranteed to be available, and the data queried is not the latest

Self-protection mechanisms can lead to:

Eureka不再从注册列表移除因长时间没收到心跳而应该过期的服务
Eureka仍然能够接受新服务的注册和查询请求,但是不会被同步到其他节点(高可用)
当网络稳定时,当前实例新的注册信息会被同步到其他节点中(最终一致性)

Eureka can handle the situation where some nodes lose contact due to network failures, without paralyzing the entire registration system like ZooKeeper

2. ZooKeeper has the roles of Leader and Follower, and Eureka's nodes are equal
3. ZooKeeper adopts the principle of majority survival, and Eureka uses a self-protection mechanism to solve the partition problem
4. Eureka is essentially a project, and ZooKeeper is just a process

7. How to communicate independently between microservices

Microservice communication mechanism
Each microservice in the system can be deployed independently, and each microservice is loosely coupled. Each microservice only focuses on completing one task and completing that task well.
Organize services around business capabilities, automated deployment, intelligent endpoints, and decentralized control of language and data.

将组件定义为可被独立替换和升级的软件单元。
以业务能力为出发点组织服务的策略。
倡导谁开发,谁运营的开发运维一体化方法。
RESTful HTTP协议是微服务架构中最常用的通讯机制。
每个微服务可以考虑选用最佳工具完成(如不同的编程语言)。
允许不同微服务采用不同的数据持久化技术。
微服务非常重视建立架构及业务相关指标的实时监控和日志机制,必须考虑每个服务的失败容错机制。
注重快速更新,因此系统会随时间不断变化及演进。可替代性模块化设计。

Microservice communication method:

同步:RPC,REST等

异步:消息队列。要考虑消息可靠传输、高性能,以及编程模型的变化等。

How to select the message queue middleware

    1.协议:AMQP、STOMP、MQTT、私有协议等。
    2.消息是否需要持久化。
    3.吞吐量。
    4.高可用支持,是否单点。
    5.分布式扩展能力。
    6.消息堆积能力和重放能力。
    7.开发便捷,易于维护。
    8.社区成熟度。

RabbitMQ is a message queue middleware that implements the AMQP (Advanced Message Queuing Protocol) protocol. RabbitMQ supports at most once and at least once. The service architecture of NetEase's cellular platform uses RabbitMQ to realize communication between services.

8. What is a service circuit breaker? What is a service degradation

   在复杂的分布式系统中,微服务之间的相互调用,有可能出现各种各样的原因导致服务的阻塞,在高并发场景下,服务的
   阻塞意味着线程的阻塞,导致当前线程不可用,服务器的线程全部阻塞,导致服务器崩溃,由于服务之间的调用关系是同
   步的,会对整个微服务系统造成服务雪崩,为了解决某个微服务的调用响应时间过长或者不可用进而占用越来越多的
   系统资源引起雪崩效应就需要进行服务熔断和服务降级处理。
  

  所谓的服务熔断指的是某个服务故障或异常一起类似显示世界中的“保险丝"当某个异常条件被触发就直接
  熔断整个服务,而不是一直等到此服务超时。

  服务熔断就是相当于我们电闸的保险丝,一旦发生服务雪崩的,就会熔断整个服务,通过维护一个自己的线程池,
  当线程达到阈值的时候就启动服务降级,如果其他请求继续访问就直接返回fallback的默认值。

Nine. What are the advantages and disadvantages of microservices? Tell me about the pits you encountered in project development

**优点**
    每一个服务足够内聚,代码容易理解
    开发效率提高,一个服务只做一件事
    微服务能够被小团队单独开发
    微服务是松耦合的,是有功能意义的服务
    可以用不同的语言开发,面向接口编程
    易于与第三方集成
    微服务只是业务逻辑的代码,不会和HTML,CSS或者其他界面组合
        开发中,两种开发模式
            前后端分离
            全栈工程师
    可以灵活搭配,连接公共库/连接独立库
    
**缺点**
    分布式系统的负责性
    多服务运维难度,随着服务的增加,运维的压力也在增大
    系统部署依赖
    服务间通信成本
    数据一致性
    系统集成测试
    性能监控

10. What are the microservice technology stacks you know? Please list one or two

A collection of multiple technologies. If
we are discussing a distributed microservice architecture, what dimensions are needed

维度(SpringCloud)
    服务开发
        SpringBoot
        Spring
        SpringMVC
    服务配置与管理
        Netfilx公司的Archaiusm,阿里的Diamond
    服务注册与发现
        Eureka,ZooKeeper
    服务调用
        Rest,RPC,gRPC
    服务熔断器
        Hystrix
    服务负载均衡
        Ribbon,Nginx
    服务接口调用
        Feign
    消息队列
        Kafka,RabbitMq,ActiveMq
    服务配置中心管理
        SpringCloudConfing
    服务路由(API网关)
        Zuul
    事件消息总线
        SpringCloud Bus

11. Eureka self-protection mechanism

Based on the three projects established in the previous article (eureka-server, uerreg, myweb), Eureka is self-protected by default. Let's do a test. Let's start three projects first, we visit the registration center http://localhost:8761/,

As you can see, the instance is successfully registered to the center. At this point we close the uerreg service, refresh the registration center, we will find the following interface

In addition to seeing a line of red warning messages, we also discovered a magical thing. Although our service instance was killed, it still exists in the service registry. This is Eureka's self-protection mechanism, he will not remove the service that has been hung up, he will think that the service is trying to reconnect.
We definitely do not want this during the development process, which is not conducive to development. We can turn off Eureka's self-protection mechanism (it is not recommended to turn off the actual production environment).

eureka-server服务端
配置文件中我们添加如下配置

#关闭保护机制,以确保注册中心将不可用的实例正确剔除
eureka.server.enable-self-preservation=false
#(代表是5秒,单位是毫秒,清理失效服务的间隔 )
eureka.server.eviction-interval-timer-in-ms=5000

userreg客户端
配置文件中我们添加如下配置

# 心跳检测检测与续约时间
# 测试时将值设置设置小些,保证服务关闭后注册中心能及时踢出服务
# 配置说明
#  lease-renewal-interval-in-seconds 每间隔10s,向服务端发送一次心跳,证明自己依然”存活“
#  lease-expiration-duration-in-seconds  告诉服务端,如果我20s之内没有给你发心跳,就代表我“死”了,
将我踢出掉。
eureka.instance.lease-renewal-interval-in-seconds=10
eureka.instance.lease-expiration-duration-in-seconds=20

We restart the service, and then close the userreg client for testing.

At this time, we found that the red warning became a warning that self-protection was turned off, and the instance was removed by the registry, indicating that the self-protection mechanism was turned off at this time.

12. Eureka health check

What are the actual application scenarios of health check? For example, we configure the userreg project data source
and introduce the following dependencies in the pom file

...
 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
...

Then create a configuration class, configure the data source DataSource

@Configuration
public class Myconfig {
   @Bean
   public DataSource dataSource()
   {
       org.apache.tomcat.jdbc.pool.DataSource dataSource=new org.apache.tomcat.jdbc.pool.DataSource();
       dataSource.setUrl("jdbc:mysql://localhost/test?characterEncoding=UTF-8");
       dataSource.setUsername("root");
       dataSource.setPassword("mysql");
       dataSource.setDriverClassName("com.mysql.jdbc.Driver");
       return dataSource;
   }
}

This has been learned in springboot, and I will log the springboot learning process in a blog way. After configuring the data source, we start the service and visit http://localhost:9001/admin/health to check the health status

We can see the health of db. What if our mysql service is down at this time?

We manually stop the mysql service, and then look at the health

Original link: https://blog.csdn.net/jerryDzan/article/details/89137818

.
.
.
Afterword: After I found out that my white shoes were dirty, my partner would brush me clean, so I decided to buy only white shoes from now on.

Guess you like

Origin blog.csdn.net/weixin_43945983/article/details/106261752