SpringBoot -- Feign 聲明式web service

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 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)
feign:
    httpclient:
        enabled:true

代碼

代碼請移步 Github參考地址

如有疑問請加公衆號(K171),如果覺得對您有幫助請 github start
公衆號_k171

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章