springcloud——openfeign(feign)的服务调用

feign的服务调用方法只需在服务消费者端声明服务提供者的service接口,然后调用接口即可。

feign自带负载均衡

在这里插入图片描述

1、声明的业务接口

package com.springcloud.com.springcloud.service;

import com.springcloud.enties.CommonResult;
import com.springcloud.enties.Payment;
import feign.Param;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author dc
 * @date 2020/7/27 - 17:45
 */
@Component  //注明该类为一个组件被spring容器扫描
@FeignClient("payment-provider")    //注明为feign客户端
public interface IPaymentService {

    //参数位置必须添加@RequestParam("id")
    @GetMapping("/payment/second")
    public CommonResult<Payment> doSecond(@RequestParam("id") Long id);
}

2、控制器

package com.springcloud.com.springcloud.controller;

import com.springcloud.com.springcloud.service.IPaymentService;
import com.springcloud.enties.CommonResult;
import com.springcloud.enties.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author dc
 * @date 2020/7/27 - 17:54
 */
@RestController
@Slf4j
public class OrderFeignController {

    @Resource
    private IPaymentService paymentService;

    @GetMapping("/feign/first")
    public CommonResult<Payment> getPaymentById(long id){
        System.out.println("我被访问了-----------");
        return paymentService.doSecond(id);
    }

}

3、主启动类

package com.springcloud.com.springcloud;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @author dc
 * @date 2020/7/27 - 17:41
 */
@SpringBootApplication
@Slf4j
@EnableFeignClients     //激活feign客户端
public class OrderFeignMain81 {

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

4、服务提供者的控制器

package com.springcloud.controller;

import com.springcloud.enties.CommonResult;
import com.springcloud.enties.Payment;
import com.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author dc
 * @date 2020/7/18 - 16:43
 */

@RestController
@Slf4j
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String port;

    @PostMapping("/payment/first")
    //此处必须加上@RequestBody 注解,否则不能从上一个请求模块中获取payment值。
    public CommonResult doFirst(@RequestBody Payment payment) {

        System.out.println("==========================");

        System.out.println("payment = " + payment);

        int result = paymentService.addPayment(payment);

        log.info("*******插入结果:" + result);

        if (result > 0) {
            return new CommonResult(200, "插入数据库成功 , port = " + port, result);
        }else {
            return new CommonResult(444, "插入数据库失败", null);
        }

    }


//这里必须添加@RequestParam("id")
    @GetMapping("/payment/second")
    public CommonResult doSecond(@RequestParam("id") Long id) {

        Payment payment = paymentService.findPayment(id);

        log.info("*******查找结果:" + payment);

        if (payment != null) {
            return new CommonResult(200, "数据查找成功 , port = " + port, payment);
        }else {
            return new CommonResult(444, "数据查找失败", payment);
        }
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Cloud微服务架构中,Nacos是一个注册中心和配置中心。Feign是一个声明式的Web服务客户端,它使得编写Web服务客户端变得更加容易。 使用Feign调用接口需要以下步骤: 1. 在pom.xml中添加Feign依赖 ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ``` 2. 在启动类上添加@EnableFeignClients注解启用Feign客户端 ```java @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 3. 创建接口,并使用@FeignClient注解指定调用服务名称和服务路径 ```java @FeignClient(name = "service-provider") public interface UserService { @GetMapping("/user/{id}") String getUserById(@PathVariable("id") Long id); } ``` 其中,name属性指定服务名称,GetMapping注解指定服务路径。 4. 在需要使用该服务的地注入UserService并调用法即可 ```java @RestController public class UserController { @Autowired private UserService userService; @GetMapping("/user/{id}") public String getUserById(@PathVariable("id") Long id) { return userService.getUserById(id); } } ``` 在这个例子中,我们定义了一个名为UserService的Feign客户端,指定了调用服务名称和服务路径。然后在UserController中注入了UserService并调用了其法。最终,Feign会自动将该请求转发到名为service-provider的微服务,并返回结果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值