In-depth understanding of microservice architecture: from service registration to API management


Microservices are an architectural pattern, or a solution for distributed systems. It divides a large and complex application into many small, independent services that can be developed, deployed, and scaled independently. Next, we will follow this outline and delve into the key parts of implementing microservices in Java.

1. Service registration discovery

In a microservices architecture, service registry and discovery are key infrastructures that support communication and coordination between microservices. The service registration and discovery pattern mainly includes two core components: service registry and client.

The service registry is a database that stores available services. Each service instance will register its own network address and other metadata with the service registry when it starts. Before calling the service, the service consumer will first obtain the network address of the service provider from the service registry, and then directly call the service provider. The service registry provides a service health check mechanism to periodically check whether the registered service is available, and if the check fails, the service instance will be removed from the service registry.

In Java, there are many mature solutions for service registration and discovery, such as Netflix's Eureka, Apache's Zookeeper, and HashiCorp's Consul. These tools all support the microservices architecture well.

Here, we take Eureka as an example to briefly introduce how to implement service registration and discovery in Spring Boot applications.

Eureka Service Registry

First, we need to create a Eureka Server as a service registry. In a Spring Boot application, @EnableEurekaServeran Eureka Server can be started via annotations.

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
    
    

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

}

Then configure the Eureka Server information in the application.yml file.

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false

The above configuration defines the startup port and host name of Eureka Server, and closes the function of registering with itself and obtaining registration information, because this is a service registration center, no need to do these operations.

Eureka client

Then, we need to create a Eureka Client and register it with Eureka Server. In a Spring Boot application, an Eureka Client can be started via @EnableEurekaClientannotations @EnableDiscoveryClient.

@SpringBootApplication
@EnableEurekaClient
public class ServiceApplication {
    
    

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

}

Then configure the Eureka Client information in the application.yml file.

spring:
  application:
    name: service-a

server:
  port: 8080

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

The above configuration defines the name of the service, the startup port, and the address of the Eureka Server.

So far, we have created a Eureka service registry and a Eureka client, and completed the basic process of service registration and discovery. In actual development, it is also necessary to consider service health checks, how service consumers call service providers through service names, and high-availability deployment of services.

1.2 API Gateway

insert image description here

API gateway plays a very critical role in microservice architecture. It acts as an intermediate layer between the client and various microservices. All client requests will first pass through the API gateway and then be distributed to the corresponding microservices. This has many advantages, including simplifying client calls, hiding details of internal services, providing unified security protection, and centrally handling cross-service issues.

In Java, Spring Cloud Gateway and Netflix Zuul are two commonly used API gateway solutions. Let's take Spring Cloud Gateway as an example to see how to implement API gateway in Spring Boot application.

First, we need to create a Spring Boot project and add Spring Cloud Gateway dependencies to the project.

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

Then, we can configure the routing rules in the application.yml file. The following configuration indicates that all requests whose request path starts with /api/users will be routed to the user-service service.

