前面我们已经将服务消费者和服务生产者都已经注册到了注册中心,并且注册中心也已经添加的安全认证,下面我们开始让消费者采用声明式的方式调用服务生产者。
首先我们需要在服务消费者里面添加 feign的依赖,当然也可以将依赖添加到我们的父项目中,这样我们的子项目都可以有这个依赖了,以后在每个子项目中都可以使用。springcloud认为一个微服务既可以是生产者也可以是消费者,因此我们的生产者也可以作为消费者去消费别的服务,也就是说我们的每个微服务都会需要使用feign去调用生产者。在这里我是将feign的依赖放在父项目的pom中
-
<dependency>
-
<groupId>org.springframework.cloud
</groupId>
-
<artifactId>spring-cloud-starter-openfeign
</artifactId>
-
</dependency>
添加完依赖之后我们在消费者的启动类上添加@EnableFeignClients注解,然后新建一个接口,具体的内容如下所示
-
@FeignClient(name="provider")
-
public
interface ProviderIndex {
-
-
@RequestMapping(method = RequestMethod.GET, value = "/index")
-
String index();
-
}
其中@FeignClient 注解的name属性指定你所调用的服务的服务名字,即在注册中心中注册的名字。然后在接口中定义方法,之后在方法上面添加springmvc的注解,这里要注意,不能使用@GetMapping类似的组合注解,必须使用@RequestMapping注解并指定请求方式和路径。配置完成之后,将此接口注入到controller中即可使用。
上面就是springCloud中使用feign的简单方式,在这里记录一下在配置feign中遇见的问题:
我最开始springBoot和springCloud采用的都是最新版本的,boot使用的是2.1.1.RELEASE版本,cloud使用的是Greenwich.RC1版本,在整合feign的过程中遇见了一个问题,无论怎样配置我定义的feign接口在spring容器中都找不到,因此在controller中无法注入,启动报错,找了很长时间的原因,并且按照网上说的在启动类上添加扫描路径也没有解决,最后把自己的boot版本和cloud版本都降了一个版本才实现,至于新版本为什么出错我还没有找到原因,各位知道原因的朋友看见了可以留言,大家一起学习。
还有就是如果我们自己定义的feign接口和启动类要是不在一个包下面,我们一定要配置启动类上注解@EnableFeignClients的
basePackages属性指定我们feign的包路径
至于想要完成更复杂的功能可以查看官网相关配置,我这里只是实现了最简单的使用feign调用生产者的接口
<li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
<use xlink:href="#csdnc-thumbsup"></use>
</svg><span class="name">点赞</span>
<span class="count"></span>
</a></li>
<li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{"mod":"popu_824"}"><svg class="icon" aria-hidden="true">
<use xlink:href="#icon-csdnc-Collection-G"></use>
</svg><span class="name">收藏</span></a></li>
<li class="tool-item tool-active is-share"><a href="javascript:;"><svg class="icon" aria-hidden="true">
<use xlink:href="#icon-csdnc-fenxiang"></use>
</svg>分享</a></li>
<!--打赏开始-->
<!--打赏结束-->
<li class="tool-item tool-more">
<a>
<svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
</a>
<ul class="more-box">
<li class="item"><a class="article-report">文章举报</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="person-messagebox">
<div class="left-message"><a href="https://blog.csdn.net/weixin_42606135">
<img src="https://profile.csdnimg.cn/6/F/8/3_weixin_42606135" class="avatar_pic" username="weixin_42606135">
<img src="https://g.csdnimg.cn/static/user-reg-year/2x/2.png" class="user-years">
</a></div>
<div class="middle-message">
<div class="title"><span class="tit"><a href="https://blog.csdn.net/weixin_42606135" data-report-click="{"mod":"popu_379"}" target="_blank">weixin_42606135</a></span>
</div>
<div class="text"><span>发布了17 篇原创文章</span> · <span>获赞 2</span> · <span>访问量 5340</span></div>
</div>
<div class="right-message">
<a href="https://im.csdn.net/im/main.html?userName=weixin_42606135" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
</a>
<a class="btn btn-sm bt-button personal-watch" data-report-click="{"mod":"popu_379"}">关注</a>
</div>
</div>
</div>
前面我们已经将服务消费者和服务生产者都已经注册到了注册中心,并且注册中心也已经添加的安全认证,下面我们开始让消费者采用声明式的方式调用服务生产者。