feign的简单实例
一、什么是feign
Feign是Netflix开发的声明式(目前由Spring在维护)、模板化的HTTP客户端, Feign可以帮助我们更快捷、优雅地调用HTTP API。
简单的来说就是一个远程调用服务的框架/工具,让开发者以更少耦合更少代码更快更兼容的方法进行远程服务调用
二、Feign功能
- 可插拔的注解支持,包括Feign注解和JAX-RS注解;
- 与Ribbon负载均衡器、Hystrix或Sentinel熔断器无缝集成。
- 支持可插拔的HTTP编码器和解码器;
- 支持HTTP请求和响应的压缩。
三、基本使用
创建spring-cloud-feign项目
项目结构
父:spring-cloud-feign
子模块1:provider
子模块2:consumer
添加依赖
整个项目的依赖(由于全写在了父pom中,所以子依赖无需添加新的依赖)
<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>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-nacos-discovery</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
提供者:provider
1.提供者的application.yml
server:
port: 8180 //服务器端口
spring:
application:
name: test-feign-provider //提供者注册的服务名称
cloud:
nacos:
discovery:
server-addr: http://(nacos注册中心ip):8848
cluster-name: ${spring.application.name} //提供者注册的服务名称
2.提供者的Application
package com.smart.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* 添加@EnableDiscoveryClient注册服务
*/
@SpringBootApplication
@EnableDiscoveryClient
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class);
}
}
3.提供者的Controller
package com.smart.provider.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping
public class TestController {
@RequestMapping(value = "/getStr",method = RequestMethod.GET)
public String getStr(@RequestParam("stats") Integer stats){
if (stats==1){
return "对了";
} else {
return "错了";
}
}
}
注意:@RequestParam(“stats”)一定要加上,否则会报错
服务消费者:consumer
1.消费者的yml文件:
server:
port: 8083
spring:
application:
name: service-consumer
cloud:
nacos:
discovery:
server-addr: http://(nacos注册中心ip):8848
cluster-name: ${spring.application.name}
2.消费者的application
package com.smart.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注解开启对FeignClients的扫描
*/
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class);
}
}
3.消费者的dao层
package com.smart.consumer.dao;
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;
@FeignClient("test-feign-provider")
public interface TestFeign {
@RequestMapping(value = "/getStr",method = RequestMethod.GET)
String getStr(@RequestParam("stats") Integer stats);
}
4.消费者的service层和impl
package com.smart.consumer.service;
public interface TestFeignService {
String getTestStr(Integer stats);
}
package com.smart.consumer.service.impl;
import com.smart.consumer.dao.TestFeign;
import com.smart.consumer.service.TestFeignService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class TestFeignServiceImpl implements TestFeignService {
@Resource
TestFeign testFeign;
@Override
public String getTestStr(Integer stats) {
return testFeign.getStr(stats);
}
}
5.消费者的Controller层
package com.smart.consumer.controller;
import com.smart.consumer.service.TestFeignService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class TestFeignController {
@Resource
TestFeignService testFeignService;
@GetMapping("/getStr")
public String getStr(Integer stats){
String testStr = testFeignService.getTestStr(stats);
return testStr;
}
}
测试
分别注册两个服务到注册中心nacos后,用postman测试