Feign 是一个声明式的伪Http客户端,使用时只需要创建一个接口并注解。
Feign 默认集成了 Ribbon,并和 Eureka 结合,默认实现了负载均衡的效果。
在这篇文章: SpringBoot2 整合 SpringCloud Ribbon 实现负载均衡实例 中我们使用RestTemplate+Ribbon去消费服务,本篇文章主要讲述如何通过Feign去消费服务。
工程目录如下图所示:
root 工程 spring-study,root 工程的 pom.xml 如下所示:
<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.0modelVersion>
<groupId>com.syrdbtgroupId>
<artifactId>studyartifactId>
<packaging>pompackaging>
<version>1.0-SNAPSHOTversion>
<modules>
<module>springcloud-study-eureka-servermodule>
<module>springcloud-study-feignmodule>
<module>springcloud-study-hello-servicemodule>
modules>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.7.RELEASEversion>
<relativePath/>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
<spring-cloud.version>Finchley.RELEASEspring-cloud.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${spring-cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
pom.xml 的依赖如下所示:
<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">
<parent>
<artifactId>studyartifactId>
<groupId>com.syrdbtgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>eurkea-serverartifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
dependency>
dependencies>
project>
配置文件如下所示:
# 服务注册中心端口号
server.port=8761
# 服务注册中心主机名
eureka.instance.hostname=127.0.0.1
# 不向注册中心注册自己
eureka.client.register-with-eureka=false
# 不需要检索服务
eureka.client.fetch-registry=false
# 注册中心地址,会自动填充 主机名 和 端口号
eureka.client.service-url.defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
package com.syrdbt.study.eureka.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @author syrdbt
* @date 2019-10-12
*/
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
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">
<parent>
<artifactId>study</artifactId>
<groupId>com.syrdbt</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>hello-service</artifactId>
<dependencies>
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
eureka:
client:
serviceUrl:
#注册中心的地址
defaultZone: http://localhost:8761/eureka/
server:
#当前服务端口号
port: 8762
spring:
application:
#当前应用名称,当前应用的名称
name: service-hi
package com.syrdbt.study.hello.service.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* @author syrdbt
* @date 2019-10-12
*/
@RestController
public class HelloController {
@Value("${server.port}")
private String port;
@RequestMapping(value = "hello", method = RequestMethod.GET)
public String index() {
return "eurekaClient:Hello World " + this.port;
}
}
package com.syrdbt.study.hello.service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @author syrdbt
* @date 2019-10-12
*/
@SpringBootApplication
@EnableEurekaClient
public class HelloServiceApplication {
public static void main(String[] args) {
SpringApplication.run(HelloServiceApplication.class, args);
}
}
<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">
<parent>
<artifactId>studyartifactId>
<groupId>com.syrdbtgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>feignartifactId>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
dependencies>
project>
application.yml:
server:
port: 8765
spring:
application:
# 该服务的名字
name: server-feign
eureka:
client:
serviceUrl:
# eureka服务的地址
defaultZone: http://localhost:8761/eureka/
通过@ FeignClient(“服务名”),来指定调用哪个服务。下面的示例中调用了service-hi服务的“/hello”接口,HelloFeign.java 代码如下:
package com.syrdbt.study.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author syrdbt
* @date 2019-10-12
*/
@FeignClient(name = "service-hi")
public interface HelloFeign {
@RequestMapping(value="/hello")
String hello();
}
HelloController.java,通过上面定义的Feign客户端 HelloFeign 来消费服务。
package com.syrdbt.study.feign.controller;
import com.syrdbt.study.feign.HelloFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author syrdbt
* @date 2019-10-12
*/
@RestController
public class HelloController {
@Autowired
private HelloFeign helloFeign;
@GetMapping(value="/hello")
public String getHi() {
return helloFeign.hello();
}
}
package com.syrdbt.study.feign;
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;
/**
* @author syrdbt
* @date 2019-10-12
*/
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableDiscoveryClient
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
}
需要启动4个实例。
⚠️注意:HelloServiceApplication 需要启动两个,两个的端口号分别为 8762 和 8763 。
IDEA 启动多实例教程:https://www.cnblogs.com/wuxun1997/p/11208519.html。
访问 http://localhost:8761/ 。
注册中心注册的 实例如下所示:
访问:http://localhost:8765/hello 。
运行截图如下所示: