SpringCloud of Feign service consumers

Feign Profile

Feign is a pseudo Http client a declarative, it makes writing easier Http client. 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, and Eureka and combined, the default implementation of load balancing.

  • Feign uses annotation-based interface
  • Feign integrated ribbon, with load balancing capabilities
  • Integrated Hystrix, has the ability to fuse

Feign writing service

Creating a model project as a service to consumers, namely eureka-feign-client.
Import dependence

<dependency>
    <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> 

At the launch of the program plus class @EnableFeignClients comment open Feign features:

 1 import org.springframework.boot.SpringApplication;
 2 import org.springframework.boot.autoconfigure.SpringBootApplication;
 3 import org.springframework.cloud.client.loadbalancer.LoadBalanced;
 4 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
 5 import org.springframework.cloud.openfeign.EnableFeignClients;
 6 import org.springframework.context.annotation.Bean;
 7 import org.springframework.web.client.RestTemplate;
 8 
 9 @SpringBootApplication
10 @EnableEurekaClient
11 // 开启Feign的功能
12 @EnableFeignClients
13 public class EurekaFeignClientApplication {
14 
15     public static void main(String[] args) {
16         SpringApplication.run(EurekaFeignClientApplication.class, args);
17     }
18 }
View Code

Profile application.yml as follows:

server:
  port: 8083 spring: application: name: eureka-feign-client eureka: client: serviceUrl: defaultZone: http://localhost:9100/eureka/ 

Feign a defined interface through @FeignClient(“服务名”), to specify which service calls.

import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; // 申明这是一个Feign客户端,并且指明服务id @FeignClient(value = "eureka-client") public interface FeignClientInter { // 这里定义了类似于SpringMVC用法的方法,就可以进行RESTful方式的调用了 @GetMapping(value = "/hello") String sayHelloFromClient(); } 

Feign customers through the above-defined end to consumer services.

 1 import com.yq.feign.service.FeignClientInter;
 2 import org.springframework.beans.factory.annotation.Autowired;
 3 import org.springframework.web.bind.annotation.GetMapping;
 4 import org.springframework.web.bind.annotation.RestController;
 5 
 6 @RestController
 7 public class HelloController {
 8 
 9     @Autowired(required = false)
10     FeignClientInter feignClientInter;
11 
12     @GetMapping("/feign")
13     public String demo() {
14         return feignClientInter.sayHelloFromClient();
15     }
16 }

 

test:
Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/const-/p/11454565.html