准备工作 ,三个工程如下:
1、引入依赖
org.springframework.boot
spring-boot-starter-web
2、创建一个实体对象|Student
public class Student{
private String name;
private int age;
public Student() {
super();
}
public Student(String name,int age) {
super();
this.name = name;
this.age = age;
}
//......get set 省略......
}
3、创建一个facade接口
@RequestMapping("/student")
public interface StudentFacade {
@GetMapping("list")
public List list();
@GetMapping("get")
public Student get(@RequestParam("id") int id);
@PostMapping("add")
public Student add(@RequestBody Student student);
}
4、中创建一个基本的启动类,实际上启动类用不到,但是不加上打包会不成功
@SpringBootApplication
public class YmSpringbootFeignApplication {
public static void main(String[] args) {
SpringApplication.run(YmSpringbootFeignApplication.class, args);
}
}
1、引入依赖
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
2.1.0.RELEASE
com.ym.springboot.facade
feign-api
0.0.1-SNAPSHOT
2、创建一个实现类StudentFacadeImpl实现feign-api中的接口StudentFacade
@RestController
public class StudentFacadeImpl implements StudentFacade{
@GetMapping("list")
@Override
public List list(){
List list = new ArrayList();
list.add(new Student("A同学", 13));
list.add(new Student("B同学", 16));
list.add(new Student("C同学", 18));
return list;
}
@GetMapping("get")
@Override
public Student get(int id) {
return new Student("B同学", 16);
}
@PostMapping("add")
@Override
public Student add(@RequestBody Student student) {
return student;
}
}
3、创建一个启动类,加上@EnableEurekaClient注解
@EnableEurekaClient
@SpringBootApplication
public class YmSpringbootFeignApplication {
public static void main(String[] args) {
SpringApplication.run(YmSpringbootFeignApplication.class, args);
}
}
4、application.yml
spring:
application:
name: feign-core
server:
port: 8084
eureka:
client:
serviceUrl:
defaultZone: http://user:123456@eureka1:8761/eureka/,http://user:123456@eureka2:8761/eureka/
1、引入依赖
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
2.1.0.RELEASE
com.ym.springboot.facade
feign-api
0.0.1-SNAPSHOT
org.springframework.cloud
spring-cloud-starter-openfeign
2.1.0.RELEASE
2、创建一个 接口继承StudentFacade接口,这里通过feign获取到eureka中注册的服务feign-core
@FeignClient(name="feign-core")
public interface StudentService extends StudentFacade{
}
3、创建调用
package com.ym.springboot.consumer.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ym.springboot.consumer.service.StudentService;
import com.ym.springboot.facade.domain.Student;
@RestController
public class StudentController {
@Autowired
private StudentService studentService;
/**
* GET无参调用
* @return
*/
@GetMapping("list")
public List list(){
return studentService.list();
}
/**
* GET参数调用
* @param id
* @return
*/
@GetMapping("get")
public Student get(@RequestParam("id") int id){
return studentService.get(id);
}
/**
* POST调用
* @param student
* @return
*/
@GetMapping("add")
public Student add(){
Student student = new Student("JAVA",12);
return studentService.add(student);
}
}
4、application.yml
spring:
application:
name: feign-consumer
server:
port: 8085
compression:
enabled: true #开启GZIP压缩,以1.X版本的时候还需要配置类型,2.X设置了默认类型和默认大小
eureka:
client:
serviceUrl:
defaultZone: http://user:123456@eureka1:8761/eureka/,http://user:123456@eureka2:8761/eureka/
feign:
httpclient:
enabled: true #启用httpclient