Introduction to Feign
There are many service instances of the microservice architecture. How to call between services and services, Spring Cloud provides a solution: Feign the pretender.
Feign is a component of Spring Cloud and a WebService client, used for transfer between services.
To learn more about Feign, please check: https://blog.csdn.net/wo18237095579/article/details/83343915
Create two services
Create two projects and two services, configure the registry, and then start the two services, you can see two service names in the registry you configured: my two services, Daily-server and Task-Server. as follows:
Methods of calling between Feign microservices
I am here to call Task-server through Daily-server. First, configure Daily-server. The details are as follows:
- 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 spring cloud here, that is, when there is a spring boot dependency in your project, you need to add the spring cloud dependency as:
<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 you directly create a spring cloud project, you do not need the steps in 1.2 above, and directly add spring cloud dependencies:
<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>
- Add annotations to the service startup class
@EnableFeignClients
As follows:
3. Create an interface to reference the TaskServer interface
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 @FeignClient("task-server") where task-server is the name of your other service
@GetMapping("/task/test") The address of the interface in another service
String getTest(); The interface of another service name.
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测试"})
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 locally for swagger testing. You can also use other methods to call this interface for testing, such as postman
@Autowired
private TaskServerFeign taskServerFeign; Inject the interface that calls task-server in the third step.
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. The local test is as follows:
here is the test tool for testing,
here is the Daily-server and
this is the Task-servrer.
The result is obvious. Novice Xiaobai welcomes your comments at any time. 0. 0