目录
feign/openfeign和dubbo是常用的微服务RPC框架,由于feigin内部已经集成ribbon,自带了负载均衡的功能,当有多个同名的服务注册到注册中心时,会根据ribbon默认的负载均衡算法将请求分配到不同的服务。这篇文章就简单介绍一下怎么使用feign来调用远程的服务。
首先,需要有一个微服务注册中心来提供服务注册与发现,本章就使用之前创建的eureka作为注册中心。点击以下文章链接,教你快速搭建一个eureka server
springboot整合eureka、config搭建注册中心和配置中心https://blog.csdn.net/heyl163_/article/details/131715281首先,要实现服务间的调用,需要有服务提供者和服务消费者,创建两个项目,分别用于服务提供者和服务消费者。
一、服务提供者
创建一个springboot项目,取名为provider
1、修改maven配置文件pom.xml
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 https://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.3.4.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<eureka.version>1.4.4.RELEASE</eureka.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR12</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>${eureka.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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-eureka-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、修改系统配置文件
XML
server:
port: 8085
spring:
application:
name: provider
eureka:
instance:
hostname: localhost
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:8761/eureka
3、创建一个controller
在根目录下创建controller包,然后在controller包下创建一个UserController
java
package com.example.provider.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* @author heyunlin
* @version 1.0
*/
@RestController
@RequestMapping(path = "/user", produces = "application/json;charset=utf-8")
public class UserController {
@RequestMapping(value = "/name", method = RequestMethod.GET)
public String name() {
return "heyunlin";
}
}
4、启动类上添加@EnableDiscoveryClient注解
java
package com.example.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
二、服务消费者
1、创建一个springboot项目并命名为consumer
2、修改pom.xml配置文件
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 https://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.3.4.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<eureka.version>1.4.4.RELEASE</eureka.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR12</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>${eureka.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
</project>
3、修改配置文件,注册到eureka
XML
server:
port: 8086
spring:
application:
name: consumer
eureka:
instance:
hostname: localhost
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:8761/eureka
4、通过feign调用远程的方法
根目录下创建feign包,在feign包下创建一个接口FeignService(类名不重要)
@FeignClient("provider")指定注册到eurka的服务名
@RequestMapping的路径写provider服务的控制器接口路径
java
package com.example.consumer.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* @author heyunlin
* @version 1.0
*/
@FeignClient("provider")
public interface FeignService {
@RequestMapping(value = "/user/name", method = RequestMethod.GET)
String name();
}
5、最后,创建一个控制器类,类名随便取
java
package com.example.consumer.controller;
import com.example.consumer.feign.FeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* @author heyunlin
* @version 1.0
*/
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
FeignService feignService;
@RequestMapping(value = "/name", method = RequestMethod.GET)
public String name() {
return feignService.name();
}
}
这时候@Autowired会报错,找不到FeignService的bean,因为没有在配置类上面添加@EnableFeignClients注解
java
package com.example.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
三、测试效果
完成以上操作之后,依次启动euraka-server,provider和consumer
浏览器上访问consumer的控制器地址http://localhost:8086/user/name,发现成功返回了字符串。
四、开启Hystrix实现降级功能
首先需要开启hystrix
在pom.xml文件中添加配置
XML
feign:
hystrix:
enabled: true
然后创建一个FeignService的实现类,交给spring管理
java
package com.example.consumer.feign;
import org.springframework.stereotype.Component;
/**
* @author heyunlin
* @version 1.0
*/
@Component
public class FeignServiceImpl implements FeignService {
@Override
public String name() {
return "error";
}
}
最后,在FeiginService的@FeiginCilent上指定fallback=FeignServiceImpl.class
java
@FeignClient(value = "provider", fallback = FeignServiceImpl.class)
完成以上配置之后,重启consumer,访问http://localhost:8086/user/name时正确调用了provider的控制器方法,得到了正确的结果。
接着把关掉provider项目,再访问,发现调用失败,成功执行了配置的降级方法,直接返回了error
好了,springboot整合feign的介绍到这里就完了,代码已开源,按需获取~
注册中心
eurekahttps://gitee.com/he-yunlin/eureka.git服务提供者