springcloud服务消费者调用方式(rest 和 feign)

本文介绍了SpringCloud中两种服务调用方式:1) 使用Feign进行远程调用,包括配置Maven Jar,实现服务接口,启动类设置及调用示例;2) 实现REST方式调用服务。这两种方式在微服务架构中常用于服务间的通信。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、feign进行远程调用

      maven jar

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

   实现调用服务接口

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

//调用注册的服务
@FeignClient(name="eureka-server")
public interface HelloRemote {

	 //restful api 调用
    @GetMapping("/hello/{name}")
    public String hello(@PathVariable("name") String name);
}

  启动类加上(basePackages服务接口所在的package)

@EnableFeignClients(basePackages="com.example.demo.remote")

  调用服务

import org.springframework.beans.factory.annotation.Autowired;

import org.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值