Spring Cloud | 第三篇:服务消费者(Feign)

来源:互联网 发布:破解版预算软件 编辑:程序博客网 时间:2024/05/21 19:23

本文的参考资料:
spring cloud feign文档
方志明博客
Spring Cloud构建微服务架构(二)服务消费者

一、Feign简介

Feign是一个声明式的Web Service客户端,它使得编写Web Serivce客户端变得更加简单。我们只需要使用Feign来创建一个接口并用注解来配置它既可完成。它具备可插拔的注解支持,包括Feign注解和JAX-RS注解。Feign也支持可插拔的编码器和解码器。Spring Cloud为Feign增加了对Spring MVC注解的支持,还整合了Ribbon和Eureka来提供均衡负载的HTTP客户端实现。

下面,通过一个例子来展现Feign如何方便的声明对上述computer-service服务的定义和调用。

二、准备工作

  • 启动服务注册中心Eureka-server工程
  • 启动服务提供方eureka-client
  • 将eureka-client的端口号修改为 8763,再启动一个服务

此时有eureka-client有两个服务在运行:

  • http://127.0.0.1:8762/hello
  • http://127.0.0.1:8763/hello

三、创建消费者

创建一个Spring Boot工程,配置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.orange</groupId>    <artifactId>feign</artifactId>    <version>0.0.1-SNAPSHOT</version>    <packaging>jar</packaging>    <name>feign</name>    <description>Demo project for Spring Feign</description>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.5.7.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.SR4</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>

在应用主类中通过@EnableFeignClients注解开启Feign功能:

@EnableEurekaClient@EnableFeignClients@SpringBootApplicationpublic class FeignApplication {    public static void main(String[] args) {        SpringApplication.run(FeignApplication.class, args);    }}

定义一个feign接口,通过@ FeignClient(“服务名”),来指定调用哪个服务。比如在代码中调用了eureka-client服务的“/hello”接口,代码如下:

/** * 通过@ FeignClient(“服务名”),来指定调用哪个服务。比如在代码中调用了eureka-client服务的“/hello”接口 */@FeignClient(value = "eureka-client")public interface HelloService {    @RequestMapping(value = "/hello", method = RequestMethod.GET)    String sayHelloFromClientOne();}

在Web层的controller层,对外暴露一个”/hello”的API接口,通过上面定义的Feign客户端HelloService 来消费服务。代码如下:

@RestControllerpublic class HelloController {    @Autowired    HelloService service;    @RequestMapping(value = "/hello", method = RequestMethod.GET)    public String sayHello() {        return service.sayHelloFromClientOne();    }}

application.properties,指定eureka服务注册中心即可,如:

spring.application.name=feign-consumerserver.port=8765eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

启动该应用,访问http://localhost:8765/hello,页面交替出现

This is a Eureka client, from port: 8762This is a Eureka client, from port: 8763

源码

https://gitee.com/tianqian/Springcloudlearn/tree/master/Chapter3

阅读全文
0 0