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所显示的微服务名称去调用 服务暴露对外提供的方法。

最新文章

  1. HDU 5742 Chess SG函数博弈
  2. java反射案例
  3. 顺序队列的C语言实现
  4. Map.Entry用法示例
  5. jdk配置环境变量(windows)
  6. selenium通过WebDriverWait实现ajax测试
  7. HTML5 开发APP(打开相册以及图片上传)
  8. 每天学点SpringMVC-拦截器
  9. Gradle-----搭建简单的Gradle项目
  10. Android Studio的构建系统:Gradle
  11. Python学习之数组类型一:
  12. Alpha 冲刺 (5/10)
  13. DevExpress--TreeList节点添加图片
  14. 【LeetCode225】 Implement Stack using Queues★
  15. MYSQL的基本函数 (加密函数)
  16. Lowest Common Ancestor of a Binary Search Tree(Java 递归与非递归)
  17. 20155208徐子涵 2016-2017-2 《Java程序设计》第9周学习总结
  18. Java实现二分法排序
  19. SSDT Hook实现内核级的进程保护
  20. 分类器评估方法:精确度-召回率-F度量(precision-recall-F_measures)

热门文章

  1. bash默认组合按键
  2. JVM类加载机制小结
  3. 每日一题 - 剑指 Offer 53 - I. 在排序数组中查找数字 I
  4. css定位方式有哪几种?
  5. 利用Cython对python代码进行加密
  6. All elments are null 异常
  7. cf1216E2 Numerical Sequence (hard version)(思维)
  8. day21 模块与包+软件开发目录规范
  9. 01-flask旅行网系统功能设计
  10. 上亿数据怎么玩深度分页?兼容MySQL + ES + MongoDB