博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring cloud 学习(3) - feign入门
阅读量:6859 次
发布时间:2019-06-26

本文共 3476 字,大约阅读时间需要 11 分钟。

 是一个让rest服务调用更简洁的开源项目,很多介绍文章或书也称它为声明式REST调用。传统的web service中,通过引用wsdl来自动生成一些client的代理类(或stub代码),feign跟这个有点类似,但是更灵活。

先回顾一下,中service-consumer对服务的调用代码:

1    @GetMapping("/order/{userId}/{orderNo}")2     public String findOrder(@PathVariable Integer userId, @PathVariable String orderNo) {3         UserDTO user = restTemplate.getForEntity("http://SERVICE-PROVIDER-DEMO/user/" + userId, UserDTO.class).getBody();4         if (user != null) {5             return user.getUserName() + " 的订单" + orderNo + " 找到啦!";6         }7  8         return "用户不存在!";9     }
View Code

如果调用的参数比较多,调用的代码会充斥着很多拼装参数这样的代码,不太优雅。另外还有一个问题,通常为了安全起见,一些服务(或服务中的某些方法)可能要求认证后,才能调用,如果每个调用的地方,都要调用登录之类的服务来处理,这类与业务无关的代码就会大量侵入业务逻辑中,不好维护。

下面看看用feign如何改进:

一、添加依赖引用

compile 'org.springframework.cloud:spring-cloud-starter-feign'

 

二、定义feignClient接口

package com.cnblogs.yjmyzz.spring.cloud.study.service.client;import com.cnblogs.yjmyzz.spring.cloud.study.dto.UserDTO;import org.springframework.cloud.netflix.feign.FeignClient;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;@FeignClient(name = "service-provider-demo", configuration = BasicAuthConfiguration.class)public interface UserFeignClient {    @RequestMapping(value = "/user/{id}", method = RequestMethod.GET)    UserDTO findUser(@PathVariable("id") Integer userId);}

这里面一个BasicAuthConfiguration类,也是自己写的

package com.cnblogs.yjmyzz.spring.cloud.study.service.client;import feign.auth.BasicAuthRequestInterceptor;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class BasicAuthConfiguration {    @Bean    public BasicAuthRequestInterceptor basicAuthorizationInterceptor() {        return new BasicAuthRequestInterceptor("app01", "passwd01");    }}

这上面的app01, passwd01,就是服务端分配的用户名及密码

附:服务提供方的application.yml中可参考下面这样设置

security:  basic:    enabled: true  user:    name: app01    password: passwd01

 

三、feignClient的使用

package com.cnblogs.yjmyzz.spring.cloud.study.service.controller;import com.cnblogs.yjmyzz.spring.cloud.study.dto.UserDTO;import com.cnblogs.yjmyzz.spring.cloud.study.service.client.UserFeignClient;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class OrderController {    @Autowired    private UserFeignClient userFeignClient;    @GetMapping("/order/{userId}/{orderNo}")    public String findOrder(@PathVariable Integer userId, @PathVariable String orderNo) {        UserDTO user = userFeignClient.findUser(userId);        if (user != null) {            return user.getUserName() + " 的订单" + orderNo + " 找到啦!";        }        return "用户不存在!";    }}  

  

最后,main入口类上要增加一个注解

package com.cnblogs.yjmyzz.spring.cloud.study.service;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;@EnableDiscoveryClient@SpringBootApplication@EnableFeignClientspublic class ServiceConsumer {    public static void main(String[] args) {        SpringApplication.run(ServiceConsumer.class, args);    }}

起作用的主要是@EnableFeignClients这个注解。

转载地址:http://mmtyl.baihongyu.com/

你可能感兴趣的文章
过游戏保护NP或TP的几种方法和思路
查看>>
equals和hashcode为什么要一起重写
查看>>
模态与非模态对话框的问题
查看>>
httpclient 备注 控制连接时间及多线程错误
查看>>
地对地导弹地对地导弹地对地导弹
查看>>
浏览器根对象window之performance
查看>>
让div 充满整个body
查看>>
常用排序算法
查看>>
程序员保持快乐活跃的6个好习惯(转)
查看>>
找工作的一些感悟——前端小菜的成长
查看>>
jSON Call can throw but it is not marked with try
查看>>
基于bootstrap的jQuery多级列表树插件 treeview
查看>>
node06
查看>>
笔试题[转]
查看>>
图片轮换
查看>>
PHP数据结构练习笔记--栈
查看>>
JSON对象配合jquery.tmpl.min.js插件,手动攒出一个table
查看>>
编译安装QEMU 及卸载
查看>>
关于php-fpm与nginx进程重载的坑
查看>>
P2S、P2P、P2SP之对比
查看>>