Three, Spring Cloud Alibab-Feign- service calls

I. Overview

        Feign is a pseudo HTTP client a declarative, it makes writing HTTP clients easier. Using Feign, only you need to create an interface and annotation. It has pluggable annotation feature can be used Feign JAX-RS annotations and notes. Feign pluggable encoder and decoder. Feign default integrated Ribbon, Nacos also very good compatible Feign, the default implementation of load balancing.

Second, use the steps

  1. Add dependence;
  2. Start class enables Feign;
  3. Write remote call interface;
  4. Use interface to complete the service call

Third, case presentations

        This case is the last article, " two, Spring Cloud Alibaba-Nacos- service registration and discovery to transform the" basis.
        In the last article of 服务消费者(nacos-consumer)the module is to use RestTemplate to call the service provider, and will not do in the actual development, in this case on the use of Feign to implement the service call.
        Note: The following code reform are the 服务消费者(nacos-consumer)module! ! !

3.1, Maven dependent (POM)

        In 服务消费者(nacos-consumer)adding Feign dependency module:

<!-- Feign Start -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- Feign End -->

3.2, startup class (Application)

        Added to the startup class @EnableFeignClientsnotes, Feign service call function is enabled:

package com.zhoumo.nacos.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
// Nacos服务注册与发现
@EnableDiscoveryClient
// Feign服务调用
@EnableFeignClients
public class NacosConsumerApplication {

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

3.3, remote call interface (Client)

  1. Create a remote call interface, designated Feign call the service name, and service methods defined in the interface need to call; name- service name is called, fallback- call fails processing class
package com.zhoumo.nacos.consumer.client;

import com.zhoumo.nacos.consumer.client.fallback.ProviderServiceFallback;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient(name = "service-provider", fallback = ProviderServiceFallback.class)
public interface ProviderClient {

    @RequestMapping("/provider/hello")
    String hello();

}
  1. When Feign call service fails, it will enter the appropriate processing method.
package com.zhoumo.nacos.consumer.client.fallback;

import com.zhoumo.nacos.consumer.client.ProviderClient;
import org.springframework.stereotype.Component;

@Component
public class ProviderServiceFallback implements ProviderClient {

    @Override
    public String hello() {
        return "Remote call to nacos-provider failed!";
    }
}

Fourth, the project start verify successful registration

        Nacos Server access through a browser: http: //192.168.145.129: 8848 / nacos; then click on the list of services, you can view the service has been registered on the Nacos Server:
Here Insert Picture Description

V. browser to access test interface

Here Insert Picture Description

Sixth, the test service call fails

  1. Open Hystrix fuse function in the configuration file;
feign:
  hystrix:
    enabled: true
  1. Close Service Provider (nacos-provider);
    Here Insert Picture Description
  2. Refresh your browser to view the result of the call.
    Here Insert Picture Description

Seven summary

        This paper describes how to use Feign complete the steps in the project to service calls, as well as open the service fuse handling of service calls fail. On the basis of the article on the use Feign alternative RestTemplate achieve service invocation case presentations.


Published 47 original articles · won praise 16 · views 70000 +

Guess you like

Origin blog.csdn.net/zorro_jin/article/details/105293609