java interview questions (microservices)

 Spring Cloud

What is spring Cloud? Advantages and disadvantages

SpringCloud is an ordered collection of a series of frameworks; it uses the development convenience of Spring Boot to subtly simplify the development of distributed system infrastructure; springCloud does not repeat the wheel, but leaves developers with the combination of more mature frameworks. A set of distributed system development kits that are easy to understand, easy to deploy, and easy to maintain;

advantage:

  1. Low coupling between components does not affect the development of other modules

  1. Reduce team development costs

  1. Simple configuration

  1. Cross-platform, can be developed in multiple languages

  1. Rich components and complete functions

shortcoming:

  1. Too many microservices, high governance costs, not conducive to system maintenance

  1. Deployment, performance monitoring, and system integration testing are cumbersome

Why do you need spring Cloud?

springcloud is based on the simplified configuration of springboot, and the convention is greater than the elegance and simplicity of the configuration; as the application of the monolithic structure increases with the complexity of the system, various problems will be exposed, and the microservice architecture gradually replaces the monolithic architecture, and this trend will become more and more popular

The difference between springCloud and springboot?
  1. springboot focuses on the rapid and convenient development of individual microservices; springCloud focuses on the global microservice coordination and governance framework, which integrates and manages individual microservices developed by SpringBoot;

  1. springboot can use development projects independently without SpringCloud, but SpringCloud cannot do without SpringBoot, which is a dependency relationship

  1. Springboot focuses on the rapid and convenient development of individual microservices, while SpringCloud focuses on the global service governance framework

What problems are faced when using Spring Boot to develop distributed microservices?
  1. Complexities associated with distributed systems include network issues, latency overhead, bandwidth issues, security issues

  1. Redundancy - Redundancy issues in distributed systems

  1. Load Balancing -- Load balancing improves the distribution of workloads across multiple computing resources, such as computers, computer clusters, network links, central processing units, or disk drives

  1. Performance-Issues Performance issues due to various operational overheads

  1. deployment complexity

The difference between REST and RPC

rest: is an architectural style, referring to a set of architectural constraints and principles (CRUD)

rpc: It is an inter-process communication method. Allows calling remote services as if they were local services

  1. rpc is suitable for intranet service calls, and rest is suitable for providing external services

  1. rpc is suitable for IO-intensive services, and rest is suitable for low-frequency services

  1. The communication protocol of rpc generally uses tcp, and the communication protocol of rest uses http

The difference between Spring Cloud and dubbo
  1. SpringCloud is a microservice framework, and dubbo is an RPC call framework;

  1. SpringCloud is large and comprehensive, and dubbo focuses on service calls;

  1. Dubbo service call performance is higher than SpringCloud

  1. SpringCloud and dubbo can be used together

The relationship and difference between distributed and microservices

Distributed belongs to microservices; microservices are not necessarily distributed;

the difference:

1. The microservice architectural style is a method of developing a single application as a set of small services, each of which runs in its own process; a distributed system is a collection of several independent computers

2. Microservices are things at the design level. Generally, how to split the system logically, that is, vertical splitting; distributed is something at the deployment level, that is, emphasizing the composition of the physical level, that is, the deployment of each subsystem of the system on a different computer

3. Microservices solve the problem of system complexity: generally speaking, it is a business problem; distributed solves the problem of system performance: that is, it solves the problem of a single point of system deployment

4. Microservice applications can be deployed on the same server, not necessarily scattered on multiple servers; distributed is to divide a large system into multiple business modules, and these business modules will be deployed on different machines

5. Compared with distributed services, microservices have smaller granularity and lower coupling between services. Since each microservice is in charge of an independent small team, it has higher agility. Distributed services are finally Will evolve to microservice architecture

Talk about your understanding of microservices

Microservice is an architectural concept designed to reduce system coupling and provide more flexible service support by decomposing functions into discrete services

Eureka (registration center)

  1. What is Eureka?

Eureka serves as the service registration function server of Spring Cloud. He is the service registration center. Other services in the system use Eureka's client to connect it to Eureka Service and keep the heartbeat, so that the staff can monitor whether each microservice is running through EurekaService normal

  1. How Eureka achieves high availability

Use the cluster to register multiple Eurekas, and then register SpringCloud services with each other. When the client obtains information from Eureka, it accesses them in the order of Eureka

  1. Eureka self-protection mechanism

