Call of services between Spring Cloud microservices Spring Cloud microservices use Feign to call another service and use of Feign

Feign introduction

There are many microservice architecture service instances. How to call between services? Spring Cloud provides a solution: Feign.

Feign is a component of Spring Cloud and a WebService client, which is used to transfer services.
To learn more about Feign, you can view: https://blog.csdn.net/wo18237095579/article/details/83343915

Create two services

Create two projects and two services, configure the registration center, and then start two services. You can see two service names in the registration center you configured: my two services, daily server and task server. As follows:

Method of calling between Feign microservices

Here, I call task server through daily server. First, configure daily server. Details are as follows:

  1. Add dependency

1.1: add Feign's dependency

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

1.2 if the spring boot project integrates the spring cloud, that is to say, when you have the dependency of spring boot in your project, you need to add the dependency of spring cloud as follows:

			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Hoxton.SR1</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>

1.3 when creating a spring cloud project directly, you don't need the steps in 1.2 above to add the dependency of spring cloud directly:

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-hystrix</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-feign</artifactId>
		</dependency>
  1. Add comments to the service startup class
@EnableFeignClients

As follows:
3. Create an interface to reference the interface of TaskServer

package ...app.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient("task-server")
public interface TaskServerFeign {
    @GetMapping("/task/test")
    String getTest();
}

Here, task server in @ FeignClient("task server") is the name of your other service
@GetMapping("/task/test") interface address in another service
String getTest(); the interface name of another service.

4. Create a test interface and call another service's interface

import . . . .feign.TaskServerFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = {"Feign test"})
public class FeignTest {
    @Autowired
    private TaskServerFeign taskServerFeign;

    @GetMapping("/task/test1")
    public void getTest() {
        String test = taskServerFeign.getTest();
        System.out.println(test);
        System.out.println("----------this is daily_Server_test");
    }
}

@Api(tags = {"Feign test"}) this annotation is only used for swagger test locally. You can also use other methods to call this interface for test, such as postman

@Autowired
private TaskServerFeign taskServerFeign; injection step 3 calls the task server interface.

5. Create the called interface in task server

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FeignTest {
    @GetMapping("/task/test")
    public String getTest() {
        System.out.println("----------this is task_Server_test");
        return "this is task_Server_test";
    }
}

The next step is to run and test. Local tests are as follows:
Here is the test tool for testing,

This is daily server

This is in Task-servrer

The result is obvious. Rookie Xiaobai welcomes your comments at any time. 0. Zero

Published 5 original articles, praised 0, visited 94
Private letter follow

Keywords: Spring

Added by craygo on Wed, 04 Mar 2020 08:10:30 +0200