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
- Add dependence;
- Start class enables Feign;
- Write remote call interface;
- 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 @EnableFeignClients
notes, 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)
- 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();
}
- 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:
V. browser to access test interface
Sixth, the test service call fails
- Open Hystrix fuse function in the configuration file;
feign:
hystrix:
enabled: true
- Close Service Provider (nacos-provider);
- Refresh your browser to view the result of the call.
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.