码上敲享录 > SpringCloud实战教程 > Springcloud通过OpenFeign调用注册中心其他服务接口示例教程

Springcloud通过OpenFeign调用注册中心其他服务接口示例教程

上一章章节目录下一章 2020-06-19已有1276人阅读 评论(0)

各个章节可能有关联关系,查找《SpringCloud实战教程》其他章节请参考:

http://www.yayihouse.com/yayishuwu/book/79




4. Springcloud通过OpenFeign调用注册中心其他服务接口示例教程


(1) 右键刚刚创建好的工程--New--Module--Spring Initializr--next--》修改一下Groupcom.wlg.springcloudArtifacteureka-client-feign--》next--》Spring Cloud Routing--》勾选OpenFeign --》 next--》Finish。

(2) 启动类上加上@EnableFeignClients和@EnableDiscoveryClient注解

@SpringBootApplication

@EnableFeignClients(basePackages = "com.wlg.springcloud.eurakaclientfeifn")

@EnableDiscoveryClient

public class EurakaClientFeifnApplication {

public static void main(String[] args) {

SpringApplication.run(EurakaClientFeifnApplication.class, args);

}

}

(3) application.properties改为application.yml,内容如下:

server:

 port: 9903

spring:

 application:

   name: service-two

eureka:

 client:

   serviceUrl:

     defaultZone: http://localhost:9901/eureka/

(4) 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
     <groupId>com.wlg.springcloud</groupId>
     <artifactId>springcloud</artifactId>
     <version>0.0.1-SNAPSHOT</version>
  </parent>
  <groupId>com.wlg.springcloud</groupId>
  <artifactId>euraka-client-feifn</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>euraka-client-feifn</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-netflix-eureka-client</artifactId>
     </dependency>
     <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
     </dependency>
  </dependencies>
</project>

(5) Feign客户端代码:

service-one就是被调用的服务(前面一章eureka-client工程)的application.yml定义的工程名,@RequestMapping("/hello")/hello请求地址就是被调用的服务sayHello方法的请求地址

@FeignClient(value = "service-one")

public interface FeignClientService {

   @RequestMapping("/hello")

   public  String sayHello(@RequestParam("txt")String txt);

}

创建一个controller类调用feign接口,在浏览器访问这个接口测试用,把调用结果打印到浏览器显示:

@Controller

public class FeignClientController {

   @Autowired

   private FeignClientService feignClientService;

    @RequestMapping("/sayHello")

   public  void sayHello(String txt, HttpServletResponse response) throws IOException {

        String result = feignClientService.sayHello(txt);

        response.setCharacterEncoding("utf-8");

        response.setContentType("text/html; charset=utf-8");

        PrintWriter writer = response.getWriter();

        writer.print(result);

        writer.flush();

        writer.close();

    }

}

(6) Feign客户端调用服务的接口代码:

我在前面一节中的eureka-client项目中创建了一个controller类,里面定义一个给feign客户端调用的接口作为演示

@Controller

public class FeignServerController {

    @RequestMapping("/hello")

    @ResponseBody

   public  String sayHello(@RequestParam("txt") String txt){

        return  "我收到了您的问候:"+txt;

    }

}

(7) 测试要结合前面几章来联合测试,分别启动注册中心eureka-servereureka-client和现在的eureka-client-feign,浏览器访问注册中心服务:http://localhost:9901/就可以看到服务提供者service-oneservice-two已经注册到注册中心了。


在浏览器地址栏输入:http://localhost:9903/sayHello?txt=你好

浏览器就会显示:我收到了您的问候:你好

这个就说明feign调用注册中心中其他服务接口成功


补充@FeignClient的属性说明:

name和value:两者的作用是一致的,指定FeignClient的名称,如果项目使用了Ribbon, name属性会作为被调用微服务的名称,用于服务发现。


url:url一般用于调试,可以手动指定@FeignClient调用的地址。


decode404:当发生404错误时,如果该字段为true,会调用decoder进行解码,否则抛出FeignException。


configuration:Feign配置类,可以自定义Feign的Encoder、Decoder、LogLevel、Contract。


fallback:定义容错的处理类,当调用远程接口失败或超时时,会调用对应接口的容错逻辑,fallback指定的类必须实现@FeignClient标记的接口。


fallbackFactory:工厂类,用于生成fallback类示例,通过这个属性我们可以实现每个接口通用的容错逻辑,减少重复的代码。


path:定义当前FeignClient的统一前缀。


向大家推荐《Activiti工作流实战教程》:https://xiaozhuanlan.com/activiti
0

有建议,请留言!

  • *您的姓名:

  • *所在城市:

  • *您的联系电话:

    *您的QQ:

  • 咨询问题:

  • 提 交