spring:
  cloud:
    gateway:
      routes:
      - id: user-service
        uri: lb://user-service
        predicates:
        - Path=/api/users/**
        filters:
        - StripPrefix=1

In this configuration, lb://user-serviceit is the address of the user-service service, Path=/api/users/**the condition for route matching, StripPrefix=1and a filter that removes the prefix /api/users in the request path.

In addition, Spring Cloud Gateway also provides many powerful functions, including request current limiting, circuit breaker, retry, security verification, etc. These functions can be realized very conveniently by configuring corresponding filters and global filters.

For example, the following configuration indicates that only 100 requests per second are allowed for user-service access.

spring:
  cloud:
    gateway:
      routes:
      - id: user-service
        uri: lb://user-service
        predicates:
        - Path=/api/users/**
        filters:
        - StripPrefix=1
        - name: RequestRateLimiter
          args:
            redis-rate-limiter.replenishRate: 100
            redis-rate-limiter.burstCapacity: 100

The above is the basic process of implementing API gateway in Spring Boot application. In actual development, it is also necessary to customize various routing rules and filters according to specific needs.

1.3 Configuration Center

In the microservice architecture, due to the large number of services, the configuration information is scattered in each service, which brings great challenges to configuration management. In order to solve this problem, we can introduce a configuration center to centrally store and manage all configuration information. When configuration information changes, the configuration center can push updates in real time to ensure that all services use the latest configuration information.

In Java, Spring Cloud Config is a commonly used configuration center solution. Let's take Spring Cloud Config as an example to see how to implement a configuration center in a Spring Boot application.

First, we need to create a Spring Cloud Config Server as a configuration center. In a Spring Boot application, @EnableConfigServera Config Server can be started via annotations.

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
    
    

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

}

Then, we need to configure the Config Server information in the application.yml file, as well as the storage location of the configuration file. The following configuration indicates that Config Server will read configuration files from the local git repository.

server:
  port: 8888

spring:
  cloud:
    config:
      server:
        git:
          uri: file://${
    
    user.home}/config-repo

In this configuration, file://${user.home}/config-repoit is the storage location of the configuration file, and the Config Server will read the configuration file from this location.

Then, we need to create a Spring Cloud Config Client to read configuration information from the Config Server. In a Spring Boot application, spring-cloud-starter-configa Config Client can be started by.

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

Then, we need to configure the Config Client information in the bootstrap.yml file. The following configuration indicates that the Config Client will read the configuration information named my-service from the Config Server at localhost:8888.

spring:
  application:
    name: my-service
  cloud:
    config:
      uri: http://localhost:8888

In this configuration, http://localhost:8888it is the address of the Config Server my-serviceand the name of the service, and the Config Client will read the configuration information corresponding to this name from the Config Server.

The above is the basic process of implementing the configuration center in the Spring Boot application. In actual development, it is also necessary to consider encryption of configuration information, real-time push of configuration changes, and version management of configuration.

1.4 Event Scheduling (Kafka)

In the microservice architecture, communication between services often adopts a synchronous or asynchronous manner. However, as the scale and complexity of the system increase, this communication method may lead to an increase in coupling between services. Event-driven architecture (EDA) is an effective way to solve this problem, which is based on the event-based publish-subscribe model to achieve decoupling between services.

Kafka is a popular event streaming platform that stores and transmits events as time streams to individual microservices. Kafka provides event stream processing with high throughput, low latency, and strong consistency guarantees.

In Spring Boot, we can use Spring Kafka for Kafka operations. First, we need to introduce Spring Kafka dependencies into the project.

<dependency>
    <groupId>org.springframework.kafka</groupId>
    <artifactId>spring-kafka</artifactId>
</dependency>

Kafka producer

Kafka producers are used to send messages to Kafka. Here is an example of a simple Kafka producer:

@Service
public class KafkaProducer {
    
    

    private final KafkaTemplate<String, String> kafkaTemplate;

    public KafkaProducer(KafkaTemplate<String, String> kafkaTemplate) {
    
    
        this.kafkaTemplate = kafkaTemplate;
    }

    public void send(String topic, String message) {
    
    
        kafkaTemplate.send(topic, message);
    }

}

In this example, KafkaTemplatefor sending messages to Kafka. We just need to call its sendmethod and specify the subject and message content.

Kafka consumer

Kafka consumers are used to receive messages from Kafka. Here is an example of a simple Kafka consumer:

@Service
public class KafkaConsumer {
    
    

    @KafkaListener(topics = "${kafka.topic}")
    public void receive(String message) {
    
    
        System.out.println("Received message: " + message);
    }

}

In this example, @KafkaListenerthe method marked with the annotation will be used as the processing method of the message. ${kafka.topic}This method will be called when a message with subject is received .

The above code shows the basic process of how to use Spring Kafka for event scheduling in Spring Boot. In actual development, issues such as message serialization and deserialization, error handling, transaction management, and message partitioning need to be dealt with.

1.5 Service tracking (starter-sleuth)

In the microservice architecture, requests may be completed through multiple services. If there is a problem in a certain link, it is difficult to quickly locate it. To solve this problem, we need to introduce service tracking. Service tracking can record the complete path of the request in the system and provide a visual call link to help us locate the problem.

In Java, Spring Cloud Sleuth is a commonly used solution for service tracking. In the Spring Boot application, we can enable service tracking by introducing the dependency of spring-cloud-starter-sleuth.

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>

Sleuth will automatically generate a unique Trace ID and multiple Span IDs for each request. The Trace ID represents a complete request link, and the Span ID represents a link in the request link. We can view these IDs in the log, and can also cooperate with tools such as Zipkin for visual display.

1.6 Service fuse (Hystrix)

insert image description here

In the microservice architecture, due to the dependencies between services, once a service fails, the entire system may be paralyzed, which is the so-called avalanche effect. In order to prevent the avalanche effect, we need to introduce a service fusing mechanism. When there is a problem with a service, the service fuse can disconnect the call to the service in time to prevent the problem from expanding further.

In Java, Netflix's Hystrix is ​​a commonly used service fusing and fault-tolerant processing library. In the Spring Boot application, we can enable the service fuse by introducing the dependency of spring-cloud-starter-netflix-hystrix.

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

Then, we can add the @HystrixCommand annotation to the method that needs fuse protection to specify the downgrade method.

@HystrixCommand(fallbackMethod = "fallbackMethod")
public String hystrixMethod() {
    
    
    // your code
}
public String fallbackMethod() {
    
    
    return "fallback value";
}

In this example, when hystrixMethodthe method execution fails or times out, Hystrix will automatically call fallbackMethodthe method and return the degradation result.

The above is the basic process of implementing service tracking and service fusing in Spring Boot applications. In actual development, it is also necessary to consider storage and query of service tracking, policy configuration and monitoring of service fusing, etc.

1.7 API management

API management is an important part of the microservice architecture, which mainly includes API design, release, version management, access control, current limiting, billing, monitoring and other functions. Through API management, we can provide a consistent API interface, ensure the availability and security of the API, and improve the experience of using the API.

In Java, there are many API management tools, such as Swagger, Postman, etc. In Spring Boot applications, we can use Swagger for API management.

First, we need to introduce Swagger dependencies into the project.

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

Then, we need to configure Swagger to generate API documentation.

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    
    
    @Bean
    public Docket api() {
    
    
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
}

In this configuration, @EnableSwagger2Swagger is enabled, apis(RequestHandlerSelectors.any())and paths(PathSelectors.any())API documents for all Controllers are generated.

In addition, we can also use Swagger annotations on Controller methods to provide more detailed API information.

@RestController
@RequestMapping("/api/users")
public class UserController {
    
    

    @ApiOperation(value = "Get all users", notes = "Get a list of all users")
    @GetMapping("/")
    public List<User> getAllUsers() {
    
    
        // your code
    }
    
    // other methods
}

In this example, @ApiOperationannotations represent the operations and annotations of this API.

Summarize

Microservice architecture is a distributed architecture that splits a complex system into a set of independent services, each service has its own database and business logic, and services communicate through APIs.

In this article, we introduce seven key components of the microservice architecture, including service registration and discovery, API gateway, configuration center, event scheduling, service tracking, service fusing, and API management, and use Java's Spring Boot for detailed accomplish.

This is just the tip of the iceberg of the microservice architecture, which also involves many other aspects, such as service deployment and monitoring, data consistency and isolation, service testing and evolution, etc. I hope this article can help you have a basic understanding of microservices and lay a solid foundation for your further study of microservices.

Guess you like

Origin blog.csdn.net/weixin_46703995/article/details/131820311