zoukankan      html  css  js  c++  java
  • 使用Feign访问接口

     添加主要依赖

    使用Feign访问接口的配置,如果服务不在Eureka上,可以不加Eureka的依赖,用在FeignClient上指定url的方式访问

    dependencies {
        compile('org.springframework.boot:spring-boot-starter-web')
        compile('org.springframework.cloud:spring-cloud-starter-feign')
        compile('org.springframework.cloud:spring-cloud-starter-eureka')
        compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.16.12'
        testCompile('org.springframework.boot:spring-boot-starter-test')
    }

    gradle配置文件

    
    buildscript {
        ext {
            springBootVersion = '1.5.8.RELEASE'
        }
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        }
    }
    
    apply plugin: 'java'
    apply plugin: 'org.springframework.boot'
    
    group = 'com.example'
    version = '0.0.1-SNAPSHOT'
    sourceCompatibility = 1.8
    
    repositories {
        mavenCentral()
    }
    
    
    dependencies {
        compile('org.springframework.boot:spring-boot-starter-web')
        compile('org.springframework.cloud:spring-cloud-starter-feign')
        compile('org.springframework.cloud:spring-cloud-starter-eureka')
        testCompile('org.springframework.boot:spring-boot-starter-test')
    }
    
    ext {
        springCloudVersion = 'Dalston.SR4'
    }
    dependencyManagement {
        imports {
            mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
        }
    }

    配置文件中Eureka地址

    (下面是yml文件的配置格式)

    eureka:
      client:
        enabled: true #访问eureka服务时要打开
        serviceUrl:
          defaultZone: http://localhost:8761/eureka/

    Application主类添加注解

    @EnableDiscoveryClient
    @EnableFeignClients
    @SpringBootApplication
    public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }

    编写FeignClient接口

    dp-dealer-shop是指定的应用名,不用Eureka的话不可以这样做,可以用指定一个Name和Url的方式调用接口
    接口中不支持@GetMapping这样的简写,要用@RequestMapping的写法
    @FeignClient("dp-dealer-shop")
    
    public interface Feign {
        @RequestMapping(method = RequestMethod.GET, value = "/api/quotation/v0/sendLargeRegionMail")
        List<AuditEmailContentDto> getLargeRegionMailContent();
    }
  • 相关阅读:
    tyflow birth节点
    tyflow雨滴在物体上滑落测试
    【bootstrap】如何在js中动态修改提示冒泡(Tooltips)的显示内容
    解决office自动更新失败,错误代码0xc0000142
    hosts文件路径(Windows)
    【微信测试版】支持安卓平板和手机同时登录
    【javascript】canvas画布涂鸦及保存图片到本地
    【python】图片批量压缩(多线程)
    【python】提取pdf文件中的所有图片
    【python】计算程序运行所消耗的总时间
  • 原文地址:https://www.cnblogs.com/longling2344/p/7764540.html
Copyright © 2011-2022 走看看