Feign是简化Java HTTP客户端开发的工具,是通过处理注解生成Request,从而实现简化Http API开发的目的,即开发人员可以使用注解的方式定制Request api模板,在发送请求http requst之前,feign通过处理注解的方式替换掉request模板中的参数,这种实现方式更为直接,可理解。
Feign是一种声明式,模板化的HTTP客户端,可以做到使用HTTP请求远程服务时能与调用本地方法一样的编码体验,感知不到是远程方法和HTTP请求.
1,服务提供者microservice-provider-user:
package com.taikang.it.mobile.jpa;
import com.taikang.it.mobile.bean.Girl;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface GirlRepository extends JpaRepository<Girl, Integer> {
List<Girl> findByAge(Integer age);
}
package com.taikang.it.mobile.controller;
import com.taikang.it.mobile.bean.Girl;
import com.taikang.it.mobile.jpa.GirlRepository;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/provider-user")
public class GirlController {
@Autowired
private GirlRepository repository;
@GetMapping(value = "/girls/{id}")
public Girl queryOne(@PathVariable("id") Integer id) {
return repository.findOne(id);
}
}
package com.taikang.it.mobile.bean;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Girl {
@Id
@GeneratedValue
private Integer id;
private String cupSize;
private Integer age;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCupSize() {
return cupSize;
}
public void setCupSize(String cupSize) {
this.cupSize = cupSize;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
feign项目
microservice-consumer-movie-feign:
package com.taikang.it.mobile.feign;
import com.taikang.it.mobile.bean.Girl;
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(name = "microservice-provider-user")
public interface GirlFeignClient {
@RequestMapping(value = "/provider-user/girls/{id}", method = RequestMethod.GET)
Girl findById(@PathVariable("id") Integer id);
}
package com.taikang.it.mobile.controller;
import com.taikang.it.mobile.bean.Girl;
import com.taikang.it.mobile.feign.GirlFeignClient;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "provider-feign")
public class MovieController {
@Autowired
private GirlFeignClient feignClient;
@GetMapping("/girls/{id}")
public Girl findById(@PathVariable Integer id) {
System.out.println(id);
return this.feignClient.findById(id);
}
}
启动eureka,启动2个user实例,启动feign,多次访问http://localhost:8010/provider-feign/girls/4
发现两个user实例都会打印sql日志