SpringBoot Feign声明web service

转自:http://blog.csdn.net/cwenao/article/details/54571816

Feign 声明式web service

Feign是一种基于HTTP的声明式、模板化的web service客户端 
Spring Cloud Feign 通过@FeignClient(“ribbonserver”),声明当前Interface为ribbonserver服务的客户端 
通过这种方式在开发调用远程服务时可以像调用本地服务一样,通过注解的方式调用

集成Feign

创建Feign module,引入spring-cloud-starter-feign

build.gradle

apply plugin: 'org.springframework.boot'
dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:" + springCloudVersion
        mavenBom "org.springframework.boot:spring-boot-starter:"+ springBootVersion
    }
}
dependencies {
    compile ('org.springframework.cloud:spring-cloud-starter-feign')
    compile('org.springframework.cloud:spring-cloud-starter-eureka')

    compile ('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-log4j2')
    compile ('org.springframework.boot:spring-boot-starter-thymeleaf')

    compile('org.apache.logging.log4j:log4j-1.2-api:'+ log4jAPIVersion)
    compile ('net.sourceforge.nekohtml:nekohtml:'+nekoHtmlVersion)
    testCompile ('org.springframework.boot:spring-boot-starter-test')
    testCompile group: 'junit', name: 'junit', version: '4.11'
}
configurations {
    all*.exclude module: 'spring-boot-starter-logging'
    all*.exclude module: 'logback-classic'
    all*.exclude module: 'log4j-over-slf4j'
    all*.exclude module: 'snappy-java'
}
jar {
    baseName = 'feignserver-bootcwenao'
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

配置application.yml

通过@FeignClient创建web service客户端FeignServer

@FeignClient("ribbonserver")
public interface FeignServer {
    @RequestMapping(value ="/testRealRibbon",method= RequestMethod.GET)
    String testRealRibbon(@RequestParam("content") String content);
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5

创建 Controller FeignController


/**
 * @author cwenao
 * @version $Id FeignController.java, v 0.1 2017-01-15 13:50 cwenao Exp $$
 */
@Controller
public class FeignController {
    @Autowired
    FeignServer feignServer;

    @RequestMapping("/testFeign")
    @ResponseBody
    public void testFeign(String content) {
        String ribbonStr = feignServer.testRealRibbon(content);
        System.out.println(ribbonStr);
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

通过 @EnableFeignClients启用FeignClient功能

/**
 * @author cwenao
 * @version $Id FeignServerApplication.java, v 0.1 2017-01-15 13:32 cwenao Exp $$
 */
@SpringBootApplication(scanBasePackages={"com.bootcwenao.feignserver"})
@EnableDiscoveryClient
@EnableFeignClients
public class FeignServerApplication {
    public static void main(String[] args) {
        new SpringApplicationBuilder(FeignServerApplication.class).web(true).run(args);
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

测试调用

  • ribbonserver 服务为上一节写Ribbon时的服务
  • @RequestParam(“content”) ,在使用@RequestMapping注解时需要参数注解否则,在指定method 为POST或者GET时未使用注解的参数将被忽略
  • @RequestParam(“content”)中“content”需要显示使用value以传递参数,不然可能会出错。
  • 依次启动 服务注册中心Ribbon ServerFeign Server

访问 Feign Server Controller http://localhost:8084/testFeign/content=Hello World

返回结果:Hello World for Spring Boot

启用Apache HTTP Client

替换默认的http client 
获取http连接池等 
build.gradle 引入 httpclient、feign httpclient 
application.yml中启用

compile ('org.apache.httpcomponents:httpclient:'+ httpclientVersion)
    compile ('com.netflix.feign:feign-httpclient:'+ feignHttpclientVersion)
 
 
  • 1
  • 2
feign:
    httpclient:
        enabled:true
 
 
  • 1
  • 2
  • 3

代码

代码请移步 Github参考地址

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值