The purpose of Feign is to make it easier to write Java clients. In other words, it will make it easier to write Java clients like controllers.
1 introduction OpenFeign
Create a parent open-Feign module and create a child Feign-server module under its module. In order to import The OpenFeign module, modify the POM file of the parent module, which actually adds the dependency
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
Copy the code
This allowed OpenFeign to be introduced, and the final project structure was as follows
Configuration files and main startup classes
There are no special cases where services are registered in Eureka and then write their own port and service name.
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
server:
port: 8101
spring:
application:
name: feign-server
Copy the code
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
// @enableFeignClients // Note the commented out annotation
public class FeignConusme9201 {
public static void main(String[] args) { SpringApplication.run(FeignConusme9201.class, args); }}Copy the code
Here is a brief description of the difference between @EnableDiscoveryClient and @EnableEurekaclient. Service discovery is generally implemented in Eureka, Consul, and Zookeeper modes. If using Eureka for service registry and service discovery center, the latter is recommended. If it is another registry, then the former can be used.
3 business class
-
service
@FeignClient(value = "eureka-provide") public interface FeignService { @GetMapping(value = "/eureka/provide") String consume(a); } Copy the code
@ GetMapping value is request in the service address, @ FeignClient value is also the name of the note inside, or repeat the same sentence, this value is the need to call the server in the configuration file springcloud. Application. The value of the name, For the @FeignClient annotation, there are probably a few attributes, most of which are as their names suggest and may be covered in more detail later.
-
controller
@RestController public class FeignController { FeignService feignService; public FeignController(FeignService feignService) { this.feignService = feignService; } @GetMapping(value = "/feign/consume") public String consume(a) { returnfeignService.consume(); }}Copy the code
It’s straightforward, just like the normal project I wrote before, with less writing to inject the RestTemplate, then call the URL to return the result, etc.
4 test
Leave the rest untouched and directly start the FeignConsume9201 service. If the memory is insufficient, you only need to open the service registry EurekaServer8001, service provider EurekaProvide7001, and service consumer FeignConsume9201.
Then go to http://localhost:9201/feign/consume, can see the right call service provider side. You can see that the service provider can be invoked normally.
Note that when multiple interfaces in a project invoke the same service using @FeignClient, it means that a service can only be used once with @FeignClient.
At this point, you can follow the instructions to set Spring.main. allow-bean-mrit-overriding =true in the configuration file.
5 Service Degradation
Service degradation will come later, and I’ll mention it here, because OpenFiegn has quite a bit of integration. By the way, the concept of service degradation is simple, that is, when the service is not available, it is not possible to crash directly or directly report an error, there will be a backup plan.
Changing the service code
@FeignClient(value = "eureka-provide", fallback = FeignServiceFullbackImpl.class)
public interface FeignService {
@GetMapping(value = "/eureka/provide")
String consume(a);
}
Copy the code
The fullback attribute is the service degradation class that is required to implement the FeignService interface
public class FeignServiceFullbackImpl implements FeignService {
@Override
public String consume(a) {
return "Service degradation"; }}Copy the code
In order to simulate the scenario of service degradation, only EurekaServer8001 service registration discovery center and FeignConsume9201 need to be enabled, and other service providers do not need to be enabled.
However, if you do this, you will definitely get an error because there is an idea of service degradation, but it is not implemented, so you need to open Hystrix in the configuration file, which will be covered in more detail later. Also, implementation classes need to be registered with Spring in order to be found in the event of a downgrade.
feign:
hystrix:
enabled: true
Copy the code
@Component // Add an annotation
public class FeignServiceFullbackImpl implements FeignService {
/ / to omit
}
Copy the code
Or visit http://localhost:9201/feign/consume, also can see has been downgraded