共计 4079 个字符,预计需要花费 11 分钟才能阅读完成。
上一篇文章主要介绍了如何搭建一个简单的 springcloud 框架。不过,我们搭建好框架就是为了消费它使用它,那么这篇文章就来看看如何去消费使用我们之前搭建起来的服务吧!
首先本文是基于上一篇文章进行的。
一、Feign 简介
Feign 是 Netflix 开发的声明式、模板化的 HTTP 客户端,Feign 可以帮助我们更快捷、优雅地调用 HTTP API。
二、启动程序
继续用上一节的工程,启动 eureka-server,端口为 8080; 启动 eureka-client 两次,端口分别为 8081、8082.
三、创建一个 feign 的服务
我们首先要新建一个 spring-boot 工程,取名为 serice-feign。
这次我们要选择三个依赖:
对应的 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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zhouxiaoxi</groupId>
<artifactId>eureka-feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-feign</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</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>
接下来我们就需要配置 application.yml 文件了:
server:
#端口号
port: 8083
spring:
application:
#端口名称
name: eureka-feign
eureka:
client:
serviceUrl:
#服务注册地址
defaultZone: http://localhost:8080/eureka/
在程序的启动类 EurekaFeignApplication,加上 @EnableFeignClients 注解开启 Feign 的功能:
package com.zhouxiaoxi.eurekafeign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class EurekaFeignApplication {public static void main(String[] args) {SpringApplication.run(EurekaFeignApplication.class, args);
}
}
现在我们需要定义一个 feign 接口,通过 @FeignClient(“服务名”)注解来指定调用哪个服务。
比如在下面的代码中调用了 eureka-client 服务的“/hello”接口,代码如下:
package com.zhouxiaoxi.eurekafeign.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(value = "eureka-client")
public interface SchedualServiceHello {@RequestMapping(value = "/hello",method = RequestMethod.GET)
String sayHiFromClientOne(@RequestParam(value = "name") String name);
}
在 Web 层的 controller 层,对外暴露一个”/hello”的 API 接口,通过上面定义的 Feign 客户端 SchedualServiceHello 来消费服务。代码如下:
package com.zhouxiaoxi.eurekafeign.controller;
import com.zhouxiaoxi.eurekafeign.service.SchedualServiceHello;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
SchedualServiceHello schedualServiceHello;
@GetMapping(value = "/hello")
public String sayHi(@RequestParam String name) {return schedualServiceHello.sayHiFromClientOne( name);
}
}
启动程序,多次访问 http://localhost:8083/hello?name=feign,浏览器显示内容为:
hello feign ,i am from port:8081
hello feign ,i am from port:8082
至此我们这个服务消费的框架就搭建完毕了。。。