上一节中,使用了RestTemplate结合Ribbon来消费服务,并进行了负载均衡。本节将使用Feign来远程调度其他服务,Feign使用声明式API接口的风格,将HTTP客户端绑定到它的内部,使HTTP客户端调用过程变的更加简单。
启动一个Server端,端口为:8761,开启两个Client实例,接口分别是:8764和8765。然后新建一个名为eureka-feign-client的Maven工程,pom文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.hzlitai</groupId>
<artifactId>eureka-memo</artifactId>
<version>1.1-SNAPSHOT</version>
</parent>
<groupId>com.hzlitai</groupId>
<artifactId>eureka-feign-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-feign-client</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置文件application.yml代码如下:
spring:
application:
name: eureka-feign-client
server:
port: 8767
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
建立Feign的配置类
package com.hzlitai.eurekafeignclient.configuration;
import feign.Retryer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import reactor.retry.Retry;
import java.util.concurrent.TimeUnit;
@Configuration
public class FeignConfig {
@Bean
public Retryer feignRetrayer(){
return new Retryer.Default(100, TimeUnit.SECONDS.toMillis(1),5 );
}
}
建立EurekaFeignClient接口调用远程服务,代码如下:
package com.hzlitai.eurekafeignclient.service;
import com.hzlitai.eurekafeignclient.configuration.FeignConfig;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
//value值为服务名称
@FeignClient(value = "eureka-client",configuration = FeignConfig.class)
public interface EurekaFeignClient {
@GetMapping("/hi")
String sayHiFromEurekaClient(@RequestParam(value = "name") String name);
}
建立Service调用接口提供的方法,代码如下:
package com.hzlitai.eurekafeignclient.service.impl;
import com.hzlitai.eurekafeignclient.service.EurekaFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class HiService {
@Autowired
EurekaFeignClient eurekaFeignClient;
public String sayHi(String name){
return eurekaFeignClient.sayHiFromEurekaClient(name);
}
}
建立Controller,调用Service函数,代码如下:
package com.hzlitai.eurekafeignclient.controller;
import com.hzlitai.eurekafeignclient.service.impl.HiService;
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;
@RestController
public class HiConroller {
@Autowired
HiService hiService;
@GetMapping("/sayHi")
public String sayHi(@RequestParam String name){
return hiService.sayHi(name);
}
}
访问http://localhost:8767/sayHi?name=zhuheliang即可。
Feign工作原理介绍
Feign通过包扫描注入FeignClient的bean,程序在启动时,会检查是否有@EnableFeignClients注解,如果有该注解,则开始开启包扫描,扫描被@FeignClient注解的接口。
Feign中使用HttpClient和OkHttp
Feign中默认使用HttpURLConnection来实现网络请求,也可以采用引入依赖的方式,使用其他网络请求框架;
配置文件增加配置信息:
feign:
httpclient:
enabled: true
引入HttpClient:
<dependency>
<groupId>com.netflix.feign</groupId>
<artifactId>feign-httpclient</artifactId>
<version>RELEASE</version>
</dependency>
引入OkHttp:
<dependency>
<groupId>com.netflix.feign</groupId>
<artifactId>feign-okhttp</artifactId>
<version>RELEASE</version>
</dependency>