SpringCloud-声明式Rest调用Feign(四)
前言:一般情况下我们通常使用RestTemplate来实现声明式远程调用,但是当参数过多,那么效率就会变得很低,并且难以维护,所以在微服务当中也有声明式Rest调用的组件Feign
一、Feign简介
Feign是Netflix开发的声明式、模板化的http客户端,Feign可以帮我们更加便捷、优雅地调用HTTP API。在SpringCloud中使用Feign非常简单,创建一个接口,并在接口上加上注解,就完成了声明式调用;
二、Feign与SpringCloud的整合简单使用
注:本次学习记录是基于之前的Eureka介绍和Ribbon介绍之上实践,这里只展示关键代码,其余代码可在代码示例中查看;
1、创建基于Eureka和Ribbon的服务端和两个客户端生产者、消费者:
Server:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.cn</groupId>
<artifactId>eureka-ribbon-server</artifactId>
<version>1.0-SNAPSHOT</version> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.RELEASE</version>
</parent> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 添加spring-boot的maven插件 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
pom.xml
server.port=8761
#注意:这两个配置eureka默认为true,要改成false,否则会报错,connot connect server
#表示是否将自己注册在EurekaServer上
eureka.client.register-with-eureka=false
#表示是否从EurekaServer获取注册信息
eureka.client.fetch-registry=false eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
application.properties
package com.cn; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; /**
* @program: springcloud-example
* @description: 启动类
* @author:
* @create: 2018-06-15 15:43
**/
@SpringBootApplication
@EnableEurekaServer
public class ServerApplication { public static void main(String[] args) {
SpringApplication.run(ServerApplication.class,args);
} }
ServerApplication.java
Client生产者:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.cn</groupId>
<artifactId>eureka-ribbon-client2</artifactId>
<version>1.0-SNAPSHOT</version> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.RELEASE</version>
</parent> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 添加spring-boot的maven插件 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
pom.xml
server.port=8763
spring.application.name=client-87
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
application.properties
package com.cn; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; /**
* @program: springcloud-example
* @description:
* @author:
* @create: 2018-06-15 16:05
**/
@SpringBootApplication
@EnableDiscoveryClient
public class ClientApplication { public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
} }
ClientApplication.java
package com.cn.contorller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody; /**
* @program: springcloud-example
* @description:
* @author:
* @create: 2018-06-15 16:12
**/
@Controller
public class ClientController { @GetMapping("/getUser")
@ResponseBody
public String getUser() {
System.out.println("获取用户成功");
return "{\"username\":\"张三\",\"age\":\"10\"}";
} }
ClientController.java
2、在消费者module中的maven依赖中添加相关依赖库,创建Feign访问接口,并注解,通过识别Eureka提供的应用名称,来找对应的请求路径:
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.cn</groupId>
<artifactId>eureka-feign-client</artifactId>
<version>1.0-SNAPSHOT</version> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.RELEASE</version>
</parent> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency> </dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 添加spring-boot的maven插件 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties:
server.port=8762
spring.application.name=client-8762
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
Feign接口:
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping; @FeignClient(name = "CLIENT-87")
public interface UserFeign { @RequestMapping("/getUser")
public String getUser(); }
FeignController:
package com.cn.controller; import com.cn.feign.UserFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; /**
* @program: springcloud-example
* @description:
* @author:
* @create: 2018-06-15 15:55
**/
@RestController
public class FeignController {
@Autowired
private LoadBalancerClient loadBalancerClient; @Autowired
private RestTemplate restTemplate; @Autowired
private UserFeign userFeign; @RequestMapping("/getUser")
public String getUser() {
return userFeign.getUser();
} @GetMapping("/loadInstance")
public String loadInstance() {
ServiceInstance choose = this.loadBalancerClient.choose("client-87");
System.out.println(choose.getServiceId()+":"+choose.getHost()+":"+choose.getPort());
return choose.getServiceId() + ":" + choose.getHost() + ":" + choose.getPort();
} }
在ClientApplication.java启动类中加入@EnableFeignClients注解:
package com.cn; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; /**
* @program: springcloud-example
* @description:
* @author:
* @create: 2018-06-15 15:51
**/
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ClientApplication { public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
} /**
* @Description:
* @Param:
* @return:
* @Author:
* @Date: 2018/6/15
*/
@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
} }
3、分别启动Server、Client生产者、Client消费者,并调用访问http://localhost:8761、http://localhost:8762/getUser,如图:


