openfeign的简单使用

openfeign用在客户端,对服务端没啥要求,故只写客户端如何开发

1.开发环境

springboot 2.7.4

java 17

springcloud 2021.0.4

2.maven依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>2021.0.4</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

3.主函数application类添加注解

@SpringBootApplication
@EnableFeignClients
public class Openfeign2Application {

    public static void main(String[] args) {
        SpringApplication.run(Openfeign2Application.class, args);
    }

}
 

4.编写读取远程API接口类

import java.util.List;

@FeignClient(value = "ProjectClient",
        url = "http://127.0.0.1:8080/"
)
public interface ProjectClient {
    @RequestMapping(method = RequestMethod.GET, value = "/projects", produces = MediaType.APPLICATION_JSON_VALUE)
    ResponseEntity<List<ProjectDTO>> getAllProjects();
}
 

参考文章:

7. Declarative REST Client: Feign

sample/SampleController.java at main · The-Tech-Tutor/sample · GitHub

feign/SampleController.java at main · The-Tech-Tutor/feign · GitHub

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用OpenFeign,首先需要创建一个启动类,将其标记为@SpringBootApplication,并在注解中排除掉DataSource自动配置,同时添加@EnableFeignClients、@EnableEurekaClient和@EnableDiscoveryClient注解来启用Feign客户端和服务注册与发现功能。 接下来,需要定义一个接口,并使用@FeignClient注解来标识该接口是一个Feign客户端,并指定要调用的服务。在接口中,可以使用REST风格的方式来定义接口请求服务的方法。例如,可以使用@RequestMapping注解来指定请求路径和请求方法,然后在方法中返回要调用的服务的响应结果。 要在项目中使用OpenFeign,需要添加OpenFeign的依赖。可以在项目的pom.xml文件中添加以下依赖: ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>2.2.2.RELEASE</version> </dependency> ``` 最后,需要配置Feign接口的错误回滚配置。这可以通过在Feign接口上添加注解来实现。 综上所述,使用OpenFeign的步骤如下: 1. 创建一个启动类,并标记为@SpringBootApplication,同时排除DataSource自动配置,添加@EnableFeignClients、@EnableEurekaClient和@EnableDiscoveryClient注解。 2. 定义一个接口,并使用@FeignClient注解来标识该接口是一个Feign客户端,指定要调用的服务。 3. 在接口中使用REST风格的方式定义接口请求服务的方法,并添加相应的注解来配置请求路径和请求方法。 4. 添加OpenFeign的依赖到项目的pom.xml文件中。 5. 配置Feign接口的错误回滚配置,可以通过在接口上添加注解来实现。 请注意,确保在项目中正确配置了服务注册中心和相应的服务提供者。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [OpenFeign的理解和使用](https://blog.csdn.net/m0_45016797/article/details/123633386)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [OpenFeign简单使用](https://blog.csdn.net/qq_50626505/article/details/124060848)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值