Service consumption in Spring Boot

Service consumption in Spring Boot

In distributed systems, service consumption is a very common scenario. Through service consumption, a service in one system can be used as a component in another system. Spring Boot provides many tools to simplify the process of service consumption. This article will delve into what service consumption in Spring Boot is, its principle and how to use it.

What is service consumption

Service consumption means that in a distributed system, one application completes some tasks by invoking the services of another application. In the process of service consumption, the service provider does not need to care who the caller of the service is, and the service caller does not need to care who the service provider is. This decoupling approach makes the system more flexible and scalable.

In Spring Boot, service consumption is usually implemented through REST API. REST API is a lightweight web service based on HTTP protocol. The service provider returns the data to the caller in the form of JSON or XML, and the caller invokes the service through an HTTP request.

insert image description here

Principles of Service Consumption

The principle of service consumption can be briefly summarized as the following steps:

  1. Service providers register services with a service registry.
  2. The service consumer obtains the address of the service from the service registry.
  3. Service consumers invoke services through HTTP requests.
  4. The service provider returns the data to the caller in the form of JSON or XML.

In Spring Boot, service providers usually use Eureka in Spring Cloud to register services. Eureka is a REST-based service for service discovery and registration. Service consumers can use Ribbon or Feign in Spring Cloud to invoke services.

Ribbon is an HTTP and TCP based client load balancer. It can dynamically allocate requests to different service instances according to the number and load of service providers. Ribbon also provides some load balancing strategies, such as round robin, random and weighted round robin, etc.

Feign is an HTTP client based on Ribbon and Spring MVC. It makes service calls simpler and more elegant. Through annotations and interface definitions, Feign can automatically generate REST client code, convert the request into HTTP request and send it to the service provider.

How to use service consumption

In Spring Boot, using service consumption is very simple. The following will introduce how to use Ribbon and Feign to consume services.

Use the Ribbon

First, add the following dependencies to the pom.xml file:

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

Then, add the @EnableDiscoveryClient annotation to the startup class:

@SpringBootApplication
@EnableDiscoveryClient
public class Application {
    
    
  public static void main(String[] args) {
    
    
    SpringApplication.run(Application.class, args);
  }
}

Next, define a service call class:

@RestClient
public class MyService {
    
    
  private final RestTemplate restTemplate;

  @Autowired
  public MyService(RestTemplate restTemplate) {
    
    
    this.restTemplate = restTemplate;
  }

  public String callService() {
    
    
    return restTemplate.getForObject("http://SERVICE-PROVIDER/hello", String.class);
  }
}

In this class, we use the @RestClient annotation to declare that this is a REST client. In the constructor, we inject a RestTemplate object, which is the default HTTP client in Spring Boot. In the callService() method, we call the /hello interface of the service provider through the restTemplate.getForObject() method.

Finally, inject the MyService class in the Controller and call the callService() method:

@RestController
public class MyController {
    
    
  private final MyService myService;

  @Autowired
  public MyController(MyService myService) {
    
    
    this.myService = myService;
  }

  @GetMapping("/hello")
  public String hello() {
    
    
    return myService.callService();
  }
}

In this Controller, we inject the MyService class and call the callService() method of the MyService class in the hello() method.

In the above code, SERVICE-PROVIDER is the name of the service provider, which can be configured in the application.properties file:

spring.application.name=my-app
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

Here, we set the name of the application to my-app and the default address of Eureka to http://localhost:8761/eureka/.

Now, we can start the application, visit http://localhost:8080/hello, and we can see the data returned by the service provider.

Use Feign

If you want to consume services more simply and elegantly, you can use Feign. First, add the following dependencies to the pom.xml file:

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

Then, add the @EnableFeignClients annotation to the startup class:

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class Application {
    
    
  public static void main(String[] args) {
    
    
    SpringApplication.run(Application.class, args);
  }
}

Next, define a Feign interface:

@FeignClient("SERVICE-PROVIDER")
public interface MyFeignClient {
    
    
  @GetMapping("/hello")
  String callService();
}

In this interface, we use the @FeignClient annotation to declare that this is a Feign client. In the @FeignClient annotation, we specify the name of the service provider. In the interface, we define a callService() method, which corresponds to the /hello interface of the service provider.

Finally, inject the MyFeignClient class in the Controller and call the callService() method:

@RestController
public class MyController {
    
    
  private final MyFeignClient myFeignClient;

  @Autowired
  public MyController(MyFeignClient myFeignClient) {
    
    
    this.myFeignClient = myFeignClient;
  }

  @GetMapping("/hello")
  public String hello() {
    
    
    return myFeignClient.callService();
  }
}

In this Controller, we inject the MyFeignClient class and call the callService() method of the MyFeignClient class in the hello() method.

Now, we can start the application, visit http://localhost:8080/hello, and we can see the data returned by the service provider.

Summarize

Service consumption is a very important part of a distributed system. In Spring Boot, we can use Ribbon and Feign to simplify the process of service consumption. Through the introduction of this article, we understand the principle of service consumption and learn how to use Ribbon and Feign to consume services. Hope this article can help you better understand service consumption in Spring Boot.

Guess you like

Origin blog.csdn.net/2302_77835532/article/details/131506717