spring cloud 学习(3) - feign入门
feign 是一个让rest服务调用更简洁的开源项目,很多介绍文章或书也称它为声明式REST调用。传统的web service中,通过引用wsdl来自动生成一些client的代理类(或stub代码),feign跟这个有点类似,但是更灵活。
先回顾一下,上节中service-consumer对服务的调用代码:
@GetMapping("/order/{userId}/{orderNo}")
public String findOrder(@PathVariable Integer userId, @PathVariable String orderNo) {
UserDTO user = restTemplate.getForEntity("http://SERVICE-PROVIDER-DEMO/user/" + userId, UserDTO.class).getBody();
if (user != null) {
return user.getUserName() + " 的订单" + orderNo + " 找到啦!";
} return "用户不存在!";
}
如果调用的参数比较多,调用的代码会充斥着很多拼装参数这样的代码,不太优雅。另外还有一个问题,通常为了安全起见,一些服务(或服务中的某些方法)可能要求认证后,才能调用,如果每个调用的地方,都要调用登录之类的服务来处理,这类与业务无关的代码就会大量侵入业务逻辑中,不好维护。
下面看看用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; @Configuration
public 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; @RestController
public 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
@EnableFeignClients
public class ServiceConsumer { public static void main(String[] args) {
SpringApplication.run(ServiceConsumer.class, args);
}
}
起作用的主要是@EnableFeignClients这个注解。
spring cloud 学习(3) - feign入门的更多相关文章
- Spring Cloud 学习 (三) Feign
新建 spring-cloud-eureka-feign-client Module pom <parent> <artifactId>spring-cloud-parent& ...
- Feign详细使用-Spring Cloud学习第四天(非原创)
文章大纲 一.Feign是什么二.Feign的基本实现三.Feign的继承特性四.Feign配置详解五.项目源码与参考资料下载六.参考文章 一.Feign是什么 前面几篇文章我们详细的介绍了Rib ...
- Spring Boot和Spring Cloud学习资源推荐
Spring Boot和Spring Cloud学习资源推荐 比较好的学习资源,分享一下. 1.Spring Boot官方文档:http://projects.spring.io/spring-b ...
- Spring Cloud 学习笔记(二)——Netflix
4 Spring Cloud Netflix Spring Cloud 通过自动配置和绑定到Spring环境和其他Spring编程模型惯例,为Spring Boot应用程序提供Netflix OSS集 ...
- Spring Cloud(Dalston.SR5)--Feign 声明式REST客户端
Spring Cloud 对 Feign 进行了封装,集成了 Ribbon 并结合 Eureka 可以实现客户端的负载均衡,Spring Cloud 实现的 Feign 客户端类名为 LoadBala ...
- spring cloud 学习(9) - turbine stream无法在eureka注册的解决办法
turbine是啥就不多解释了,初次接触的可以移步spring cloud 学习(4) - hystrix 服务熔断处理 拉到最后看一下,turbine stream默认情况下启动成功后,eureka ...
- Spring Cloud实战之初级入门(六)— 服务网关zuul
目录 1.环境介绍 2.api网关服务 2.1 创建工程 2.3 api网关中使用token机制 2.4 测试 2.5 小结 3.一点点重要的事情 1.环境介绍 好了,不知不觉中我们已经来到了最后一篇 ...
- Spring Cloud实战之初级入门(五)— 配置中心服务化与配置实时刷新
目录 1.环境介绍 2.配置中心服务化 2.1 改造mirco-service-spring-config 2.2 改造mirco-service-provider.mirco-service-con ...
- Spring Cloud实战之初级入门(四)— 利用Hystrix实现服务熔断与服务监控
目录 1.环境介绍 2.服务监控 2.1 加入依赖 2.2 修改配置文件 2.3 修改启动文件 2.4 监控服务 2.5 小结 3. 利用hystrix实现消费服务熔断 3.1 加入服务熔断 3.2 ...
随机推荐
- 阿里云Linux服务器安装 nginx+mysql+php
阿里云Linux服务器安装 nginx+mysql+php步骤1.登录服务器2.下载安装包3.将安装包上传到服务器的/home目录下 注:使用rz sz命令进行本地和服务器间的上传.下载,安装命令yu ...
- AngularJS开发指南:表达式
---恢复内容开始--- 表达式是类似Javascript的代码片段,通常在绑定中用到,写在双大括号中如{{表达式}}.表达式是用$parse方法来处理的. 下面是一些合法的AngularJS表达式 ...
- C根据排序字符串
#include<stdio.h> #include<string.h> #include <stdlib.h> #define STR_LEN_MAX 100 c ...
- shape-outside 矩形之外的另一种思路
http://docs.webplatform.org/wiki/css/properties/shape-outside
- HDU 4712 Hamming Distance(随机算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4712 解题报告:输入n个数,用十六进制的方式输入的,任意选择其中的两个数进行异或,求异或后的数用二进制 ...
- Servlet、ServletConfig、ServletContext深入学习
1.Servlet学习 1.Servlet生命周期 Servlet 加载—>实例化—>服务—>销毁. init(servletConfig):(经过自己的测试发现会先调用这个而不是i ...
- 深入理解C语言的函数调用过程 【转】
转自:http://blog.chinaunix.net/uid-25909619-id-4240084.html 原文地址:深入理解C语言的函数调用过程 作者:wjlkoorey258 本文 ...
- Parameters.Add和Parameters.AddWithValue
因为vs2013没有更新update 5所以Parameters.Add可以用Parameters.AddWithValue赋值无效 更新后可以. Parameters.AddWithValue的底层 ...
- [转] caffe视觉层Vision Layers 及参数
视觉层包括Convolution, Pooling, Local Response Normalization (LRN), im2col等层. 1.Convolution层: 就是卷积层,是卷积神经 ...
- Vue 虚拟Dom 及 部分生命周期初探
踏入前端,步入玄学 17年底至18年初附带做了vue的一些框架搭建,中途断断续续用了部分vue,时隔几个月后的工作又拾起vue,对于一些原理性的知识淡忘了,正值这段时间使用中遇到了一些坑,又拨了部分代 ...