版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_23490433/article/details/89185936
feign是一种声明式伪http客户端,它使得编写http客户端变得简单
使用feign,只需要创建一个接口并注解,它具有可拔插的注解特性,可使用feign注解和JAX-RS注解
feign支持可插拔的编码器和解码器,feign默认集成了ribbon和eureka结合,默认实现了负载均衡的效果。
ribbon是一个负载均衡客户端,可以很好的控制http和tcp的一些行为,feign默认集成了ribbon
feign调用服务的方式实现步骤如下:
第一步、新建maven工程,如下图:
第二步、在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.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cn</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</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</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: 8505
spring:
application:
name: eureka-feign
eureka:
client:
service-url:
defaultZone: http://localhost:8501/eureka/
第四步、新建com.cn.service类,代码如下:
package com.cn.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;
/**
* 定义一个feign接口
* */
@FeignClient(value = "service-client1")
public interface IFeignService {
@RequestMapping(value = "/hello",method = RequestMethod.GET)
String sayHelloFromClientOne(@RequestParam(value="name")String name);
}
第五步、新建包com.cn.controller,然后在该包下新建类,代码如下:
package com.cn.controller;
import com.cn.service.IFeignService;
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;
/**
* 对外暴露一个/hi的API接口,通过feign客户端的IFeignService来消费服务
* */
@RestController
public class HelloController {
@Autowired
IFeignService feignService;
@GetMapping(value="/hi")
public String sayHello(@RequestParam String name){
return feignService.sayHelloFromClientOne(name);
}
}
第六步、修改启动类如下:
package com.cn.eurekafeign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients("com.cn.service") //开启feign功能
@ComponentScan({"com.cn.controller"})
public class EurekaFeignApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaFeignApplication.class, args);
}
}
第七步、测试
启动类如下图所示