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

网友投稿 404 2023-01-26

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

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

引入依赖

org.springframework.cloud

spring-cloud-starter-openfeign

2.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 = "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

@SlbttzodrfwIf4j

public class DemoService {

@Autowired

private DemoClient demoClient;

public void getUser(Long id){

demoClient.getUser("123", "abc");

}

}

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

关bttzodrfwI于调用https接口

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

org.springframework.cloud

spring-cloud-starter-netflix-ribbon

2.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证书校验。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:解析spring
下一篇:免费api的使用(API免费接口)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~