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'
}
配置application.yml
通过@FeignClient创建web service客户端FeignServer
@FeignClient("ribbonserver")
public interface FeignServer {
@RequestMapping(value ="/testRealRibbon",method= RequestMethod.GET)
String testRealRibbon(@RequestParam("content") String content);
}
创建 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);
}
}
通过 @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);
}
}
测试调用
- ribbonserver 服务为上一节写Ribbon时的服务
- @RequestParam("content") ,在使用@RequestMapping注解时需要参数注解否则,在指定method 为POST或者GET时未使用注解的参数将被忽略
- @RequestParam("content")中"content"需要显示使用value以传递参数,不然可能会出错。
- 依次启动 服务注册中心、Ribbon Server、Feign 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)
feign:
httpclient:
enabled:true
代码
代码请移步 Github参考地址
如有疑问请加公众号(K171),如果觉得对您有帮助请 github start
网友评论