一.关于feigin
feigin是一种模板化,声明式的http客户端,feign可以通过注解绑定到接口上来简化Http请求访问。当然我们也可以在创建Feign对象时定制自定义解码器(xml或者json等格式解析)和错误处理。
二.添加SpringCloud对feign的支持
gradle配置:
compile(‘org.springframework.cloud:spring-cloud-starter-feign‘)
feigin最基本使用方法:
1 interface GitHub { 2 @RequestLine("GET /repos/{owner}/{repo}/contributors") 3 List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo); 4 } 5 6 static class Contributor { 7 String login; 8 int contributions; 9 } 10 11 public static void main(String... args) { 12 GitHub github = Feign.builder() 13 .decoder(new GsonDecoder()) 14 .target(GitHub.class, "https://api.github.com"); 15 16 // Fetch and print a list of the contributors to this library. 17 List<Contributor> contributors = github.contributors("OpenFeign", "feign"); 18 for (Contributor contributor : contributors) { 19 System.out.println(contributor.login + " (" + contributor.contributions + ")"); 20 } 21 }
feign发送json与xml的格式的http请求:
1 interface LoginClient { 2 3 @RequestLine("POST /") 4 @Headers("Content-Type: application/xml") 5 @Body("<login \"user_name\"=\"{user_name}\" \"password\"=\"{password}\"/>") 6 void xml(@Param("user_name") String user, @Param("password") String password); 7 8 @RequestLine("POST /") 9 @Headers("Content-Type: application/json") 10 // json curly braces must be escaped! 11 @Body("%7B\"user_name\": \"{user_name}\", \"password\": \"{password}\"%7D") 12 void json(@Param("user_name") String user, @Param("password") String password); 13 }
注意示例中需要添加对gson的支持
feign发送https信任所有证书的代码:
1 final TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() { 2 @Override 3 public void checkClientTrusted( 4 java.security.cert.X509Certificate[] chain, 5 String authType) { 6 } 7 8 @Override 9 public void checkServerTrusted( 10 java.security.cert.X509Certificate[] chain, 11 String authType) { 12 } 13 14 @Override 15 public java.security.cert.X509Certificate[] getAcceptedIssuers() { 16 return null; 17 } 18 }}; 19 final SSLContext sslContext = SSLContext.getInstance("TLSv1"); 20 sslContext.init(null, trustAllCerts, 21 new java.security.SecureRandom()); 22 // Create an ssl socket factory with our all-trusting manager 23 final SSLSocketFactory sslSocketFactory = sslContext 24 .getSocketFactory(); 25 Feign.builder().client(new Client.Default(sslSocketFactory, (s, sslSession) -> true));
三.在SpringCloud中使用Feign
比如说注册中心有如下服务:
1)application.yml的配置:
spring: application: name: demo-consumer eureka: client: service-url: defaultZone: http://localhost:8080/eureka,http://localhost:8081/eureka server: port: 8090
2)创建接口
1 package com.bdqn.lyrk.consumer.demo.api; 2 3 import org.springframework.cloud.netflix.feign.FeignClient; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 6 @FeignClient("demo") 7 public interface DemoConfigService { 8 9 @RequestMapping("/demo.do") 10 String demoService(); 11 }
注意在接口上加上注解:@FeignClient("demo") 注解里的参数是在eureka注册的服务名
3)编写启动类:
package com.bdqn.lyrk.consumer.demo; import com.bdqn.lyrk.consumer.demo.api.DemoConfigService; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.feign.EnableFeignClients; import org.springframework.context.ConfigurableApplicationContext; @EnableDiscoveryClient @EnableFeignClients @SpringBootApplication public class DemoConsumerProvider { public static void main(String[] args) { ConfigurableApplicationContext applicationContext = SpringApplication.run(DemoConsumerProvider.class, args); DemoConfigService demoConfigService = applicationContext.getBean(DemoConfigService.class); System.out.println(demoConfigService.demoService()); } }
注意在启动类上加上@EnableFeignClients来开启Feign功能
运行后输出:
此时我们可以发现使用feign客户端我们访问服务的代码简介了好多