Feign simple case

Feign Web Service client is a declarative, its aim is to make Web Service calls easier. Feign provides a template for HTTP requests, through a simple interface to write and insert annotations, you can define the parameters of good information HTTP request, format, address and so on.

Feign = Ribbon (called in sequence) + RestTemplate

Add pom.xml configuration

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.8 </version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

application.yml

spring:
  application:
    name: feign
server:
  port: 9093

eureka:
  client:
    service-url:
      defaultZone: http://eureka:eureka@eureka1:8761/eureka,http://eureka:eureka@eureka2:8761/eureka

management:
  endpoint:
    shutdown:
      enabled: true
  endpoints:
     web:
       exposure:
         include:
           - shutdown
           - info
           - health

UserService.java

package com.example.feign.service;

import com.example.feign.entity.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

@FeignClient("EUREKA-PROVIDER")
public interface UserService {

    @GetMapping("/getUser")
    public List<User> getUser();
}

RibbonApplication.java

package com.example.feign;

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

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class RibbonApplication {

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

}

 

Guess you like

Origin www.cnblogs.com/ShaoXin/p/11114341.html