Springboot&Springcloud interview questions (including code and display diagram)

1 Briefly talk about springboot?

Spring Boot is a brand-new framework provided by the Pivotal team. Its design purpose is to simplify the initial setup and development process of Spring applications. -After using springboot, building a spring application and development, deployment becomes very simple.

2 How to simply implement a springboot application

1) Create a new maven project

2) Let the project take spring-boot-starter-parent as the parent module

<parent>     
    <groupId>org.springframework.boot</groupId>   
    <artifactId>spring-boot-starter-parent</artifactId>    
    <version>2.0.5.RELEASE</version> 
</parent>

3) Choose some starters according to the scene

<dependency>   
   <groupId>org.springframework.boot</groupId>    
   <artifactId>spring-boot-starter-web</artifactId>  
</dependency>  

<dependency>      
  <groupId>org.springframework.boot</groupId>    
  <artifactId>spring-boot-starter-test</artifactId>
</dependency>

4) Configure application.yml/application.properties

server:

 port: 8080 

5) Write port

@SpringBootApplication 

 public class PetHomeApplication {   
       public static void main(String[] args) {
            SpringApplication.run(PetHomeApplication.class,args);    
  }  }

6) Test

Start main, start via localhost:8080 without error

3 How does springboot realize multi-environment configuration

1) What is multi-environment configuration

It is a set of code, which may be used in multiple environments. It is necessary to configure multiple environments.

2) How to configure multiple environments

① Configure multiple environments in one file

② Multiple files (application-environment name.yml/properties)

3) How to specify a specific environment

①Start by default

②Specify the startup environment

4 How to deploy the springboot project

Springboot has integrated the built-in Tomcat by default. After packaging it in the jar, there is tomcat in it. It is ok to run directly as a jar package.

1) Packing

pom.xml加入打包插件

<build>      
      <plugins>          
          <plugin>            
  <groupId>org.springframework.boot</groupId>       
  <artifactId>spring-boot-maven-plugin</artifactId>         
          </plugin>      
      </plugins> 
</build>

打包:

mvn clean package

2) Deployment

windows: java -jar xxx.jar

Linux: run nohup java -jar xxx.jar in the background

5 Principles of springboot automatic configuration

When we introduce the starter, we will use the springboot automatic configuration principle to configure this scenario.

image

①Springboot application entry class uses @EnableAutoConfiguration (parent annotation) through @SpringBootApplication annotation introduction

②After using @EnableAutoConfiguration, all spring.factories files in the jar will be scanned, and all configuration classes will be loaded (@Configuration classes added)

③Configuration class (class with @Configuration added) uses XxxProperties to complete the configuration of the corresponding scene in the way of @Bean

④ The configuration information in XxxPropereis has default values, but we can get it in application.properties/application.yml

6 What is Monolith

Under normal circumstances, the code corresponding to this project can be composed of multiple modules, and each module has a clear boundary according to the different functions provided by it. During compilation, these projects will be packaged into a JAR package, and finally merged together to form a jar or WAR package

image

This kind of monolithic application is relatively simple when the project is small, but as the project gets bigger and bigger, the code becomes more and more. There will be the following shortcomings.

①Difficult to compile, difficult to deploy, difficult to test

The amount of code increases. Even if a line of code is changed, it takes a lot of time to compile. Before deployment, it needs to be compiled, packaged, decompressed, etc. It is difficult to deploy, and it is difficult to test after deployment.

②Difficult to choose technology

As it becomes larger and larger, the technology used in our applications will also become more and more. Some of these technologies are incompatible. For example, it is almost impossible to mix C++ and Java on a large scale in a project. In this case, we need to abandon the use of certain incompatible technologies, and choose a technology that is not so suitable to achieve specific functions.

③Difficult to expand

Even if there is only one function with high concurrency, all must be extended

image

7 What is MicroService

Microservice architecture is an architectural style. A large and complex software should consist of one or more microservices. Each microservice in the system can be selected by independent technology, independent development, independent deployment, independent operation and maintenance, and each microservice is loosely coupled. Each microservice only focuses on completing one task and completing that task well. In all cases, each task represents a small business capability.

image

8 Monolith&MicroService selection

image

As can be seen from the above figure, in the initial stage, the efficiency of developing applications using the Microservice architecture pattern is significantly lower than that of Monolith. However, as the scale of applications increases, the development efficiency based on the Microservice architecture model will increase significantly, while the development efficiency based on the Monolith model will gradually decline.

Monolithic application architecture: small and medium-sized projects (relatively few functions), company official website, management system, etc.

Micro-service architecture: large-scale projects (more functions) mall erp, human resources, pet paradise, etc.-Internet projects

9 What to pay attention to when MicroService splits microservices

1. Single responsibility, high cohesion and low coupling

2. Moderate service granularity

3. Consider team structure

4. Cut in with a business model

5. Evolutionary split

6. Avoid circular dependency and bidirectional dependency

10 Problems to be solved at the core of MicroService

1. Service Registration and Discovery-Registration Center

2. Unified entrance of microservices-API network management

3. Service fault tolerance and current limit

4. Centralized configuration center for services

5. Service call chain tracking

6. Service deployment

7. Service Monitoring

