SpringCloud-服务的消费者(Feign)

Feign简介

Feign是一个声明式的伪Http客户端,它是的写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用Feign注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果
Feign采用的事基于接口的注解
Feign整合了ribbon

项目创建

新建eureka-client项目,集成feign

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.SpringCloud</groupId>
<artifactId>service-feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>service-feign</name>
<description>service-feign-description</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.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.SR3</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-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-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如下

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

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

@FeignClient(value = "eureka-client")
public interface SchedualServiceHi {

@GetMapping("/hello")
String sayHiFromClientOne(@RequestParam(value = "name") String name);
}

新建一个controller,对外暴露一个”/hi”的API接口,通过上面定义的Feign客户端SchedualServiceHi来消费服务。代码如下

@RestController
public class HiController {

@Autowired
SchedualServiceHi schedualServiceHi;

@GetMapping("/hi")
public String sayHi(@RequestParam String name) {
return schedualServiceHi.sayHiFromClientOne(name);
}

}

浏览器访问
http://localhost:8765/hi?name=test

 

posted on 2018-06-30 15:01  嘣嘣嚓  阅读(190)  评论(0编辑  收藏  举报

导航