By default, if Eureka Service does not receive the heartbeat of a microservice within a certain period of time, Eureka Service will enter the self-protection mode. In this mode, Eureka Service will protect the information in the service registry and will not delete the information in the registry. Data, when the network failure is restored, the Eureka Servic node will automatically exit the self-protection mode.

  1. What is Service Renewal

Eureka Client will send a heartbeat every 30 seconds to renew the contract. Inform Eureka Server by renewing the contract that the Eureka Client is running normally and there is no problem. By default, if Eureka Server does not receive a renewal from Eureka Client within 90 seconds, the Server will delete the instance from its registry

  1. Culling service

The provider does not necessarily go offline normally. It may be due to memory overflow, network failure and other reasons that the service cannot work normally. EurekaServer will remove these invalid services from the service list. So it will start a timed task. Every 60 seconds The invalid service will be eliminated once

6. Eureka defects

Since the synchronous replication between clusters is carried out through HTTP, based on the unreliability of the network, the registry information between the Eureka Servers in the cluster will inevitably have out-of-sync time nodes, which does not meet the C (data consistency) in CAP

Ribbon (load balancing)

  1. What is Ribbon

The Ribbon client component provides a series of comprehensive configuration items, such as connection timeout, retry, etc. Simply put, it is to list all the following machines in the configuration file, and Ribbon will automatically help you connect to these machines based on certain rules (such as simple polling, random connection, etc.). We can also easily use Ribbon to implement a custom load balancing algorithm; the main function is to provide the client's software load balancing algorithm

2. The meaning of load balancing

The meaning of load balancing refers to balancing the load tasks and distributing them to multiple operating units for operation. It is mainly used to avoid application downtime caused by a single application due to concurrency and other reasons, making the system as a whole unusable and multiple loads working at the same time. , then using load balancing can solve high concurrency problems and achieve high availability of services

3. The principle of Ribbon

Ribbon uses discoveryClient to read the target service information from the registration center, counts the same interface requests, uses the % remainder algorithm to obtain the target service cluster index, and returns the obtained target service information. The role of @LoadBalanced annotation to enable client load balancing

4. How does Ribbon choose which instance to call
  1. Get the service list: In order to reduce the delay of the service, the client will filter the service list through the zone specified by eureka

  1. Load balancing: Obtain one of them from the service list through the load balancing strategy, the default is the polling strategy, and then call the server

5. How does Ribbon avoid calling invalid instances
  1. Detect instances through IPing, if it is detected that a service instance does not exist/does not respond for a certain period of time, it will be removed from the holding service list in time

  1. Keep the statistics of the zone, and the ribbon can avoid access to the invalid zone

6. What load balancing strategies does Ribbon have?
  1. Random strategy: choose randomly

  1. Polling strategy: select in order

  1. Retry strategy: within the configuration time, the current selection is unsuccessful, keep trying

  1. Minimum concurrency strategy: examine each one and choose the one with the lowest concurrency

  1. Available filtering strategies: filter out failed and high concurrency

  1. Response time weighting strategy: assign weights according to the response time, the longer the time, the lower the weight

  1. Region Weighting Strategy

7. The difference between Ribbon and Nginx

Ribbon belongs to the client load, and before calling the server, the client chooses to call the service according to the policy

Nginx belongs to the server load, the service only calls the load address, that is, the load will choose the server to call

Hystrix (fault tolerance protection)

1. What is Hystrix? What is the realization principle?

hystrix is ​​a latency and fault tolerance library designed to isolate access points to remote systems, services, and third-party libraries, stop cascading failures, and recover quickly after failures.

Implementation principle:

Normally, the circuit breaker is closed and the service consumer requests the microservice normally

Over a period of time, when the failure rate reaches a certain threshold, the circuit breaker will open, but a fail-fast approach

After the circuit breaker is opened for a period of time, it automatically enters the half-open state, allowing a request to pass through, if the circuit breaker is successfully closed, otherwise it continues to open

Circuit breakers ensure that local errors do not spread to the entire system

2. What is the function of Hystrix?

resource isolation

Limiting

fuse

downgrade

Operation and maintenance monitoring

3. Hystrix design principles
  1. Prevent any one dependent service from consuming all resources

  1. Avoid request queuing and backlogs

  1. Provide a fallback downgrade mechanism to deal with failures

  1. Use resource isolation techniques to limit the impact of failure of any one dependent service

  1. Improve the speed of fault discovery through near real-time statistics/monitoring/alarm functions

  1. Accelerate fault handling and recovery with near real-time attribute and configuration hot modification

  1. Protects against all failure conditions of dependent service calls, not just network failure conditions

