SpringBoot使用Feign调用其他服务接口

这篇文章主要介绍了SpringBoot使用Feign调用其他服务接口,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

使用SpringCloud的Feign组件能够为服务间的调用节省编码时间并提高开发效率,当服务本身不复杂时可以单独将该组件拿出使用。

引入依赖

  org.springframework.cloudspring-cloud-starter-openfeign2.0.4.RELEASE

引入SpringBoot打包的Feign依赖,需要注意的是Feign的版本与SpringBoot版本的对应关系,老版本的Feign并不叫openfeign。由于我是用的SpringBoot版本是2.0x,所以openfeign使用了2.0x版本,若使用诸如2.1x或其他高版本的openfeign,在项目启动时会报“抽象方法错误”这类的异常。

编写接口作为服务调用入口

 import com.bhz.demo.client.domain.req.ProductReceiveReq; import com.bhz.demo.client.domain.resp.MemberPointBaseResp; import com.bhz.demo.client.domain.resp.UserPointResp; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; /** * @Author guomz * @create 2021/3/15 14:50 */ @FeignClient(url = "www.123.com", name = "demoClient") public interface DemoClient { @RequestMapping(value = "/demo/user/{uuid}/{name}", method = RequestMethod.GET) DemoBaseResp getUser(@PathVariable("uuid") String uuid, @PathVariable("name") String name); @RequestMapping(value = "/demo/buy", method = RequestMethod.POST) DemoBaseResp buyProduct(DemoBuyReq req); } 

Feign的服务调用编写类似mybatis的dao接口,接口上方需要标注@FeignClient注解,该注解有url、name、value三个重要参数。其中name与value等效,必须填写一个。在微服务环境下name或value填写用于被注册中心发现的服务名,例如调用的用户服务叫userService则此处填写userService,此使url可以不填写,因为已经指定了调用方。url则是直接指定服务的全路径,若同时填写url与name,则以url为准,name便被当作当前客户端的名称。

上面的示例并不属于复杂的微服务环境,所以采用直接指定url来调用其他服务。

方法定义上与controller基本一致,需要注意post方法不能传递多个参数,需要用map或对象进行封装。

调用服务

 @Service @Slf4j public class DemoService { @Autowired private DemoClient demoClient; public void getUser(Long id){ demoClient.getUser("123", "abc"); } } 

在需要调用其他服务的模块中引入之前定义的接口即可。

关于调用https接口

调用https接口时会进行证书校验,若没有证书则会抛出No subject alternative names present异常,可以使用以下代码来绕过证书校验:

  org.springframework.cloudspring-cloud-starter-netflix-ribbon2.0.4.RELEASE

首先需要引入Ribbon依赖,在绕过证书的代码中存在一些需要被注入的类属于Ribbon。Ribbon的引入同样需要注意版本问题。

 import feign.Client; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.cloud.netflix.ribbon.SpringClientFactory; import org.springframework.cloud.openfeign.ribbon.CachingSpringLoadBalancerFactory; import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.net.ssl.*; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.security.cert.X509Certificate; /**feign client配置 * @Author guomz * @create 2021/3/16 9:52 */ @Configuration public class FeignConfiguration { /** * 调用https接口时绕过ssl证书验证 * @param cachingFactory * @param clientFactory * @return * @throws NoSuchAlgorithmException * @throws KeyManagementException */ @Bean @ConditionalOnMissingBean public Client feignClient(@Qualifier("cachingLBClientFactory") CachingSpringLoadBalancerFactory cachingFactory, SpringClientFactory clientFactory) throws NoSuchAlgorithmException, KeyManagementException { SSLContext ctx = SSLContext.getInstance("TLSv1.2"); X509TrustManager tm = new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] x509Certificates, String s) { } @Override public void checkServerTrusted(X509Certificate[] x509Certificates, String s) { } @Override public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } }; ctx.init(null, new TrustManager[]{tm}, null); return new LoadBalancerFeignClient(new Client.Default(ctx.getSocketFactory(), new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession sslSession) { return true; } }), cachingFactory, clientFactory); } } 

之后是Feign的配置类,用来绕过https证书校验。

以上就是SpringBoot使用Feign调用其他服务接口的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java