Feign是一个声明式的web service客户端,它使得编写web service客户端更为容易。创建接口,为接口添加注解,即可使用Feign。Feign可以使用Feign注解或者JAX-RS注解,还支持热插拔的编码器和解码器。Spring Cloud为Feign添加了Spring MVC的注解支持,并整合了Ribbon和Eureka来为使用Feign时提供负载均衡。
https://blog.csdn.net/miaodichiyou/article/details/104160284
1、启动SpringCloud高可用服务注册中心(Eureka)搭建的注册中心
2、启动三个springcloud-eureka-provider实例,端口分别为8001,8002和8003
4.0.0
com.springcloud
springcloud-root
0.0.1-SNAPSHOT
springcloud-feign
springcloud-feign
http://maven.apache.org
UTF-8
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-openfeign
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
junit
junit
3.8.1
test
org.springframework.boot
spring-boot-maven-plugin
spring:
application:
name: springcloud-feign
freemarker:
prefer-file-system-access: false
security:
user:
name: admin
password: 123456
server:
port: 8101
eureka:
instance:
hostname: eureka-feign.com
instance-id: eureka-feign
client:
service-url:
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer1.com:8897/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer2.com:8898/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer3.com:8899/eureka/
修改C:\Windows\System32\drivers\etc\hosts
127.0.0.1 eureka-feign.com
FeignApplication.java
在程序的启动类加上注解@EnableFeignClients开启Feign Client功能
package org.springcloud.feign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
}
FeignConsumer.java
新建一个FeignConsumer的接口:
package org.springcloud.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Component
/*指定这个接口所要调用的提供者服务名称 */
@FeignClient(value = "springcloud-eureka-provider",configuration = FeignConfig.class)
public interface FeignConsumer {
@GetMapping(value = "/hi")
String sayHiFromEurekaProvider(@RequestParam(value = "name")String name);
}
FeignConfig .java
在FeignConfig类上加上@Configuration注解,表明这是一个配置类,并注入一个BeanName为feignRetryer的Retryer的Bean。可使feign在远程调用失败后会进行重试。
package org.springcloud.feign;
import java.util.concurrent.TimeUnit;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import feign.Retryer;
@Configuration
public class FeignConfig {
@Bean
public Retryer feignRetryer(){
return new Retryer.Default(100,TimeUnit.SECONDS.toMillis(1),5);
}
}
FeignService .java
Service层FeignService类注入FeignConsumer的Bean
package org.springcloud.feign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class FeignService {
@Autowired
FeignConsumer feignConsumer;
public String hi(String name){
return feignConsumer.sayHiFromEurekaProvider(name);
}
}
FeignController.java
FeignController类调用FeignService的hi()方法,FeignService通过FeignConsumer远程调用springcloud-eureka-provider服务的API接口“/hi”。
package org.springcloud.feign;
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;
/*调用提供者的home方法*/
@RestController
public class FeignController {
@Autowired
FeignService feignService;
@GetMapping("/hi")
public String hi(@RequestParam(defaultValue = "zhaojq",required = false)String name){
return feignService.hi(name);
}
}
Feign源码实现过程如下:
运行启动类,访问http://eureka-peer1.com:8897/
http://eureka-peer2.com:8898/,http://eureka-peer3.com:8899/ 结果同上。
服务消费者实例已成功注册。