11 Common Solutions of MicroService

1. Springcloud Netflix

2. Springcloud alibaba

3. Dubbo

4. Kubernates-k8s

5. Service mesh-istio

6. Other

12 Briefly talk about Springcloud

Microservice architecture is an idea that requires technology to be implemented. In the spring microservice solution, springboot is used to implement a single service, and the coordinated calls between services are implemented using the service governance framework springcloud netflix/alibaba. The following mainly talk about the common components of netflix as follows:

Five Great Beasts:

Service registration discovery (registry center)-Netflix Eureka: Help us manage the service's communication address (ip, port)

Client load balancing-Netflix Ribbon\Feign: Solve service calls

Circuit Breaker-Netflix Hystrix: Solve the failure of microservices, protect the microservices

Service gateway-Netflix Zuul: unified access entrance, the door to microservices (security department)

Distributed configuration-Spring Cloud Config: unified management of microservice configuration

13 Briefly talk about Springcloud Netflix eureka

Eureka is a submodule of netflix and one of the core modules.

Eureka is a service registration and discovery component. Simply put, it is used to uniformly manage the communication address of microservices. It consists of two parts: EurekaServer server (also called registry) and EurekaClient client. EureakServer is an independent service, and EurekaClient It needs to be inherited into each microservice.

When the microservice (EurekaClient) is started, it will submit its own communication address list to EureakServer, such as: service name, ip, port, and EurekaServer will form a microservice communication address list — this is called service registration

The microservice (EurekaClient) will periodically pull a list of microservice communication addresses from EureakServer and cache it locally. When a microservice initiates a call to another microservice, it will find its communication address list based on the service name of the target service, and then initiate a request to the target service based on the HTTP protocol. -This is called service discovery

In addition, the microservice (EurekaClient) uses the "heartbeat" mechanism to send a request to EureakServer to renew the service. In fact, it sends a request to EureakServer to report its health status regularly, telling EureakServer that you are still alive, and don't drop yourself from the service address list. Then when the microservice (EurekaClient) fails to renew the contract with EureakServer, or the renewal request times out, the registry will remove the failed service from the address list.

14 Briefly talk about Springcloud Netflix ribbon/feign

Ribbon is an open source project for cloud middle-tier services released by Netflix. Its main function is to provide client-side load balancing algorithms.

Ribbon is a client load balancer, it can complete multiple server load balancing calls according to certain rules, and these rules also support customization.

Feign is a declarative Web Service client, its purpose is to make Web Service calls easier. Feign provides the HTTP request interface template (the access address is marked above). By writing a simple interface and inserting annotations, you can define the parameters, format, address and other information of the HTTP request. Feign will completely proxy HTTP requests. We only need to call it like a method to complete service requests and related processing. Feign integrates Ribbon and Hystrix (we will talk about Hystrix later), which allows us to no longer need to use these two components explicitly.

15 Briefly talk about Springcloud Netflix hystrix

Hystrix is ​​a very popular high-availability framework open sourced by the well-known foreign video website Netflix. Hystrix can perfectly solve a series of technical problems faced by creating highly available services in the distributed system architecture.

Hystrix "Porcupine" has the ability to protect itself. Hystrix uses the following mechanisms to solve the avalanche effect problem.

1 Resource isolation (current limit): Including thread pool isolation and semaphore isolation, limiting the use of resources for invoking distributed services, and a problem with a called service will not affect other service calls.

2 Fuse: When the failure rate reaches the threshold, the degradation is automatically triggered (for example, the failure rate is high due to network failure/timeout), and the fast failure triggered by the fuse will recover quickly.

3 Downgrade mechanism: Downgrade over time and insufficient resources (thread or semaphore). After the downgrade, it can cooperate with the downgrade interface to return the bottom data.

4 Cache: Provides request cache and request merge implementation.

16 Briefly talk about Springcloud Netflix zuul

Zuul is an API Gateway server open sourced by netflix. The main functions are as follows:

1. Dynamic routing distinguish which specific service to call by different addresses

2. Filter Through the filter, login interception, authority judgment, etc. can be realized

3. Load balancing The bottom layer implements load balancing calls through ribbon

4. Fuse downgrade The bottom layer uses hystrix to implement service guarantee measures such as fuse downgrade

5. …

17 Briefly talk about Springcloud Netflix Config server

In a distributed system, due to the huge number of services, in order to facilitate unified management of service configuration files and real-time updates, a distributed configuration center component is required. In Spring Cloud, there is a distributed configuration center component spring cloud config, which supports the configuration service in the memory of the configuration service (that is, local), and it also supports the remote Git warehouse. In the spring cloud config component, there are two roles, one is config server, and the other is config client.

18 MicroService Architecture-Link Tracking

In the microservice architecture, there are mutual calls between services, and service call chain monitoring is required. There are many products on the market, the specific advantages and disadvantages are as follows:

image

19 MicroService Architecture-Deployment

In the microservice architecture, there are many services, so many services need to be quickly deployed and quickly upgraded. Therefore, docker deployment is generally used, and a cicd pipeline is required. The specific process is as follows:

image

Guess you like

Origin blog.csdn.net/Java_Yhua/article/details/110484030