SpringCloud -- Feign 服务注册与发现

1、父包中添加nacos依赖管理

  <!--nacos的管理依赖-->
    <dependency>
         <groupId>com.alibaba.cloud</groupId>
         <artifactId>spring-cloud-alibaba-dependencies</artifactId>
         <version>2.2.5.RELEASE</version>
         <type>pom</type>
         <scope>import</scope>
    </dependency>

2、创建一个新包,用于调用生产方的方法。

2.1、添加依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
 </dependencies>

2.2、创建FeignClient接口

类上添加@FeignClient注解,value值为生产方的spring-application-name,接口的方法和需要调用的生产方方法一致。

示例:

@FeignClient(value = "userservice")
public interface UserClient {
    @GetMapping("/user/{id}")
    User findById(@PathVariable("id") Long id);
}

2.3、生产方和消费方添加配置

application.yml

spring:
    cloud:
        nacos:
          server-addr: localhost:8848 # nacos服务地址

 pom.xml 中导入坐标

 <!--feign客户端依赖-->
 <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-openfeign</artifactId>
 </dependency>

2.4、消费方启动类中添加@EnableFeignClients注解

两种写法

1.指定FeignClient字节码

@EnableFeignClients(clients = UserClient.class)
2.指定FeignClient所在包
@EnableFeignClients(basePackages =  "com.csdn.feign.clients")

服务启动后nacos服务列表有对应的生产方和消费方

 接口测试调用成功则证明feign配置完成。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值