zoukankan      html  css  js  c++  java
  • SpringBoot Feign接口方式调用服务

    1、前文接 SpringBoot Eureka集群配置

    2、EurekaConsumer_Feign_9002

    添加openfeign依赖

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

    添加yml配置

    eureka:
      client:
        register-with-eureka: false
        serviceUrl: #注册中心的注册地址
          defaultZone: http://127.0.0.1:7001/eureka/
    server:
      port: 9002  #服务端口号
    spring:
      application:
        name: service-consumer #服务名称--调用的时候根据名称来调用该服务的方法
    

    EurekaConsumer_Feign_9002启动类添加@EnableFeignClients扫描包

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    import org.springframework.cloud.openfeign.EnableFeignClients;
    
    @SpringBootApplication
    @EnableEurekaClient
    @EnableFeignClients(basePackages = {"com.kikyo"})
    public class EurekaConsumer_Feign_9002 {
        public static void main(String[] args) {
            SpringApplication.run(EurekaConsumer_Feign_9002.class, args);
        }
    }
    

    TestService这个接口是从Eureka服务(SERVICE-PROVIDER)拿实现类的

    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.stereotype.Component;
    import org.springframework.web.bind.annotation.GetMapping;
    
    @Component
    @FeignClient("SERVICE-PROVIDER")
    public interface TestService {
        @GetMapping("/get")
        String get();
    }
    

    接收请求

    import com.kikyo.service.TestService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    
    @RestController
    public class TestController {
        @Autowired
        private TestService testService = null;
    
        @RequestMapping("/get")
        public String getUser() {
            return testService.get();
        }
    }
    
  • 相关阅读:
    【hihocoder1255 Mysterious Antiques in Sackler Museum】构造 枚举
    【hihocoder 1257 Snake Carpet】构造
    【HDU 5572 An Easy Physics Problem】计算几何基础
    【hihocoder 1258 Osu! Master】
    Coder-Strike 2014
    [不完全动态凸包]SGU277
    [成都七中]GCD
    [某模拟赛]鸡腿の乒乓
    [TCSRM518Div1]Nim
    BZOJ3289【莫队算法+树状数组+离散化】
  • 原文地址:https://www.cnblogs.com/kikyoqiang/p/14675288.html
Copyright © 2011-2022 走看看