Feign负载均衡
1、概述
官网解释:
http://projects.spring.io/spring-cloud/spring-cloud.html# spring-cloud-feign
Feign:是一个声明式WebService客户端。 使用Feign能让编写Web Service客户端更加简单,它的使用方法是定义一个接口,然后在上面添加注解,同时也支持JAX-RS标准的注解。 Feign也支持可拔插式的编码器和解码器。Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters. Feign可以与Eureka和Ribbon组合使用以支持负载均衡
2、feign使用步骤
第一步:参考springcloud-study-consumer-dept-80模块,新建springcloud-study-consumer-dept-feign模块
- 修改pom文件
<!--增加Feign的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
- 修改主启动类,增加@EnableFeignClients注
package com.blj.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages = {"com.blj.springcloud"})
public class DeptFeignClient_App {
public static void main(String[] args) {
SpringApplication.run(DeptFeignClient_App.class,args);
}
}
第二步:修改springcloud-study-api
- 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>springcloud-study</artifactId>
<groupId>com.blj</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-study-api</artifactId>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--增加Feign的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
</dependencies>
</project>
- 在springcloud-study-api中新建DeptClientService接口类,并增加注解@FeignClient
package com.blj.springcloud.service;
import com.blj.springcloud.entities.Dept;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;
@FeignClient(value = "STUDY-SPRINGCLOUD-DEPT")
public interface DeptClientService {
@PostMapping(value = "/dept/add",method = RequestMethod.POST)
public boolean add(Dept dept);
@RequestMapping(value = "/dept/get/{deptno}",method = RequestMethod.GET)
public Dept get(@PathVariable("deptno") Long deptno);
@RequestMapping(value = "/dept/list",method = RequestMethod.GET)
public List list();
}
第三步:在springcloud-study-consumer-dept-feign修改Controller,修改基于上述配置的接口
package com.blj.springcloud.controller;
import com.blj.springcloud.entities.Dept;
import com.blj.springcloud.service.DeptClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@RestController
@RequestMapping(value = "consumer")
public class DeptController_Consumer {
@Autowired
private DeptClientService deptClientService;
@RequestMapping(value = "/dept/add")
public boolean add(@RequestBody Dept dept){
//三个参数:url,requestMap ResponseBean.class
return deptClientService.add(dept);
}
@GetMapping("/dept/get/{deptno}")
public Dept get(@PathVariable("deptno")Long deptno){
//三个参数:url,requestMap ResponseBean.class
return deptClientService.get(deptno);
}
@GetMapping("/dept/list")
public List list(){
//三个参数:url,requestMap ResponseBean.class
return deptClientService.list();
}
}