This is my eighth day of the August Challenge

This series of code address: github.com/HashZhang/s…

Spring-cloud-common is no longer a purely dependent project. This module includes:

  1. Spring – framework – common dependency
  2. Common dependencies for synchronous and asynchronous microservices
  3. Synchronous and asynchronous microservices common framework code changes, which we will examine in detail later when we analyze the framework and our design changes, but will skip here

Common dependencies for synchronous and asynchronous microservices include:

Code please refer to: github.com/HashZhang/s…

1. Enable the Bootstrap Context of Spring Cloud: Starting with Spring Cloud 2020.0. X, the Bootstrap Context is not enabled by default. In our project, some modules use spring-cloud-config, which requires Bootstrap Context enabled. At the same time, our configuration also uses bootstrap.yml and application.yml to distinguish different configurations. If the configurations are the same in multiple environments and basically do not change dynamically, we put them into bootstrap.yml. Put it in application.yml for different environments or for dynamic change. So enable it by adding the following dependencies:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
Copy the code

The underlying implementation is very simple. Whether to enable the Bootstrap Context is determined by checking for the presence of a Marker class in the dependency. Reference code:

PropertyUtils. Java:

/**
 * Property name for bootstrap marker class name.
 */
public static final String MARKER_CLASS = "org.springframework.cloud.bootstrap.marker.Marker";

/**
 * Boolean if bootstrap marker class exists.
 */
public static final boolean MARKER_CLASS_EXISTS = ClassUtils.isPresent(MARKER_CLASS, null);
Copy the code

2. To use Eureka as the registry, we need to add client dependencies for Eureka:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
Copy the code

3. Instead of using the Ribbon, use Spring Cloud LoadBalancer as our LoadBalancer:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
Copy the code

4. Use Resilience4J as a component basis for retries, outages, concurrency, and current limits:

<dependency>
    <groupId>io.github.resilience4j</groupId>
    <artifactId>resilience4j-spring-cloud2</artifactId>
</dependency>
Copy the code

5. Expose the RELATED ports of the ACTUATOR

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Copy the code

6. Use Prometheus to monitor and collect indicators

<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
Copy the code

Spring-cloud-webmvc is based on the dependency of synchronous spring-MVC microservices. Similarly, Spring-cloud-webMVC also includes the common framework code transformation of synchronous microservices, which will be analyzed in detail later when we analyze the framework and the modification of our design. I’m going to skip this. Let’s just talk about dependencies here:

Code please refer to: github.com/HashZhang/s…

1. Spring-cloud-common dependency: As mentioned earlier, Spring-cloud-common is the public dependency of Spring-cloud-webMVC and Spring-cloud-Webflux.

2. Use undertow as our Web container: The default container for Web-MVC is Tomcat, so you need to exclude this dependency and add undertow dependencies.

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion>  </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-undertow</artifactId> </dependency>Copy the code

3. Use webFlust-related asynchronous interfaces. Some microservices are mainly based on synchronous interfaces, but some special interfaces use asynchronous responsive implementations, which will not conflict, so we also add Web-flux dependencies here.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
Copy the code

4. Use OpenFeign as the synchronous microservice invocation client. OpenFeign is mainly used as the synchronous client at present.

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
Copy the code

5. Adhesive the code dependence between OpenFeign and Resilience4J. The official bonding code between OpenFeign and resilience4J is provided, please refer to: Resilience4J-feIGN. We’re going to do some personalization based on that, which we’ll look at in more detail later.

<dependency>
    <groupId>io.github.resilience4j</groupId>
    <artifactId>resilience4j-feign</artifactId>
</dependency>
Copy the code

Spring-cloud-webmvc is based on the dependence of asynchronous responsive Spring-Webflux microservices. Similarly, Spring-cloud-webflux also contains the common framework code transformation of asynchronous microservices. When we analyze the framework and the design modification later, I’ll look at it in detail, but I’ll skip it here. Let’s just talk about dependencies here:

Code please refer to: github.com/HashZhang/s…

1. Spring-cloud-common dependency: As mentioned earlier, Spring-cloud-common is the public dependency of Spring-cloud-webMVC and Spring-cloud-Webflux.

2. Use Webflux as our Web container, we don’t need an additional Web container here.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
Copy the code

3. Adhere to project-reactor and Resilience4j, which is often used in asynchronous scenarios. Please refer to resilience4J-Reactor

<dependency>
    <groupId>io.github.resilience4j</groupId>
    <artifactId>resilience4j-reactor</artifactId>
</dependency>
Copy the code

In this section, we analyze the common microservice dependencies in our project as well as web-based MVC synchronous microservice dependencies and Web-based Flux asynchronous microservice dependencies. In the next section, we’ll take a closer look at our Spring Cloud framework, starting with some Spring basics.

Wechat search “my programming cat” to follow the public account, every day, easy to improve technology, win a variety of offers: