本文服务注册中心用的是SpringCloudAlibaba的Nacos
引入OpenFeign服务调用
第一步:添加pom依赖
<!--nacos-discovery-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--openfeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
第二步:开启OpenFeign
@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients
public class xxxMain {
public static void main(String[] args) {
SpringApplication.run(xxxMain.class, args);
}
}
第三步:修改application.yml
#设置feign客户端超时时间(OpenFeign默认支持ribbon)
ribbon:
#指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间
ReadTimeout: 5000
#指的是建立连接后从服务器读取到可用资源所用的时间
ConnectTimeout: 5000
第四步:使用OpenFeign调用远程服务
@Component
//value是nacos服务列表的服务名
@FeignClient(value = "nacos-service")
@RequestMapping(Constant.MAPPING_BASE_PREFIX)
public interface BaseFeignService {
@GetMapping("要和nacos-service服务同样的请求地址")
CommonResult queryArmySysCodeItemList(@PathVariable(value = "setId", required = false) Long setId);
}
隐患:如果被调用的服务宕机,会引起服务雪崩
引入Sentinel防止因服务宕机引起的服务雪崩
第一步:添加pom
<!--sentinel-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<!--如果引入sentinel导致返回格式从json变成了xml,需要添加-->
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</exclusion>
</exclusions>
</dependency>
第二步:修改application.yml
#开启feign对sentinel的支持
feign:
sentinel:
enabled: true
第三步:修改OpenFeign调用远程服务
@Component
//value是nacos服务列表的服务名
@FeignClient(value = "nacos-service",fallbackFactory = BaseFeignServiceFallback.class)
@RequestMapping(Constant.MAPPING_BASE_PREFIX)
public interface BaseFeignService {
@GetMapping("要和nacos-service服务同样的请求地址")
CommonResult queryArmySysCodeItemList(@PathVariable(value = "setId", required = false) Long setId);
}
Component
@Slf4j
public class BaseFeignServiceFallback implements FallbackFactory<BaseFeignService> {
/**
* 提高容错率 防止服务雪崩
*
* @param throwable
* @return
*/
@Override
public BaseFeignService create(Throwable throwable) {
return new BaseFeignService() {
@Override
public CommonResult queryArmySysCodeItemList(Long setId) {
log.info("远程服务调用出现问题了");
log.error("{}", throwable);
return CommonResultUtil.getCommonResult(null);
}
};
}
}
同样的场景下,并不影响后续业务处理
控制台信息
INFO ] [2020-12-02 15:15:11,006] [http-nio-9002-exec-2] com.netflix.loadbalancer.DynamicServerListLoadBalancer - DynamicServerListLoadBalancer for client base-service initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=base-service,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:com.alibaba.cloud.nacos.ribbon.NacosServerList@62d4781b
[INFO ] [2020-12-02 15:15:11,123] [http-nio-9002-exec-2] com.business.client.BaseFeignServiceFallback - 远程服务调用出现问题了
[ERROR] [2020-12-02 15:15:11,123] [http-nio-9002-exec-2] com.business.client.BaseFeignServiceFallback - {}
java.lang.RuntimeException: com.netflix.client.ClientException: Load balancer does not have available server for client: xxx-service