调用成功!
参考书籍:《SpringCloud与Docker微服务架构实战》周力著
代码示例:https://gitee.com/lfalex/springcloud-example( eureka-feign-client、 eureka-ribbon-server、 eureka-ribbon-client2这三个module)
SpringCloud-声明式Rest调用Feign(四)的更多相关文章
- SpringCloud 源码系列(6)—— 声明式服务调用 Feign
SpringCloud 源码系列(1)-- 注册中心 Eureka(上) SpringCloud 源码系列(2)-- 注册中心 Eureka(中) SpringCloud 源码系列(3)-- 注册中心 ...
- SpringCloud之声明式服务调用 Feign(三)
一 Feign简介 Feign是一种声明式.模板化的HTTP客户端,也是netflix公司组件.使用feign可以在远程调用另外服务的API,如果调用本地API一样.我们知道,阿里巴巴的doubbo采 ...
- SpringCloud开发学习总结(七)—— 声明式服务调用Feign(一)
在实践的过程中,我们会发现在微服务架构中实现客户端负载均衡的服务调用技术Spring Cloud Ribbon<SpringCloud开发学习总结(四)—— 客户端负载均衡Ribbon> ...
- 【Dalston】【第三章】声明式服务调用(Feign)
当我们通过RestTemplate调用其它服务的API时,所需要的参数须在请求的URL中进行拼接,如果参数少的话或许我们还可以忍受,一旦有多个参数的话,这时拼接请求字符串就会效率低下,并且显得好傻.那 ...
- 声明式服务调用Feign
什么是 Feign Feign 是种声明式.模板化的 HTTP 客户端(仅在 consumer 中使用). 什么是声明式,有什么作用,解决什么问题? 声明式调用就像调用本地方法一样调用远程方法;无 ...
- Spring Cloud第七篇 | 声明式服务调用Feign
本文是Spring Cloud专栏的第七篇文章,了解前六篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Cloud ...
- Spring Cloud 声明式服务调用 Feign
一.简介 在上一篇中,我们介绍注册中心Eureka,但是没有服务注册和服务调用,服务注册和服务调用本来应该在上一章就应该给出例子的,但是我觉得还是和Feign一起讲比较好,因为在实际项目中,都是使用声 ...
- Spring Cloud Feign 1(声明式服务调用Feign 简介)
Spring Cloud Feign基于Netflix Feign 同时整合了Spring Cloud Ribbon和Spring Cloud Hytrix,除了提供两者的强大功能外,它还提供了一种声 ...
- 004声明式服务调用Feign & 断路器Hystrix
1.POM配置 和普通Spring Boot工程相比,添加了Eureka Client.Feign.Hystrix依赖和Spring Cloud依赖管理 <dependencies> &l ...
- SpringCloud开发学习总结(七)—— 声明式服务调用Feign(二)
参数绑定 在上一章的示例中,我们使用Spring Cloud Feign实现的是一个不带参数的REST服务绑定.然而现实系统中的各种业务接口要比它复杂得多,我们有时会在HTTP的各个位置传入各种不同类 ...
随机推荐
- swift版的StringAttribute
swift版的StringAttribute 效果 源码 https://github.com/YouXianMing/Swift-StringAttribute // // StringAttrib ...
- [C++] 用Xcode来写C++程序[6] Name visibility
用Xcode来写C++程序[6] Name visibility 此小结包括了命名空间的一些使用细节 命名空间 #include <iostream> using namespace st ...
- Linux下安装Tomcat7
一.Tomcat7软件包下载 Tomcat下载地址http://tomcat.apache.org/download-70.cgi 下载完成后, 将软件包apache-tomcat-7.0.82.ta ...
- hibernate设置了not-null 为什么无效?
因为设错地方了! 错误--写在了property标签里 <property name="password" type="string" length=&q ...
- Hadoop HBase概念学习系列之HBase里的时间戳(二十六)
HBase集群要求每个节点的时间必须同步.HBase对于节点的时间扭曲(time skew)容忍度很低(这和HDFS是不一样的). 这主要是因为HBase需要使用系统时间来产生时间戳.如果系统时间不同 ...
- 第 6 章 C控制语句:循环
6.16.3 使用嵌套循环,按下面格式打印字母: F FE FED FEDC FEDCB FEDCBA #include <stdio.h> int main() { ; ); row ! ...
- Windows10 1709正式版WSL安装(以Ubuntu为例)
因为最近要使用Linux搭服务器,但是用远程的话延迟很烦,用双系统切换很麻烦,用虚拟机又会有点卡,刚好Windows10最近更新了正式版的WSL(windows下的Linux子系统),所以就想尝试一下 ...
- iOS 网络缓存总结
一.缓存策略: 1.缓存策略的配置: 缺省缓存策略的存储策略需要服务器的响应配置: 缺省缓存策略的使用需要请求端的配置: 2.缓存策略的缺陷: 移动端比较通用的缓存策略是先使用缓存同时更新本地数据: ...
- macaca常见错误排查
1.Error: cannot resolve path (or pattern) 'macaca-test' 这是因为执行run命令的时候未进入用例目录 C:\Users\Tony\node_mod ...
- Node.js框架之Egg.js
Node.js是我前段时间接触的一个JavaScript的服务端语言,感觉还是挺有意思的. 也许有人说,你学这么多,学的过来吗?或者说学的太多,专而不精,有必要这样吗? 其实,我个人认为,自从我进入I ...