目录
2.3 启动类添加服务发现注解 @EnableDiscoveryClient
3、添加一个feign的配置类,如日志配置可在这个类里面添加
一、什么是feign
Feign是Netflix开发的声明式,模板化的HTTP客户端。简化了HTTP的远程服务的开发。
Feign是在RestTemplate和Ribbon的基础上进一步封装,使用RestTemplate实现Http调用,使用Ribbon实现负载均衡。我们可以看成 Feign = RestTemplate+Ribbon
二、入门案例
客户端调用http://service-provider/hello 调用过程简要说明
大致流程如下
1、服务提供者(生产者)注册服务到注册中心Consul
2、客户端(服务调用者)从注册中心获取服务列表
3、客户端根据配置的负载均衡规则选择其中一个服务调用 (上图是调用:http://localhost:8082/hello)
4、服务提供者(生产者)返回调用结果
1、启动consul
这里在windows环境下进入到consul所在目录以dev模式启动
D:\JavaRevelant\software\consul>consul agent -dev -client=0.0.0.0
2、创建服务提供者Provider1、和Provider2
2.1 pom相关依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2.2 application.yml配置
spring:
application:
name: service-provider
cloud:
consul:
discovery:
register: true
service-name: ${spring.application.name}
prefer-ip-address: true
ip-address: ${spring.cloud.client.ip-address}
instance-id: ${spring.application.name}-01
port: ${server.port}
host: localhost
port: 8500
server:
port: 8081
2.3 启动类添加服务发现注解 @EnableDiscoveryClient
2.4 简单Controller类,测试用
至此,Provider1服务提供者创建成功
2.5 创建第二个服务提供者
根上边同样配置创建Provider2服务提供者,只需修改application.yml配置 (服务注册的instance-id和server.port端口号)
spring:
application:
name: service-provider
cloud:
consul:
discovery:
register: true
service-name: ${spring.application.name}
prefer-ip-address: true
ip-address: ${spring.cloud.client.ip-address}
instance-id: ${spring.application.name}-02
port: ${server.port}
host: localhost
port: 8500
server:
port: 8082
3、创建服务消费者FeignConfiguration项目
项目结构
1、pom相关依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2、application.yml
spring:
application:
name: FeignConfiguration
cloud:
consul:
host: localhost
port: 8500
discovery:
register: false
logging:
level:
com.dolphin.feignconfiguration: DEBUG
server:
port: 9005
service-provider:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule # 随机访问规则,这里配置跟Ribbon一致
3、添加一个feign的配置类,如日志配置可在这个类里面添加
package com.dolphin.feignconfiguration.config;
import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FeignConfiguration {
// @Bean //若使用原生feign配置放开此注解,一般默认使用openFeign
// public Contract feignContract() {
//
// return new feign.Contract.Default();
//
// }
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}
4、feign调用接口
package com.dolphin.feignconfiguration.feign;
import com.dolphin.feignconfiguration.config.FeignConfiguration;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
//@FeignClient(contextId = "feignClient",name = "service-provider",configuration = FeignConfiguration.class)
@FeignClient(name = "service-provider",configuration = FeignConfiguration.class)
public interface MyFeignClient {
@RequestMapping("/hello")
public String hello();
}
5、controller类
package com.dolphin.feignconfiguration.controller;
import com.dolphin.feignconfiguration.feign.MyFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
MyFeignClient myFeignClient;
@RequestMapping
public String hello() {
return myFeignClient.hello();
}
}
6、启动类开启feign调用
@EnableDiscoveryClient
@EnableFeignClients
package com.dolphin.feignconfiguration;
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 FeignConfigurationApplication {
public static void main(String[] args) {
SpringApplication.run(FeignConfigurationApplication.class, args);
}
}
启动项目可测试结果为随机访问2个服务
三、feign的压缩请求和响应
如果要对请求和响应进行压缩处理,则应在配置文件中配置一下代码:
feign:
compression:
request:
enabled: true #支持请求压缩
mime-types: text/xml,application/xml,application/json #支持媒体类型列表
min-request-size: 2048 #支持请求媒体文件大小
response:
enabled: true #支持响应压缩
四、feign的请求参数
如果在feign接口中添加请求参数,则跟我们Controller中参数格式是一致的
@FeignClient(name = "service-provider",configuration = FeignConfiguration.class)
public interface MyFeignClient {
@RequestMapping("/hello")
public String hello(@RequestParam String a);
@RequestMapping("/hello2")
public String hello2(@RequestBody User user);
}
五、feign中记录日志
日子等级 | 说明 |
---|---|
NONE | 不记录(默认) |
BASIC | 只记录请求方法、URL、响应状态码和执行时间 |
HEADERS | 记录基本信息、请求和响应标题 |
FULL | 记录请求、响应的标题、正文和元数据 |
application.yml中添加如下配置开启日志配置
feign:
client:
config:
service-provider: #此处写的是服务名称,针对我们feign微服务的配置,如果是default就是全局配置
loggerLevel: full #配置Feign的日志级别,相当于代码配置方式中的Logger
#在application.yml中使用 logging.level.<Feign客户端对应的接口的全限定名> 的参数配置格式来开启指定客户端日志
logging:
level:
com.bjpowernode.feign: debug
这里通过配置类的方式也可以,上边案例中就是通过配置类的方式,不过要注意的是,配置类方式指定的配置类需要放在@ComponentScan不能扫描到的地方。