SpringCloud-OpenFeign-服务调用

一、介绍

(1)是一款客户端组件,SpringCloud在Feign的基础上进行二次开发得到兼容SpringMVC的OpenFegin。
(2)简化了RestTemplate的请求操作代码(如编写URL、参数等),可提前编写好接口指定调用哪个微服务上的接口,即一处编写多处使用。
(3)OpenFeign集成了Ribbon,即支持负载均衡,自动启用。
在这里插入图片描述

二、使用

(1)新建子模块
在这里插入图片描述
(2)编写pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>demo20220821</artifactId>
        <groupId>com.wsh.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-consumer-feign-order80</artifactId>

    <dependencies>
        <!--openfeign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!--eureka client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>com.wsh.springcloud</groupId>
            <artifactId>cloud-api-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--一般基础通用配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>


</project>

(3)编写yml文件

server:
  port: 80

spring:
  application:
    name: cloud-order-service

eureka:
  client:
    #    客户端设置为true
    register-with-eureka: true
    #    客户端设置为true
    fetch-registry: true
    service-url:
      #      defaultZone: http://localhost:7001/eureka
      defaultZone: http://eureka1.com:7001/eureka, http://eureka2.com:7002/eureka

(4)编写启动类
在这里插入图片描述

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

(5)开启OpenFeign功能
在这里插入图片描述

@EnableEurekaClient
@EnableFeignClients
@SpringBootApplication
public class OrderFeignMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderFeignMain80.class, args);
    }
}

(6)编写Feign要调用其他服务的接口

@FeignClient("CLOUD-PAYMENT-SERVICE")
@Component
public interface OrderService {

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public CommonResult<Payment> selectById(@PathVariable("id") Long id);
}

对应
在这里插入图片描述
(7)编写controller

@Slf4j
@RequestMapping("/order")
@RestController
public class OrderFeignController {

    @Autowired
    private OrderFeignSerice orderFeignSerice;

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public CommonResult<Payment> selectById(@PathVariable("id") Long id){
        return orderFeignSerice.selectById(id);
    }
}

(8)测试
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值