【图文经典版】声明式调用服务SpringCloud之Feign实例讲解
2017-12-27 00:00
1591 查看
注意:在上两篇 创建SpringCloud服务,并向注册中心注册自己 、SpringCloud创建Eureka注册中心
的基础上,我们建立hello-consumer服务
github仓库地址:https://github.com/pocher/SpringCloudEurkaDemo
访问:localhost:1110,这时是没有服务可用的。
github仓库地址:https://github.com/pocher/SpringCloudEurka-helloservice1
刷新:localhost:1110,这时发现有一个hello-service1服务,就是我们启动的hello-service1服务。
刷新:localhost:1110,这时发现hello-consumer服务也在服务列表里。
有红色英文字体提示
没有红色粗体英文提示
看到返回的数据
B.然后通过@RequestMapping匹配路径(/feign-consumer)到指定Controller中的
方法helloConsumer()
的基础上,我们建立hello-consumer服务
第一部分:创建SpringBoot工程
1、新建项目
2、选择创模板
然后,在弹出框中选择Spring Initializr,并使用默认的模板‘http://start.spring.io’。点击下一步(Next)3、填写公司信息
4、勾选依赖
5、选择项目储存路径
6、Pom文件如下
<?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.learning.pocher</groupId> <artifactId>hello-consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>hello-consumer</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.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>Edgware.RELEASE</spring-cloud.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>${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>
第二部分:修改和添加文件
1、修改application.properties文件
spring.application.name=hello-consumer spring.port=2221 eureka.client.service-url.defaultZone=http://localhost:1110/eureka/
2、修改启动类,添加注解
添加注解@EnableDiscoveryClient和@EnableFeignClients3、添加service层
package com.learning.pocher.helloconsumer.feignservice; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; @FeignClient("hello-service1") public interface ServiceClient { @RequestMapping("/callToXiaoAI") String getHelloService1(); }
4、添加Controller层
package com.learning.pocher.helloconsumer.controller; import com.learning.pocher.helloconsumer.feignservice.ServiceClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloConsumer { @Autowired ServiceClient serviceClient; @RequestMapping("/feign-consumer") public String helloConsumer(){ String returnMessage = serviceClient.getHelloService1(); System.out.println(returnMessage); return "returnMessage"; } }
5、目录结构
第二部分:依次启动各个服务
注意:启动顺序不要错了哦,先启动注册中心eureka-center,然后启动服务提供者hello-service1,最后启动服务消费者hello-consumer1、启动注册中eureka-center服务
没有此工程的参考 SpringCloud创建Eureka注册中心 这篇文章,或者直接下载此eureka-center工程github仓库地址:https://github.com/pocher/SpringCloudEurkaDemo
访问:localhost:1110,这时是没有服务可用的。
2、启动hello-service1服务
没有此工程的参考 创建SpringCloud服务,并向注册中心注册自己 这篇文章,或者直接下载此hello-service1工程github仓库地址:https://github.com/pocher/SpringCloudEurka-helloservice1
刷新:localhost:1110,这时发现有一个hello-service1服务,就是我们启动的hello-service1服务。
3、启动hello-consumer工程
刷新:localhost:1110,这时发现hello-consumer服务也在服务列表里。
有红色英文字体提示
没有红色粗体英文提示
第三部分:Feign服务的调用
1、访问hello-consumer工程
访问地址:http://localhost:2221/feign-consumer看到返回的数据
2、请求流程分析:
A.浏览器发送请求,通过指定域名和端口(http://localhost:2221)匹配服务,B.然后通过@RequestMapping匹配路径(/feign-consumer)到指定Controller中的
方法helloConsumer()
第四部分:项目下载
github仓库地址:https://github.com/pocher/SpringCloudEurka-helloconsumer相关文章推荐
- SpringCloud之声明式服务调用Spring Cloud Feign实例
- 干货分享微服务spring-cloud(5.声明式服务调用feign)
- 笔记:Spring Cloud Feign 声明式服务调用
- Spring Cloud 声明式服务调用 Feign
- 第九章 Spring Cloud Feign声明式调用服务
- Spring Cloud中声明式服务调用Feign
- Spring Cloud Feign 声明式服务调用
- Spring Cloud 入门教程(六): 用声明式REST客户端Feign调用远端HTTP服务
- spring cloud Feign(声明式服务调用)
- 【Spring Cloud】--声明式服务调用Feign
- SpringCloud零基础上手(四)——服务发现以及Feign(声明式RESTful服务调用)
- SpringCloud 查找调用REST服务使用RestTemplate(ribbon负载)或feign模式 教程源码 火推
- Spring Cloud(十一)声名式服务调用:Feign的使用 (上)
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务的方法(推荐)
- Spring Cloud Learning | 第四篇:声明式服务调用(Fegin)
- 使用Spring cloud Feign在后台服务之间调用传递Multipart无法传递的问题
- Spring Cloud版——电影售票系统<三>使用Feign实现声明式REST调用
- springcloud使用feign实现服务间条用,参数数据太大,无法调用成功解决方法
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- 关于Dalston.SR3版本的spring cloud,用feign客户端调用服务hystrix不能生效的问题