Spring Cloud 入门Eureka -Consumer服务消费(声明式Feign)(三)
Spring Cloud Feign是一套基于Netflix Feign实现的声明式服务调用客户端。它使得编写Web服务客户端变得更加简单。我们只需要通过创建接口并用注解来配置它既可完成对Web服务接口的绑定。它具备可插拔的注解支持,包括Feign注解、JAX-RS注解。它也支持可插拔的编码器和解码器。Spring Cloud Feign还扩展了对Spring MVC注解的支持,同时还整合了Ribbon和Eureka来提供均衡负载的HTTP客户端实现。
1、pom.xml,这里有所不同dependencyManagement有变化,之前的跑不通,依赖标红处为修改
<?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.example</groupId>
<artifactId>eurekaRibbon</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>eurekaRibbon</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<!-- <version>Dalston.SR3</version> -->
<version>Camden.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
2、主类:通过@EnableFeignClients
注解开启扫描Spring Cloud Feign客户端的功能
package com.example.demo; 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;
//扫描com.example.demo.service下的FeignClient
@EnableFeignClients(basePackages={"com.example.demo.service"})
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaFeignApplication { public static void main(String[] args) {
SpringApplication.run(EurekaFeignApplication.class, args);
}
}
3、ClientFeignController
package com.example.demo.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import com.example.demo.service.IFeign; @RestController
public class ClientFeignController { @Autowired
IFeign iFeign; @GetMapping("/consumer")
public String all() {
// 发起REST请求
return iFeign.all();
} }
4、IFeign 一个FeignClient 声明 服务提供者的信息
package com.example.demo.service; import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
//声明服务提供名
@FeignClient("eurekaClient")
public interface IFeign {
//指定要消费的接口名称
// @GetMapping("/consumer")
@RequestMapping(value="/all",method={RequestMethod.GET})
String all();
}
比上篇使用Ribbon @LoadBalanced,更加具有可配置性
如果使用Dalston版,FeignClient使用GetMapping 会报java.lang.NoClassDefFoundError: feign/Feign$Builder,网上查了很多 暂时没有找到原因, 故而 换成Camden版。
Spring Cloud 入门Eureka -Consumer服务消费(声明式Feign)(三)的更多相关文章
- Spring Cloud 入门Eureka -Consumer服务消费(一)
这里介绍:LoadBalancerClient接口,它是一个负载均衡客户端的抽象定义,下面我们就看看如何使用Spring Cloud提供的负载均衡器客户端接口来实现服务的消费. 引用之前的文章中构建的 ...
- Spring Cloud 入门Eureka -Consumer服务消费(Ribbon)(二)
前面一篇介绍了LoadBalancerClient来实现负载均衡, 这里介绍Spring cloud ribbon 1.ribbon Spring Cloud Ribbon 是一个基于Http和TCP ...
- Spring Cloud 入门教程(六): 用声明式REST客户端Feign调用远端HTTP服务
首先简单解释一下什么是声明式实现? 要做一件事, 需要知道三个要素,where, what, how.即在哪里( where)用什么办法(how)做什么(what).什么时候做(when)我们纳入ho ...
- Spring Cloud 入门教程(一): 服务注册
1. 什么是Spring Cloud? Spring提供了一系列工具,可以帮助开发人员迅速搭建分布式系统中的公共组件(比如:配置管理,服务发现,断路器,智能路由,微代理,控制总线,一次性令牌,全局锁 ...
- Spring Cloud 入门教程(二): 服务消费者(rest+ribbon)
在上一篇文章,讲了服务的注册和发现.在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的.Spring cloud有两种服务调用方式,一种是ribbon+r ...
- spring cloud 使用Eureka作为服务注册中心
什么是Eureka? Eureka是在AWS上定位服务的REST服务. Eureka简单示例,仅作为学习参考 在pom文件引入相关的starter(起步依赖) /*定义使用的spring cloud ...
- spring boot 2.0.3+spring cloud (Finchley)3、声明式调用Feign
Feign受Retrofix.JAXRS-2.0和WebSocket影响,采用了声明式API接口的风格,将Java Http客户端绑定到他的内部.Feign的首要目标是将Java Http客户端调用过 ...
- Spring Cloud Netflix Eureka【服务治理】
一.简介 二.使用 一.源码分析
- Spring Cloud 入门教程(七): 熔断机制 -- 断路器
对断路器模式不太清楚的话,可以参看另一篇博文:断路器(Curcuit Breaker)模式,下面直接介绍Spring Cloud的断路器如何使用. SpringCloud Netflix实现了断路器库 ...
随机推荐
- Missing artifact jdk.tools:jdk.tools:jar:1.6
今天从svn上面下载了一个mavan项目,出现Missing artifact jdk.tools:jdk.tools:jar:1.6 这个错误. 怎么解决了,在我的根pom.xml 下加入这个依赖就 ...
- 网站启用GZip压缩后,速度快了3倍!
GZip压缩,是一种网站速度优化技术,也是一把SEO优化利器,许多网站都采用了这种技术,以达到提升网页打开速度.缩短网页打开时间的目的. 本文是一篇研究性文章,将通过某个案例来给大家显示网站采用GZi ...
- HDU 5353—— Average——————【贪心+枚举】
Average Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total S ...
- Ajax实现跨域访问的三种方法
转载自:http://www.jb51.net/article/68424.htm 一.什么是跨域 我们先回顾一下域名地址的组成: http:// www . google : 8080 / scri ...
- 4.JavaScript
1.简介 JavaScript 是一种轻量级的编程语言,是一种动态类型.弱类型.基于原型的脚本语言. JavaScript,通常缩写为JS,是一种高级的,解释执行的编程语言.JavaScript是一门 ...
- js中各种距离clientWidth
1: offsetLeft.offsetTop.offsetHeight.offsetWidth 1.1 偏移参照——定位父级offsetParent offsetParent的定义是:与当前元素 ...
- laravel安装时openssl_encrypt() 的问题?Call to undefined function openssl_decrypt()
解决方案: 如果通过上面的步骤还是不能解决参考如下: 1.从php安装根目录中拷贝 libeay32.dll 和 ssleay32.dll 然后 覆盖掉apache/bin 下的对应文件(注意需要将h ...
- Android Studio使用OpenCV的配置方法
1.下载 进入官网(http://opencv.org/)下载OpenCV4Android并解压.目录结构如下图所示. 其中,sdk目录即是我们开发opencv所需要的类库:samples目录中存放着 ...
- Vue.js-项目目录结构解析
1.Vue初始化项目目录如下: 2.目录各项含义如下:
- python读入文档中的一行
从文件log_fusion中读入数据 方法1 f = open("log_fusion.txt") # 返回一个文件对象 line = f.readline() # 调用文件的 r ...