一.基础知识
1. Feign是一个声明式的web service客户端,它让写web service客户端更加容易。
2.使用Feign只需创建一些接口和一些注解便可,它已经支持Feign和JAX-RS注解并且是可插拔的。
两个坑:1. @GetMapping不支持 ,改为requestmapping method=rquestmethod.get 2. @PathVariable得设置value
3.Spring cloud为Feign添加了Spring MVC的注解。
4.Spring Cloud整合Ribbon和Eureka以提供负载均衡的能力。
注意两点:
两个坑:1. 在feigin的接口中不能用GetMapping ,改为requestmapping method=rquestmethod.get 2. @PathVariable得设置@PathVariable(“xxx”)
5.逻辑思路:consumer端的访问请求(8004)-----》controller----》fegin 接口(接口中配置要调用的模块)---》访问具体某个模块的方法。
结论:client,server要通信,分别注册到eureka上,然后通过Fegin的接口,便可实现client访问server。
6.fegin的接口中定义要访问的项目,并且方法要和访问模块的方法保持一致,访问路径也要一样。
二.通过get请求访问provider
1.pom文件:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 注册eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- 打印日志 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- feigin的依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
2.resources:application.yml配置文件
server:
port: 8004
spring:
application:
name: ms-fegin-consumer
eureka:
client:
healthcheck:
enabled: true
serviceUrl:
defaultZone: http://ljf:123@localhost:8761/eureka
instance:
prefer-ip-address: true
instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
3.定义fegin的接口: 在feigin的接口中不能用GetMapping ,改为requestmapping method=rquestmethod.get 2. @PathVariable得设置@PathVariable(“xxx”)
package com.ljf.weifuwu.springcloud.fegin.service;
import com.ljf.weifuwu.springcloud.fegin.model.FeginUser;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient("ms-eureka-provider")
public interface UserFeginClientServvice {
// 两个坑:1. 在feigin的接口中不能用GetMapping ,改为requestmapping method=rquestmethod.get 2. @PathVariable得设置@PathVariable(“xxx”)
@RequestMapping(value = "/eureka-provider/{id}", method = RequestMethod.GET)
public FeginUser getSingleUser(@PathVariable("id") Long id);
}
4.model:
package com.ljf.weifuwu.springcloud.fegin.model;
import java.math.BigDecimal;
public class FeginUser {
private Long id;
private String username;
private String name;
private Short age;
private BigDecimal balance;
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Short getAge() {
return this.age;
}
public void setAge(Short age) {
this.age = age;
}
public BigDecimal getBalance() {
return this.balance;
}
public void setBalance(BigDecimal balance) {
this.balance = balance;
}
}
5.在controller:引用注解
package com.ljf.weifuwu.springcloud.fegin.controller;
import com.ljf.weifuwu.springcloud.fegin.service.UserFeginClientServvice;
import com.ljf.weifuwu.springcloud.fegin.model.FeginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FeginUserController
{
@Autowired
private UserFeginClientServvice ucs;
/**
* 这个是get请求
* @param id
* @return
*/
@GetMapping("/fegin-consumer/{id}")
public FeginUser findById(@PathVariable Long id) {
System.out.println("feigin的请求!!!!!!!!!!1"+id);
FeginUser u= this.ucs.getSingleUser(id);
System.out.println("u:"+u.getName());
return this.ucs.getSingleUser(id);
}
}
6.在启动类上添加fegin的注解:
package com.ljf.weifuwu.springcloud.fegin;
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;
/**
* Hello world!
*
*/
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class FeginConsumerApp
{
public static void main( String[] args )
{
SpringApplication.run(FeginConsumerApp.class, args);
System.out.println( "fegin - consumer启动起来了!" );
}
}
7.启动服务:ms-eureka-center(8761)、ms-eureka-provider(9701)、ms-fegin-consumer(8004)
8.访问:http://localhost:8004/fegin-consumer/1 报错
9.在yml文件新加:
#将feging中的超时熔断机制给关掉
feign:
hystrix:
enabled: false
重新启动服务,进行http://localhost:8004/fegin-consumer/1访问
三.通过post请求访问provider
1.修改ms-eureka-center中,新加一个方法:
2.在fegin的接口中,添加调用方法:
3.controller层的调用:
4.重启服务,进行访问:http://localhost:8004/fegin-consumer-post?id=123&username=ljf&name=jurf