4. What is the avalanche effect? cause? The way to prevent avalanches?

Avalanche effect: The calls between microservices of large-scale projects are interoperable. When a certain service goes down, other services that call this service will also go down, which will gradually expand the unavailability of the service to all other service, causing the service of the entire project to crash

cause:

  1. There is a bug in the code of a single service

  1. A surge in request visits

  1. Server hardware failure

Prevention method:

Service degradation: if the interface call fails, call the local method and return an empty

Service fusing: If the interface call fails, it will enter a fusing method defined in advance by the calling interface and return an error message

Service isolation: Interaction between isolated services

Service monitoring: when the service is invoked, the running indicators such as the number of requests per second and the number of successful requests will be recorded

5. Service downgrade, service fuse, service isolation

Service degradation: prevent the client from waiting all the time, will not process the business logic code, and directly return a friendly prompt to the client

Service fusing: request to go directly to the fallback method, and try to recover after the set time

Service isolation: Open an independent thread pool for isolated services, so that other services will not be affected under high concurrency

Feign (service call)

1. What is Feign? what's the effect?

Feign is an http client, just need to create an interface and add an annotation to help us call HTTP API more conveniently

2. Feign principle
  1. Scan the proxy class of the feign interface into the Spring container

  1. Create a RequestTemplate for the method of the interface

  1. make a request

3. How to deal with Feign timeout

The bottom layer has a built-in Ribbon framework, and uses the Ribbon's request connection timeout and request processing timeout as its timeout time, and the Ribbon's default request connection timeout and request processing timeout are both 1s;

Modify the timeout period:

  1. Passively modify Feign's timeout by modifying Ribbon's timeout (ribbon.ConnectTimeout, ribbon.ReadTimeout)

  1. Directly modify Feign's timeout (feign.client.config.default.ConnectTimeout, feign.client.config.default..ReadTimeout)

4. Feign parameter passing method
  1. Restful parameter passing @PathVariable() splices the parameter id into the path in a restful style

  1. ? Pass parameter @RequestParam [url in the form of splicing?]

  1. @RequestBody passes the user object as a parameter in the form of a json string

5. Feign optimization
  1. Open feign log

  1. Configure feign timeout

  1. http connection pool

  1. gzip compression

Zuul (gateway)

1. What is a gateway

The gateway is equivalent to the entrance of a network service architecture, and all network requests must be forwarded to specific services through the gateway

2. The role of the gateway

Unified management of microservice requests, permission control, load balancing, routing and forwarding, monitoring, security control blacklist and whitelist, etc.

3. The difference between gateway and filter
  1. The gateway is to analyze and filter the requests of all services, and the filter is for a single service

  1. A gateway is a route plus a filter

4. what is zuul

Zuul is a mature routing solution provided by SpringCloud. According to the different paths of requests, the gateway will locate the specified microservice and proxy requests to different microservice interfaces. It hides the real interface address of the microservice from the outside. Three important concepts: dynamic routing table, routing location, reverse proxy.

Dynamic routing table: Zuul supports Eureka routing, manual configuration routing, and automatic update

Routing positioning: According to the request path, Zuul has its own set of positioning service rules and routing expression matching

Reverse proxy: The client requests to the routing gateway. After the gateway accepts the request, it sends the request to the target and sends the request to the client after getting the response.

5. How to implement dynamic Zuul gateway routing and forwarding

Intercept requests through path configuration, and obtain the forwarded service list from the configuration center through ServiceId. Zuul internally uses Ribbon to achieve local load balancing and forwarding

6. How to build a cluster with Zuul gateway

Use the upstream of Nginx to set up the Zuul service cluster, intercept the request through the location and forward it to the upstream, and use the polling mechanism to send requests to the Zuul cluster by default.

7. How does Zuul implement flow control

Zuul current limiting is implemented by introducing spring-cloud-zuul-ratelimit dependencies

Current limiting type:

  1. User, limit traffic based on authenticated user or anonymous user

  1. Client IP address, flow limit based on client IP address

  1. Request path, limit flow according to request URL

  1. Throttle according to service

Guess you like

Origin blog.csdn.net/qq_35056891/article/details/129720554