Spring cloud learning [6] - communication between openfeign component services

Using RestTemplate+ribbon can complete the call between services. Why use feign?

String getResult = restTemplate.getForObject("http: / / {{service ID} / {{URL}", String.class);

  • Existing problems
  1. This code needs to be written every time the service is called, and there is a lot of code redundancy
  2. If the service address is modified, the maintenance cost will increase
  3. The response result cannot be automatically converted to the corresponding object
  4. ribbon must be integrated for load balancing

OpenFeign component

1, Introduction

1. Development

Feign component ----- -- > openfiegn component

Feign(Netflix) components enter the maintenance state later because feign provides a very good idea of inter service invocation. However, due to maintenance, the state of subsequent feign components is not managed, and developers can not enjoy the characteristics of feign components. Springcloud, the top leader of micro service, has to offer solutions to the micro service for the vast number of developers, so the springcloud team begins to absorb the essence of the open source feign components and encapsulates OpenFeign components. In order to reduce the learning cost of developers, the feign has not been greatly transformed, only absorbing the essence of feign components. OpenFeign(Spring) and Feign(Netflix) component features are used in the same way.

2. Introduction

Official website: https://cloud.spring.io/spring-cloud-openfeign/reference/html/

Declarative REST Client: Feign
Feign is a declarative web service client. It makes writing web service clients easier. To use Feign create an interface and annotate it. It has pluggable annotation support including Feign annotations and JAX-RS annotations. Feign also supports pluggable encoders and decoders. Spring Cloud adds support for Spring MVC annotations and for using the same HttpMessageConverters used by default in Spring Web. Spring Cloud integrates Eureka, as well as Spring Cloud LoadBalancer to provide a load-balanced http client when using Feign.

Feign is a declarative Web service client. It makes it easier to write Web service clients. To create an interface using feign and annotate it. It has pluggable annotation support, including feign annotations and JAX-RS annotations. Feign also supports pluggable encoders and decoders. Spring Cloud adds support for Spring MVC annotations and supports the use of annotations used by default in httpmessageconverters spring Web. Spring Cloud integrates Eureka and Spring Cloud LoadBalancer to provide a load balanced http client when feign is used.

OpenFeign and RestTemplate have the same function. They are both http clients

  • RestTemplate: the Spring framework encapsulates the HttpClient object
  • OpenFeign: pseudo HttpClient object (which can be understood from the proxy server). It can make the communication between services easier

2, Use

1. Create two springboot applications

FriendApplication calls ShareApplication

		<!--springboot-->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>
2. Register with the consumer registry

Introduce consumer dependency

		<!--introduce consul rely on-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>

        <!--Health examination dependence-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

Modify the configuration file and register the service with consumer

# Register with consumer
spring.cloud.consul.host=192.168.0.133
spring.cloud.consul.port=8500

# Specify the service name of the registration service. The default is ${spring.application.name}
spring.cloud.consul.discovery.service-name=${spring.application.name}

# Register by my ip address when registering
spring.cloud.consul.discovery.prefer-ip-address=true
spring.cloud.consul.discovery.ip-address=192.168.0.119
3. Add the service registration client annotation to the entry class
@SpringBootApplication
@EnableDiscoveryClient
public class FriendsApplication {

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

}
4. Start to view the consumer web side

5. Use OpenFeign for service invocation

Introducing the openfeign dependency on the service caller

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

Add annotations to the service caller's entry class to enable openfeign call support

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class FriendApplication {

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

}

Develop the service client interface to be called on the caller

/**
 * Interface to call Share service
 */
@FeignClient(value = "SHARE") // value = ID of the calling service
public interface ShareClients {

    /**
     * Call share service share interface
     * @return
     */
    @GetMapping("shareConApi/share")
    public String share();

}

FriendController

@RestController
@RequestMapping("friendConApi")
public class FriendController {

    private static Logger log = LoggerFactory.getLogger(FriendController.class);
    @Resource
    private ShareClients shareClients;

    @GetMapping("invokeShare")
    public String invokeShare() {
        String share = shareClients.share();
        log.info("invokeShare success={}", share);
        return "invokeShare success " + share;
    }
}



Load balancing policy for default polling

Keywords: Spring Cloud OpenFeign

Added by dvdflashbacks on Wed, 26 Jan 2022 09:36:02 +0200