SpringBoot使⽤Feign调⽤其他服务接⼝
使⽤SpringCloud的Feign组件能够为服务间的调⽤节省编码时间并提⾼开发效率,当服务本⾝不复杂时可以单独将该组件拿出使⽤。
引⼊依赖
<!-- mvnrepository/artifact/org.springframework.cloud/spring-cloud-starter-openfeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
引⼊SpringBoot打包的Feign依赖,需要注意的是Feign的版本与SpringBoot版本的对应关系,⽼版本的Feign并不叫openfeign。由于我是⽤的SpringBoot版本是2.0x,所以openfeign使⽤了2.0x版本,
若使⽤诸如2.1x或其他⾼版本的openfeign,在项⽬启动时会报“抽象⽅法错误”这类的异常。
编写接⼝作为服务调⽤⼊⼝
import com.bhz.demo.q.ProductReceiveReq;
import com.bhz.demo.sp.MemberPointBaseResp;
import com.bhz.demo.sp.UserPointResp;
import org.springframework.cloud.openfeign.FeignClient;
springcloud和springbootimport 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", name = "demoClient")
public interface DemoClient {
@RequestMapping(value = "/demo/user/{uuid}/{name}", method = RequestMethod.GET)
DemoBaseResp<DemoUserResp> 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){
}
}
在需要调⽤其他服务的模块中引⼊之前定义的接⼝即可。
关于调⽤https接⼝
调⽤https接⼝时会进⾏证书校验,若没有证书则会抛出No subject alternative names present异常,可以使⽤以下代码来绕过证书校验:
<!-- mvnrepository/artifact/org.springframework.cloud/spring-cloud-starter-netflix-ribbon -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
⾸先需要引⼊Ribbon依赖,在绕过证书的代码中存在⼀些需要被注⼊的类属于Ribbon。Ribbon的引⼊同样需要注意版本问题。
import feign.Client;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dition.ConditionalOnMissingBean;
import org.springframework.cloudflix.ribbon.SpringClientFactory;
import org.springframework.cloud.openfeign.ribbon.CachingSpringLoadBalancerFactory;
import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient;
import t.annotation.Bean;
import t.annotation.Configuration;
import javax.ssl.*;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import 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 = Instance("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.SocketFactory(), new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession sslSession) {
return true;
}
}),
cachingFactory, clientFactory);
}
}
之后是Feign的配置类,⽤来绕过https证书校验。
到此这篇关于SpringBoot使⽤Feign调⽤其他服务接⼝的⽂章就介绍到这了,更多相关SpringBoot Feign调⽤服务接⼝内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。