Nacos(三):Nacos与OpenFeign的对接使用

 

上篇文章中,简单介绍了如何在SpringCloud项目中接入Nacos作为注册中心,其中服务消费者是通过RestTemplate+Ribbon的方式来进行服务调用的。

实际上在日常项目中服务间调用大都用的是OpenFeign, OpenFeign自身整合了Ribbon和Hystrix,为服务调用提供了更优雅的方式

那么接入了Nacos之后,服务调用还能用这一套吗?

通过我在公司项目上的试水,这个大胆的设想是完全没问题的

本文在上一篇文章中的项目工程基础上,进行测试和演示,文章地址:在SpringCloud项目中接入Nacos作为注册中心

创建项目

打开之前创建的工程Nacos,目前已经有两个子工程:

  • nacos-provide:服务提供者
  • nacos-consumer:服务消费者(RestTemplate+Ribbon服务调用)

同样的操作,在Nacos项目下继续创建一个Springboot项目名为nacos-feign,创建时添加OpenFeign的依赖,如图:

nacos-fegin的pom.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>Nacos</artifactId>
<groupId>com.study.www</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.larscheng.www</groupId>
<artifactId>nacos-fegin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>nacos-fegin</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

定义远程接口

创建RemoteClient接口,来定义OpenFeign要调用的远程服务接口。

同时通过@FeginClient注解指定被调用方的服务名,通过fallback属性指定RemoteHystrix类,来进行远程调用的熔断和降级处理。

RemoteClient.java代码如下

@FeignClient(name = "nacos-provide",fallback = RemoteHystrix.class)
public interface RemoteClient { @GetMapping("/helloNacos")
String helloNacos();
}

RemoteHystrix.java代码如下

@Component
public class RemoteHystrix implements RemoteClient {
@Override
public String helloNacos() {
return "请求超时了";
}
}

通过OpenFeign调用远程服务

在启动类NacosFeignApplication.java中添加注解@EnableDiscoveryClient开启服务注册、添加注解@EnableFeignClients开启OpenFeign,启动类通过OpenFeign调用服务代码如下

@SpringBootApplication
@RestController
@EnableDiscoveryClient
@EnableFeignClients
public class NacosFeignApplication { public static void main(String[] args) {
SpringApplication.run(NacosFeginApplication.class, args);
} @Autowired
private RemoteClient remoteClient; @GetMapping("/feign")
public String test() {
return remoteClient.helloNacos();
}
}

添加项目配置文件

在resourse目录下,添加application.yml配置

server:
port: 9529 spring:
application:
name: nacos-feign
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848

启动测试

  1. 启动Nacos-server
  2. 启动项目nacos-provide
  3. 启动项目nacos-feign

完成以上三步后,访问Nacos控制台,检查服务注册情况,如果启动都成功,你看到的应该是如下图:

浏览器访问 http://127.0.0.1:9529/feign, 可以看到返回结果与RestTemplate结果无异,但对于编码和操作方式都更加优雅。

访问nacos-feign的接口 http://127.0.0.1:9529/feign, 可以通过OpenFeign远程调用nacos-provide的接口,返回结果:

你好,nacos!

总结

OpenFegin整合Ribbon和Hystrix,为微服务中远程调用提供了一种更优雅的调用方式,它支持负载均衡和容错熔断机制。通过上面的例子,在SpringCloud中接入Nacos做注册中心后,并不会影响我们继续使用其他SpringCloud组件。

本文源码https://github.com/larscheng/larscheng-learning-demo/tree/master/Nacos

一   需要添加熔断yml配置

二   spring cloud 服务互调的2中方式               OpenFeign

RestTemplate+Ribbon进行服务调用 

最新文章

  1. cefsharp重写默认js弹窗(alert/confirm/prompt)
  2. PDA移动POS终端系统,实现专柜或店铺的收货、零售、盘点通过无线网络直接连接总部中央数据库,实现高效安全的移动供应链管理
  3. 【转】ACM训练计划
  4. SQL Server 2008安装过程中的一些问题和心得
  5. Ansible playbook API 开发 调用测试
  6. [转]c++ vector 遍历方式
  7. java语言的认识
  8. 离奇“undefined reference”错误分析与解决方案
  9. (转)VS无法启动调试:“生成下面的模块时,启用了优化或没有调试信息“
  10. 关于ASP.NET 中站点地图sitemap 的使用
  11. Android_使用getIdentifier()获取资源Id
  12. struts2笔记04-XxxAware接口
  13. 深入Callable及Runnable两个接口 获取线程返回结果
  14. 实体类和数据表的映射异常(XXX is not mapping[ ])
  15. 微信小程序-查询快递
  16. 抽取非基本验证到规则文件 - A2D规则引擎
  17. Linux CentOS更改文件的权限
  18. 使用cwRsync实现windows下文件定时同步
  19. Java内存模型简析
  20. LVS-DR 配置测试

热门文章

  1. vscode分栏显示快捷键
  2. oracle11g在windows下安装
  3. 字符串压缩(一)之ZSTD
  4. 【C# 线程】内存模型(C#)---非常重要 【多线程、并发、异步的基础知识】
  5. MSBuild 和项目文件
  6. 3rd S-curve velocity profile
  7. HTML背景图加载过慢解决思路
  8. pyinstaller打包exe文件,运行时一闪而过
  9. egg中使用sequelize事务,实现原子性
  10. 自己动手写Vector【Cherno C++教程】