什么是Feign?
Feign是受到Retrofit,JAXRS-2.0和WebSocket的影响,它是一个java的到http客户端绑定的开源项目。 Feign的主要目标是将Java Http 客户端变得简单。
推荐博客:
Feign的源码地址:https://github.com/OpenFeign/feign
深入理解Feign之源码解析:https://blog.csdn.net/forezp/article/details/73480304
常见错误:
1、Spring Cloud服务消费者使用Feign,不识别@EnableFeignClients 注解解决办法
查阅资料后是Spring Cloud对Feign的支持由org.springframework.cloud:spring-cloud-netflix-core 移到org.springframework.cloud:spring-cloud-openfeign-core下。
报错信息:cannot resolve sysmol EnableFeignClients
解决方案:
更改Spring Cloud的版本Finchley.RC2换为Dalston.RC1由于Finchley.RC2使用需要Spring Boot2.0,这里需要把Spring Boot版本换到1.5即可
org.springframework.cloud
spring-cloud-dependencies
Dalston.RC1
pom
import
一、建立服务消费者
1、项目建立可参考上一篇博客:https://blog.csdn.net/zjh_746140129/article/details/80557302
2、项目pom.xml 新增
org.springframework.cloud
spring-cloud-starter-feign
完整pom.xml
4.0.0
com.serverfeign
serverfeign
0.0.1-SNAPSHOT
jar
serverfeign
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE
UTF-8
UTF-8
1.8
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.cloud
spring-cloud-starter-feign
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
Dalston.RC1
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
3、编写服务消费Service和Controller
通过@ FeignClient(“服务名”),来指定调用哪个服务
package com.serverfeign.serverfeign.service;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
/**
* 通过@FeignClient(“服务名”),来指定调用哪个服务
* Created by zhoujh on 2018/6/04
*/
@FeignClient(value = "hello-service")
public interface HelloService {
@RequestMapping(value = "/hi",method = RequestMethod.GET)
String sayHiFromClientOne(@RequestParam(value = "name") String name);
}
package com.serverfeign.serverfeign.controller;
import com.serverfeign.serverfeign.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@RequestMapping(value = "/hi",method = RequestMethod.GET)
@ResponseBody
public String HiFeign(@RequestParam String name){
return helloService.sayHiFromClientOne(name);
}
}
4、修改配置文件
server.port=8765
spring.application.name=service-feign
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
5、修改启动类
在程序的启动类ServiceFeignApplication ,加上@EnableFeignClients注解开启Feign的功能
package com.serverfeign.serverfeign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ServerfeignApplication {
public static void main(String[] args) {
SpringApplication.run(ServerfeignApplication.class, args);
}
}
二、启动服务注册中心、服务提供者(2个)、服务消费者(Feign)
三、刷新消费者,查看服务提供者后台打印,此时已经完成了消费的负载均衡(这里采用了轮询策略)
Spring Boot与Spring Cloud学习使用可参看笔者博客
①Spring Cloud入门教程之服务注册与发现Eureka
②Spring Cloud入门教程之服务消费者 Ribbon
③Spring Cloud入门教程之服务消费者 Feign
④Spring Cloud入门教程之断路器 Hystrix
⑤Spring Cloud入门教程之断路由网关 Zuul
⑥Spring Cloud入门教程之分布式配置中心 Spring Cloud Config
⑦idea下新建Spring Boot项目并配置启动
⑧Spring Boot无法自动注入bean问题解决方案
⑨idea 设置Spring Boot热部署