首页 > 其他分享 >SpringCloud学习七:Feign负载均衡

SpringCloud学习七:Feign负载均衡

时间:2022-10-31 13:42:56浏览次数:57  
标签:Feign SpringCloud 负载 dept springcloud 80 consumer feign cloud


文章目录

  • ​​一、概念​​
  • ​​二、用处​​
  • ​​2.1 简单​​
  • ​​2.2 Feign集成了Ribbon​​
  • ​​三、实验​​
  • ​​第一步:创建客户端项目:springcloud-consumer-dept-feign​​
  • ​​第二步: 导入依赖​​
  • ​​第三步:配置application.yml​​
  • ​​第四步:修改服务提供者:springcloud-api​​
  • ​​第五步:在springcloud-consumer-dept-feign项目的controller也要做相应的修改​​
  • ​​第六步:修改80主配置类​​

一、概念

SpringCloud学习七:Feign负载均衡_spring

二、用处

2.1 简单

SpringCloud学习七:Feign负载均衡_客户端_02

2.2 Feign集成了Ribbon

SpringCloud学习七:Feign负载均衡_云原生_03

三、实验

第一步:创建客户端项目:springcloud-consumer-dept-feign

SpringCloud学习七:Feign负载均衡_客户端_04


创建一个springcloud-consumer-dept-feign空maven的空项目,这也是一个消费者,端口也是80,只是这个消费者使用Feign实现的负载均衡

SpringCloud学习七:Feign负载均衡_云原生_05

第二步: 导入依赖

<dependencies>
<!--feign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
<!--Ribbon-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
<!--导入Ribbon的同时要导入erueka,因为它要发现服务从那里来-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
<dependency>
<groupId>com.wu</groupId>
<artifactId>springcloud-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--热部署工具-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>

第三步:配置application.yml

和springcloud-consumer-dept-80项目的一摸一样

server:
port: 80

#Eureka配置
eureka:
client:
register-with-eureka: false #不向eureka中注册自己
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

第四步:修改服务提供者:springcloud-api

添加依赖

<!--feign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>

从前面了解到,Feign是通过注解接口,而Ribbon是通过微服务名字,那么下面给springcloud-api添加一个service包

SpringCloud学习七:Feign负载均衡_负载均衡_06


书写接口

@Component
//Feign客户端的服务名
@FeignClient(value = "SPRINGCLOUD-PROVIDER-DEPT")
public interface DeptClientService {

@GetMapping("/dept/get/{id}")
Dept queryById(@PathVariable("id") Long id);

@PostMapping("/dept/add")
boolean addDept(Dept dept);

@RequestMapping("/dept/list")
List<Dept> queryAll();

}

第五步:在springcloud-consumer-dept-feign项目的controller也要做相应的修改

DeptConsumerController

@Controller
public class DeptConsumerController {

//这里直接把springcloud-api的DeptClientService接口注入进来
@Autowired
private DeptClientService deptClientService = null;
//然后通过接口来调用方法,调用方法后,它会调用上面类对应的方法,最后调用其服务名字的接口
@RequestMapping("/consumer/dept/get/{id}")
@ResponseBody
public Dept queryById(@PathVariable("id") Long id) {
return this.deptClientService.queryById(id);
}

@RequestMapping("/consumer/dept/add")
@ResponseBody
public boolean add(Dept dept) {
return this.deptClientService.addDept(dept);
}

@RequestMapping("/consumer/dept/list")
@ResponseBody
public List<Dept> queryAll() {
return this.deptClientService.queryAll();
}

}

第六步:修改80主配置类

SpringCloud学习七:Feign负载均衡_spring cloud_07


最后还要在启动类上添加FeignClient注解

FeignDeptConsumer_80
@SpringBootApplication
@EnableFeignClients(basePackages = {"com.kuang.springcloud"})
public class FeignDeptConsumer_80 {
public static void main(String[] args) {
SpringApplication.run(FeignDeptConsumer_80.class, args);
}
}

最后启动7001、… 、8001、… 、feign的80项目,测试


标签:Feign,SpringCloud,负载,dept,springcloud,80,consumer,feign,cloud
From: https://blog.51cto.com/u_15854304/5809333

相关文章