当前位置:   article > 正文

【Nacos学习】二、整合OpenFeign组件_nacos fegin学习

nacos fegin学习

功能实现

创建项目

创建【nacos-feign】子服务,用于测试OpenFeign组件的使用,其pom文件如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <artifactId>nacos</artifactId>
  7. <groupId>com.kongcheng</groupId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. </parent>
  10. <groupId>com.kongcheng</groupId>
  11. <artifactId>nacos-feign</artifactId>
  12. <version>0.0.1-SNAPSHOT</version>
  13. <name>nacos-feign</name>
  14. <description>Demo project for Spring Boot</description>
  15. <properties>
  16. <java.version>1.8</java.version>
  17. </properties>
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.springframework.cloud</groupId>
  21. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  22. </dependency>
  23. <!-- 在微服务中引入openfeign的依赖-->
  24. <dependency>
  25. <groupId>org.springframework.cloud</groupId>
  26. <artifactId>spring-cloud-starter-openfeign</artifactId>
  27. </dependency>
  28. </dependencies>
  29. <build>
  30. <plugins>
  31. <plugin>
  32. <groupId>org.springframework.boot</groupId>
  33. <version>2.3.0.RELEASE</version>
  34. <artifactId>spring-boot-maven-plugin</artifactId>
  35. </plugin>
  36. </plugins>
  37. </build>
  38. </project>

主入口添加【@EnableDiscoveryClient】用作开启服务注册,【@EnableFeignClients】作为开启OpenFeign

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  4. import org.springframework.cloud.openfeign.EnableFeignClients;
  5. @SpringBootApplication
  6. @EnableDiscoveryClient//开启服务注册
  7. @EnableFeignClients//开启OpenFeign
  8. public class NacosFeignApplication {
  9. public static void main(String[] args) {
  10. SpringApplication.run(NacosFeignApplication.class, args);
  11. }
  12. }

配置文件如下:

  1. server:
  2. port: 18202
  3. spring:
  4. application:
  5. name: nacos-feign
  6. cloud:
  7. nacos:
  8. discovery:
  9. server-addr: 127.0.0.1:8848 # Nacos作为服务注册中心地址

定义远程接口

创建名为【api】的Package,如下所示:

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

调用的是前面创建的【nacos-provide】子服务中的方法,需要注意方法名需要与远程服务名相同

  1. import org.springframework.cloud.openfeign.FeignClient;
  2. import org.springframework.web.bind.annotation.GetMapping;
  3. @FeignClient(name = "nacos-provide",fallback = RemoteHystrix.class)
  4. public interface RemoteClient {
  5. @GetMapping("/helloNacos")
  6. String helloNacos();
  7. }

创建RemoteHystrix接口,来进行熔断降级处理

  1. import org.springframework.stereotype.Component;
  2. /**
  3. * 服务降级处理
  4. */
  5. @Component
  6. public class RemoteHystrix implements RemoteClient{
  7. public String helloNacos() {
  8. return "请求超时了!";
  9. }
  10. }

具体调用OpenFeign测试

创建【TestController】控制层:

  1. import com.kongcheng.nacosfeign.api.RemoteClient;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. @RestController
  6. public class TestController {
  7. @Autowired
  8. private RemoteClient remoteClient;
  9. @GetMapping("/feign")
  10. public String test() {
  11. return remoteClient.helloNacos();
  12. }
  13. }

完成后启动服务。

测试

需要启动【Nacos-server】,【nacos-provide】,【nacos-feign】,启动成功后浏览器访问:localhost:18202/feign,可以通过OpenFeign远程调用nacos-provide的接口

项目源码

https://gitee.com/dukeLiuyu/nacos

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/428831
推荐阅读
相关标签