Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特 性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。
简而言之:
Feign 采用的是基于接口的注解
Feign 整合了ribbon,具有负载均衡的能力
整合了Hystrix,具有熔断的能力
项目图
1.1 在pom.xml中加入以下依赖
4.0.0
com.example
eurekaserver
0.0.1-SNAPSHOT
jar
eurekaserver
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.12.RELEASE
UTF-8
UTF-8
1.8
Edgware.SR3
org.springframework.cloud
spring-cloud-starter-eureka-server
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
1.2 application.properties 配置
server.port=8089
eureka.instance.hostname=127.0.0.1
#不要向注册中心注册自己
eureka.client.register-with-eureka=false
#禁止检索服务
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://127.0.0.1:8089/eureka
spring.application.name=eurekserver
1.3 开启eureka
package SpringCloud3;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* Hello world!
*
*/
@EnableEurekaServer
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
2.1.1 在pom.xml中加入以下依赖
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.5.RELEASE
com.example
demo
0.0.1-SNAPSHOT
demo
Demo project for Spring Boot
1.8
Greenwich.SR1
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-ribbon
1.3.4.RELEASE
org.springframework.boot
spring-boot-starter-test
test
com.google.guava
guava
25.1-jre
com.diffplug.guava
guava-cache
19.0.0
org.springframework.cloud
spring-cloud-starter-netflix-ribbon
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
2.1.2 application.properties 配置
server.port=8115
#设置应用的名称
spring.application.name=provider-demo2-ribbon
#服务注册的Eureka Server地址
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8089/eureka
##http://shumeigang:[email protected]:8081/eureka
eureka.instance.prefer-ip-address=true
#设置注册ip
#表示是否将自己注册在EurekaServer上,默认为true。由于当前应用就是EurekaServer,所以置为false
eureka.client.register-with-eureka=true
#表示表示是否从EurekaServer获取注册信息,默认为true。单节点不需要同步其他的EurekaServer节点的数据
eureka.client.fetch-registry=true
#自定义应用实例id
#健康检查
##eureka.client.healthcheck.enabled=true
2.1.3 Controller
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;
import org.springframework.web.client.RestTemplate;
@RestController
public class ProviderController {
@GetMapping("/Provider/hi")
public String sayHi(){
return "provider-demo22";
}
}
2.2.1 在pom.xml中加入以下依赖
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.5.RELEASE
com.example
demo
0.0.1-SNAPSHOT
demo
Demo project for Spring Boot
1.8
Greenwich.SR1
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-ribbon
1.3.4.RELEASE
org.springframework.boot
spring-boot-starter-test
test
com.google.guava
guava
25.1-jre
com.diffplug.guava
guava-cache
19.0.0
org.springframework.cloud
spring-cloud-starter-netflix-ribbon
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
2.2.2 application.properties 配置
server.port=8114
#设置应用的名称
spring.application.name=provider-demo3-ribbon
#服务注册的Eureka Server地址
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8089/eureka
##http://shumeigang:[email protected]:8081/eureka
eureka.instance.prefer-ip-address=true
#设置注册ip
#表示是否将自己注册在EurekaServer上,默认为true。由于当前应用就是EurekaServer,所以置为false
eureka.client.register-with-eureka=true
#表示表示是否从EurekaServer获取注册信息,默认为true。单节点不需要同步其他的EurekaServer节点的数据
eureka.client.fetch-registry=true
#自定义应用实例id
#健康检查
##eureka.client.healthcheck.enabled=true
2.2.3 Controller
package com.example.demo.controller;
import com.example.demo.Entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
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;
import org.springframework.web.client.RestTemplate;
@RestController
public class ProviderController {
@GetMapping("/Provider/hi")
public String sayHi(){
return "provider-demo3";
}
}
项目图
3.1 在pom.xml中加入以下依赖
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.5.RELEASE
com.example
demo
0.0.1-SNAPSHOT
demo
Demo project for Spring Boot
1.8
Greenwich.SR1
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-openfeign
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
3.1 application.properties 配置
server.port=8707
#设置应用的名称
spring.application.name=FeignClient3
#服务注册的Eureka Server地址
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8089/eureka
##http://shumeigang:[email protected]:8081/eureka
eureka.instance.prefer-ip-address=true
#设置注册ip
#表示是否将自己注册在EurekaServer上,默认为true。由于当前应用就是EurekaServer,所以置为false
eureka.client.register-with-eureka=true
#表示表示是否从EurekaServer获取注册信息,默认为true。单节点不需要同步其他的EurekaServer节点的数据
eureka.client.fetch-registry=true
3.2 Feign 接口
package com.example.demo.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
//@FeignClient("PROVIDER-DEMO3-RIBBON")
@FeignClient(value = "PROVIDER-DEMO3-RIBBON")
//@FeignClient(value = "PROVIDER-DEMO3-RIBBON",url="http://127.0.0.1:8115/")
public interface FeignService {
@GetMapping("/Provider/hi")
String sayHi();
}
3.3 Feign Controller
package com.example.demo.Controller;
import com.example.demo.feign.FeignFallBack;
import com.example.demo.feign.FeignService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by cong on 2018/5/17.
*/
@RestController
public class ConsumerController {
@Autowired
private FeignService feignService;
@GetMapping("/Consumer/hi")
public String sayHi(){
return feignService.sayHi();
}
}
3.4 Feign 起动
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
运行结果:
1 先运行 eureka
2, 2个生产者服务器
3 Feign 起动
eureka结果:
http://127.0.0.1:8089/
Feign 结果 负载均衡
http://127.0.0.1:8707/Consumer/hi
provider-demo22
http://127.0.0.1:8707/Consumer/hi
provider-demo3