SpringCloud入门——Feign
一、介绍
之前,我们使用Ribbon作为客户端负载均衡完成了Provider和Consumer的通信,其实我们可以发现,当我们通过RestTemplate调用其它服务时,所需要拼接url,拼接入参非常麻烦。而Feign的服务调用方式对于程序员来说更为友好,它基于Ribbon进行了封装,把一些负责的url和参数处理细节屏蔽起来,我们只需要简单编写Fiegn的客户端接口就可以像调用本地service去调用远程微服务。
Feign主要是构建微服务消费端。只要使用OpenFeign提供的注解修饰定义网络请求的接口类,就可以使用该接口的实例发送RESTful的网络请求。还可以集成Ribbon和Hystrix,提供负载均衡和断路器。
二、使用Feign
User-Provider
1、新建项目
2、application.properties
# 应用名称
spring.application.name=Person-Provider
# 应用服务 WEB 访问端口
server.port=8081
#寻找服务的主机地址端口
eureka.client.serviceUrl.defaultZone=http://eureka1.com:7001/eureka/
3、PersonController 用来提供服务
package com.zhow.personprovider;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PersonController {
@GetMapping("/alive")
public String alive(){
return "ok";
}
}
Person-Consumer
1、新建项目
2、application.properties
# 应用名称
spring.application.name=Person-Consumer
# 应用服务 WEB 访问端口
server.port=8082
#寻找服务的主机地址端口
eureka.client.serviceUrl.defaultZone=http://eureka1.com:7001/eureka/
3、PersonConsumerApplication启动类添加@EnableFeignClients注解
package com.zhow.personconsumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
public class PersonConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(PersonConsumerApplication.class, args);
}
}
4、PersonConsumerApi
package com.zhow.personconsumer;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
/**
* 不结合Eureka,就是自定义一个client名字,就用url属性指定服务器列表。url="http://ip:port/"
* @FeignClient 做的就是资源url的拼接
*/
@FeignClient(name= "person",url = "http://localhost:8081")
public interface PersonConsumerApi {
@GetMapping("/alive")
public String alive();
}
5、PersonController
package com.zhow.personconsumer;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class PersonController {
@Resource
private PersonConsumerApi personConsumerApi;
@GetMapping("/alive")
public String alive(){
return personConsumerApi.alive();
}
}
方法1:
@FeignClient使用url参数把指定服务器地址硬编码写入,实现调用Person-Provider中对应接口。
注:在使用url参数时,需要加入name或value参数,否则启动会报错。
Either 'name' or 'value' must be provided in @FeignClient
方法2:
结合Eureka,通过Ribbon实现调用Person-Provider中对应接口。
package com.zhow.personconsumer;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
/**
* 结合Eureka,通过Ribbon实现调用Person-Provider中对应接口。
*/
@FeignClient(name = "Person-Provider")
public interface PersonConsumerApi {
@GetMapping("/alive")
public String alive();
}
方法3:
通过建立Person-Api项目,让消费方和生成方继承的方式,规范对应接口
1、新建Person-Api项目
2、PersonApi
package com.zhow.personapi;
import org.springframework.web.bind.annotation.GetMapping;
public interface PersonApi {
@GetMapping("/alive")
public String alive();
}
3、maven install Person-Api项目
4、Person-Consumer 引入依赖
<!--引入自定义Api-->
<dependency>
<groupId>com.zhow</groupId>
<artifactId>Person-Api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
5、Person-Consumer 项目修改
package com.zhow.personconsumer;
import com.zhow.personapi.PersonApi;
import org.springframework.cloud.openfeign.FeignClient;
@FeignClient(name = "Person-Provider")
public interface PersonConsumerApi extends PersonApi {
}
package com.zhow.personconsumer;
import com.zhow.personapi.PersonApi;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class PersonController {
@Resource
private PersonApi personApi;
@GetMapping("/alive")
public String alive(){
return personApi.alive();
}
}
6、Person-Provider的Pom.xml添加依赖
<!--引入自定义Api-->
<dependency>
<groupId>com.zhow</groupId>
<artifactId>Person-Api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
7、Person-Provider 项目修改
package com.zhow.personprovider;
import com.zhow.personapi.PersonApi;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PersonController implements PersonApi {
@Override
public String alive() {
return "ok";
}
}