Springboot集成feign远程调用

需求:在leadnews-wemedia微服务里需要调用leadnews-article微服务的接口。新建一个支持feign调用的名为heima-leadnews-feign-api的模块

  1. heima-leadnews-feign-api的pom文件里导入openfeign依赖
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
  1. heima-leadnews-feign-api里定义远程调用接口
@FeignClient(value = "leadnews-article",path = "/api/v1/article")
public interface IArticleClient {

    /****、
     * 文章保存
     */
    @PostMapping(value = "/save")
    ResponseResult<Long> save(@RequestBody ArticleDto dto);
}
  1. 在leadnews-wemedia的pom文件中引入
<dependency>
    <groupId>com.heima</groupId>
    <artifactId>heima-leadnews-feign-api</artifactId>
</dependency>
  1. 在leadnews-wemedia的启动类上添加@EnableFeignClients注解
@EnableFeignClients(basePackages = "com.xxx.feign.article")
  1. leadnews-article的其他文件
  • ApArticleController
@RestController
@RequestMapping(value = "/api/v1/article")
public class ApArticleController {

    @Autowired
    private ApArticleService apArticleService;

    /****、
     * 文章保存
     */
    @PostMapping(value = "/save")
    public ResponseResult<Long> save(@RequestBody ArticleDto dto){
        return apArticleService.saveArticle(dto);
    }
	...

}
  • leadnews-article配置
server:
  port: 51802
spring:
  application:
    name: leadnews-article
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.33.31:8848
      config:
        server-addr: 192.168.33.31:8848
        file-extension: yml
...
...
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中开放Feign接口可以通过以下步骤实现: 1. 首先,在项目的pom.xml文件中添加Spring Cloud和Feign的依赖。你可以在添加以下依赖: ```xml <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> ``` 这些依赖将启用Feign和与服务发现组件(如Eureka)的集成。 2. 创建一个Feign客户端接口。在这个接口中,你可以使用`@FeignClient`注解来标记该接口为Feign客户端,并通过`url`属性指定需要调用的远程服务的URL。例如: ```java import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; @FeignClient(url = "${remote.service.url}") public interface MyFeignClient { @PostMapping("/api/my-endpoint") String callRemoteService(@RequestParam("param") String param); } ``` 在上述示例中,`@FeignClient`注解中的`url`属性通过`${remote.service.url}`从配置文件中获取了远程服务的URL。你可以根据实际情况进行配置。 3. 在Spring Boot应用程序的启动类上添加`@EnableFeignClients`注解以启用Feign客户端。例如: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableFeignClients public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } } ``` 这将使Feign客户端自动扫描并注册到Spring应用程序上下文中。 4. 最后,你可以在需要调用远程服务的地方注入并使用`MyFeignClient`(自定义的Feign客户端接口)。例如: ```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 MyController { @Autowired private MyFeignClient feignClient; @GetMapping("/my-api") public String callRemoteService() { return feignClient.callRemoteService("example-param"); } } ``` 在这个示例中,`MyController`类通过`@Autowired`注解将`MyFeignClient`注入,然后在`callRemoteService()`方法中调用远程服务。 通过以上步骤,你就可以在Spring Boot中开放Feign接口并调用远程服务了。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值