码迷,mamicode.com
首页 > 编程语言 > 详细

SpringCloud-服务的消费者(Feign)

时间:2018-06-30 15:24:08      阅读:298      评论:0      收藏:0      [点我收藏+]

标签:代码   des   repos   ima   sharp   str   ring   ret   zone   

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

技术分享图片

 

SpringCloud-服务的消费者(Feign)

标签:代码   des   repos   ima   sharp   str   ring   ret   zone   

原文地址:https://www.cnblogs.com/EnzoDin/p/9247179.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!