OpenFeign调用服务示例代码

OpenFeign

OpenFeign支持@RequestMappin之类的Spring MVC注解,是一个声明式Web服务客户端和模板化的HTTP客户端。@FeignClient可以解析Spring MVC的@RequestMapping、@GetMapping等注解,通过动态代理生成实现类,在类中实现负载均衡,调用其他服务。其隐藏了REST请求,这样就不用动手拼接URL,OpenFeign都包了。

目录结构

  • 📄test.py
  • 📁demo
    • 📄pom.xml
    • 📁eureka-server
      • 📄pom.xml
      • 📁src
        • 📁test.java
          • 📁org/example/demo/eureka
            • 📄EurekaServerApplication.java
        • 📁main/java
          • 📁org/example/demo/eureka
            • 📄EurekaServerApplicationTests.java
          • 📁resources
            • 📄application.properties
    • 📁demo-server
      • 📄pom.xml
      • 📁src
        • 📁test.java
          • 📁org/example/demo
            • 📄ServerApplicationTests.java
        • 📁main/java
          • 📁org/example/demo/server
            • 📁controllers
              • 📄SimpleController.java
            • 📄ServerApplication.java
          • 📁resources
            • 📄application.properties
    • 📁demo-client
      • 📄pom.xml
      • 📁src
        • 📁test.java
          • 📁org/example/demo
            • 📄ClientApplicationTests.java
        • 📁main/java
          • 📁org/example/demo/client
            • 📁services
              • 📄IGetSimpleStrService.java
            • 📁api
              • 📄SimpleApi.java
            • 📄ClientApplication.java
          • 📁resources
            • 📄application.properties

配置文件

demo/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>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.6</version>
        <relativePath/>
    </parent>
    
    <groupId>org.example</groupId>
    <artifactId>demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    
    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <spring-cloud.version>2021.0.1</spring-cloud.version>
    </properties>
    
    <modules>
        <module>demo-client</module>
        <module>demo-server</module>
        <module>eureka-server</module>
    </modules>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
当然,我可以为您提供OpenFeign远程调用示例。首先,请确保您已经在项目中引入了OpenFeign的依赖。 接下来,您需要定义一个Feign客户端接口,用于声明远程调用的方法。这里以调用一个名为"example-service"的远程服务为例,示例代码如下: ```java import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; @FeignClient(name = "example-service") public interface ExampleServiceClient { @GetMapping("/api/example") String getExampleData(); } ``` 在上述示例中,我们使用`@FeignClient`注解指定了远程服务的名称,这里是"example-service"。然后通过`@GetMapping`注解指定了远程调用的URL路径。 接下来,您需要在启动类上添加`@EnableFeignClients`注解,以启用Feign客户端的自动配置。 完成以上准备工作后,您就可以在需要调用远程服务的地方注入`ExampleServiceClient`接口,并使用其中定义的方法进行远程调用了。示例代码如下: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ExampleController { @Autowired private ExampleServiceClient exampleServiceClient; @GetMapping("/example") public String getExampleData() { return exampleServiceClient.getExampleData(); } } ``` 在上述示例中,我们通过`@Autowired`注解将`ExampleServiceClient`接口注入到`ExampleController`中,并在`getExampleData()`方法中调用该接口的方法。 这就是一个简单的OpenFeign远程调用示例。您可以根据实际需要,按照类似的方式进行远程调用。希望对您有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值