OpenFeign使用步骤
2024-09-07 11:59:05
1. 新建 cloud-consumer-feign-order80
2. pom.xml
<?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>cloud2020</artifactId>
<groupId>com.atguigu.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>
<!--引入自定义的api通用包,可以使用Payment支付Entity-->
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</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. application.yml
server:
port: 80 eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka
4. 主启动类
package com.atguigu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients //开启Feign
public class OrderFeignMain80 { public static void main(String[] args) {
SpringApplication.run(OrderFeignMain80.class,args);
}
}
5. 业务类
//业务逻辑接口 + @FeignClient 配置调用provider服务
package com.atguigu.springcloud.service;
import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@Component
@FeignClient(value = "CLOUD-PROVIDER-SERVICE") //指定调用哪个微服务
public interface PaymentFeignService { @GetMapping(value = "/payment/get/{id}") //哪个地址
CommonResult<Payment> getPaymentById(@PathVariable("id") Long id);
} 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package com.atguigu.springcloud.controller;
import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import com.atguigu.springcloud.service.PaymentFeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@Slf4j
public class OrderFeignController {
@Resource
private PaymentFeignService paymentFeignService; @GetMapping(value = "/consumer/payment/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
return paymentFeignService.getPaymentById(id);
}
}
6. 测试
先启动2个eureka集群 7001/7002
在启动2个微服务 8001/8002
启动OpenFeign 80
多次刷新,8001和8002会依次出现,Feign自带负载均衡配置项
总结:
客户端服务接口根据 @FeigClient所显示的微服务名称去调用 服务暴露对外提供的方法。
最新文章
- HDU 5742 Chess SG函数博弈
- java反射案例
- 顺序队列的C语言实现
- Map.Entry用法示例
- jdk配置环境变量(windows)
- selenium通过WebDriverWait实现ajax测试
- HTML5 开发APP(打开相册以及图片上传)
- 每天学点SpringMVC-拦截器
- Gradle-----搭建简单的Gradle项目
- Android Studio的构建系统:Gradle
- Python学习之数组类型一:
- Alpha 冲刺 (5/10)
- DevExpress--TreeList节点添加图片
- 【LeetCode225】 Implement Stack using Queues★
- MYSQL的基本函数 (加密函数)
- Lowest Common Ancestor of a Binary Search Tree(Java 递归与非递归)
- 20155208徐子涵 2016-2017-2 《Java程序设计》第9周学习总结
- Java实现二分法排序
- SSDT Hook实现内核级的进程保护
- 分类器评估方法:精确度-召回率-F度量(precision-recall-F_measures)