实例演示1:使用OpenFeign调用服务
基于nacos的服务提供者:
- 创建SimpleOpenFeign的maven项目,添加pom依赖
<dependencies><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-starter-alibaba-nacos-discovery</artifactId></dependency></dependencies>
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class SpringBootApp {public static void main(String[] args) {SpringApplication.run(SpringBootApp.class, args);}}
server.port=8080
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
- 编写控制类方法,通过OpenFeign框架调用远端服务
package prj.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;@Component
@FeignClient(name = "ServiceForOpenFeign")
interface Openfeignclient {@GetMapping("/getAccount/{id}")String getAccount(@PathVariable String id);
}@RestController
public class Controller {@Autowiredprivate Openfeignclient tool;@GetMapping("/demoOpenFeign/{id}")public String demoOpenFeign(@PathVariable String id) {return tool.getAccount(id);}}
- 在application.yml配置文件设置超时时间
feign.client.config.default.connectTimeout=2000
- 在application.yml配置文件设置输出debug级别的日志
logging.level.prj.controller=DEBUG
- 在启动类添加配置代码,设置OpenFeign输出日志范围
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class SpringBootApp {@Configurationpublic class FeignConfig{@BeanLogger.Level feignLoggerLevel(){return Logger.Level.FULL;}}public static void main(String[] args) {SpringApplication.run(SpringBootApp.class, args);}}
- 在application.yml配置文件设置请求参数压缩和返回结果
feign.compression.request.enabled=true
feign.compression.response.enabled=true
- 在application.yml配置文件设置待压缩请求的最小值
feign.compression.request.min-request-size=4096
实例演示2:实现负载均衡和服务降级
创建OpenFeignWithRibbon项目:
(1)负载均衡:
- 修改application.yml配置文件中的端口号
server.port=9090
- 编写控制类方法,引入OpenFeign框架,实现以负载均衡的方法调用远程服务
@Component
@FeignClient(name = "GateWayPrj")
interface Openfeignclient {@GetMapping("/callServiceByRibbon")String callServiceByRibbon();
}@RestController
public class Controller {@Autowiredprivate Openfeignclient tool;@GetMapping("/demoOpenFeignWithRibbon")public String demoOpenFeignWithRibbon() {return tool.callServiceByRibbon();}}
- 在application.yml文件中指定负载均衡细节,连接超时时间以及重试次数
server.port=9090
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RoundRibbonRule
feign.sentinel.enabled=true
(2)服务降级:
<dependencies><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-starter-alibaba-nacos-discovery</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId></dependency></dependencies>
- 在OpenFeign框架接口的@FeignClient注解引入fallback参数
@Component
@FeignClient(name = "GateWayPrj",fallback = FallbackHandler.class)
interface Openfeignclient {@GetMapping("/callServiceByRibbon")String callServiceByRibbon();
}
package prj.controller;import org.springframework.stereotype.Component;@Component
public class FallbackHandler implements Openfeignclient{public String callServiceByRibbon(){ return "In Fallback Function."; }}