springCloud学习第三天----远程调用feign

博客介绍了新建Spring Boot服务user - consumer的过程,除勾选feign支持外与创建eureka - client步骤相同。在eureka - client中加入代码,在user - consumer里写接口并加@FeignClient注解,最后创建模拟登陆类注入接口调用login方法实现远程调用。

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

我们要新建一个springboot服务,user-consumer 除了和之前创建eureka-client步骤都一样以外还需要勾选feign支持

我们在eureka-client中加入代码

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserService {
    @GetMapping("/login")
    public String login(){
        return "success1";
    }
}

再到user-consumer中

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class UserConsumerApplication {

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

}
启动类要加入@EnableFeignClients注解,下面我们做个小栗子,

import com.example.userconsumer.userService.impl.UserServicImpl;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient(name="eureka-client")
public interface UserService {
    @GetMapping("/login")
    String login();
}

 我们写一个借口,加上@FeignClient注解,name值就是eureka-client服务的名

接下来我们作一个模拟登陆的类,

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Longin {
    @Autowired
    private UserService userService;

    @GetMapping("/userLogin")
    public String longin(){

        return userService.login();
    }
}

把接口注入,然后调用login方法,这样就实现了远程调用

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值