Spring Cloud学习系列第二章:使用Feign调用服务
来源:互联网 发布:橘子 芥川龙之介 知乎 编辑:程序博客网 时间:2024/05/20 21:18
一、Feign简介
Feign是一个声明式的Web服务客户端。这使得Web服务客户端的写入更加方便 要使用Feign创建一个界面并对其进行注释。它具有可插入注释支持,包括Feign注释和JAX-RS注释。Feign还支持可插拔编码器和解码器。Spring Cloud增加了对Spring MVC注释的支持,并使用Spring Web中默认使用的HttpMessageConverters
。Spring Cloud集成Ribbon和Eureka以在使用Feign时提供负载均衡的http客户端。
上面这段话是官方文档,说的有些啰嗦,简单来说,就是Feign帮我们准备了一套调用Spring Cloud注册服务中心里面的服务,不用再去使用Apache httpclient之类的http客户端工具了,还提供了很多功能,编解码、负载均衡等。
二、创建Feign项目
我们的目的是使用上一章发布的服务,而且是到Eureka Server中去访问服务,所以我们的Feign项目就是比Eureka Server项目多一个Feign的依赖,内容如下。
<?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.nan</groupId><artifactId>feigndemo</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>feigndemo</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.6.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><spring-cloud.version>Dalston.SR2</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-feign</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</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>${spring-cloud.version}</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>
项目启动入口要激活Feign client,内容如下。
package com.nan.feigndemo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.cloud.netflix.feign.EnableFeignClients;@SpringBootApplication@EnableEurekaClient@EnableFeignClientspublic class FeigndemoApplication { public static void main(String[] args) { SpringApplication.run(FeigndemoApplication.class, args); }}
接下来就要声明Feign客户端了,目的就是指向我们发布的服务,首先看内容。
package com.nan.feigndemo.client;import org.springframework.cloud.netflix.feign.FeignClient;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;@FeignClient("HelloService")public interface HelloServiceClient { @RequestMapping("/hello") String hello(@RequestParam("name") String name);}
很明显,这是一个interface,是一个声明式使用方式。重要的注解,FeignClient,"HelloService"属性就是我们发布的服务的spring.application.name。下面的接口方法就和我们发布的Rest接口一致了,另外RequestMapping不要用在interface上,不然会被spring mvc容器扫描到。
然后编写我们自己的项目业务,调用这个客户端,内容如下。
package com.nan.feigndemo.controller;import com.nan.feigndemo.client.HelloServiceClient;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class FeignDemoController { @Autowired private HelloServiceClient helloServiceClient; @RequestMapping("/feigndemo") public String feignDemo(@RequestParam("name") String name) { return helloServiceClient.hello(name); }}
简单的controller,就不多废话了。重要的是下面的项目配置。
server: port: 8080spring: application: name: FeignDemoeureka: client: serviceUrl: defaultZone: http://node01:8081/eureka/,http://node02:8082/eureka/
server.port和application.name是我们项目自己的,eureka.client.serviceUrl就是我们的Eureka Server集群模式的地址,然后启动项目,记得Eureka server和HelloService也要启动。然后浏览器访问我们的feigndemo项目,浏览器输入http://localhost:8080/feigndemo?name=秦始皇,浏览器会出现 Hello 秦始皇, this client port is 9091,再刷新页面,会出现 Hello 秦始皇, this client port is 9092。OK,服务调用,负载均衡都已实现,真是太佩服他们了,一步一步简化我们的开发。
以上就是笔者目前所学,还是很初级的内容,如果有什么错误和遗漏,还请见谅并指正,不胜感激。
- Spring Cloud学习系列第二章:使用Feign调用服务
- spring cloud feign 调用服务注意问题
- Spring Cloud 声明式服务调用 Feign
- 声明式服务调用:Spring Cloud Feign
- spring cloud Feign(声明式服务调用)
- 使用Spring Cloud Feign远程调用
- Spring Cloud feign使用
- 06.Spring Cloud学习笔记之声明式服务调用组件Feign
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- Spring Cloud入门教程(三):声明式服务调用(Feign)
- Spring Cloud中声明式服务调用Feign
- Spring Cloud学习 | 第二章 | 服务提供者
- 【Spring Cloud】Feign接口调用
- Spring Cloud-使用feign来消费Restful服务
- JAVA实现简单的双向LinkedList
- Oracle 软件测试工程师日常工作中常用到的查询语句(除功能业务查询语句)
- 使用Java网络编程创建基于TCP协议的Server和Client
- Copying Books (二分)
- 心情
- Spring Cloud学习系列第二章:使用Feign调用服务
- 【算法系列】——Java实现插入&&希尔排序
- 如何在CSDN博客中的所贴的代码进行【代码块】显示
- 【POJ 3468 A Simple Problem with Integers】 线段树
- 杂谈
- poj 2566 Bound Found 尺取法 / lower_bound
- Django建教育平台(六)--首页和登录页面配置
- UVALive 6886 Golf Bot 桶排+剪枝
- CentOS 使用yum命令安装出现错误提示:Loaded plugins: fastestmirror, refresh-packagekit, security