**Feign是在Ribbon的基础上进行了一次改进,采用接口方式,将需要调用的其他服务的方法抽象,cloud会动态代理成类,进行调用.使用时直接加上@FeignClient注解,类似于@Mapper**
Feign服务客户端POM
<dependencies>
<!--web模块开发依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<!--健康监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--feign依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
</dependencies>
application.yml
server:
port: 800
spring:
application:
name: micoreservicecloud-feign800
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:7001/eureka,http://127.0.0.1:7002/eureka
instance:
instance-id: micoreservicecloud-feign800
prefer-ip-address: true
配置ribbon
server:
port: 800
spring:
application:
name: micoreservicecloud-feign800
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:7001/eureka,http://127.0.0.1:7002/eureka
instance:
instance-id: micoreservicecloud-feign800
prefer-ip-address: true
MICORESERVICECLOUD-DEPT:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #配置规则 随机
# NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule #配置规则 轮询
# NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RetryRule #配置规则 重试
# NFLoadBalancerRuleClassName: com.netflix.loadbalancer.WeightedResponseTimeRule #配置规则 响应时间权重
# NFLoadBalancerRuleClassName: com.netflix.loadbalancer.BestAvailableRule #配置规则 最空闲连接策略
ConnectTimeout: 500 #请求连接超时时间
ReadTimeout: 1000 #请求处理的超时时间
OkToRetryOnAllOperations: true #对所有请求都进行重试
MaxAutoRetriesNextServer: 2 #切换实例的重试次数
MaxAutoRetries: 1 #对当前实例的重试次数
创建接口
@FeignClient(name = "MICORESERVICECLOUD-DEPT") //服务提供端在Eureka中注册的名称
public interface FiegnInterface {
@RequestMapping("/provider/test")
String test();
}
启动类添加注解
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages = "com.api") //扫描接口包
public class ConsumerFeignApp {
public static void main(String[] args) {
SpringApplication.run(ConsumerFeignApp.class);
}
}
注册三个服务提供者
这里只写application.yml
server:
port: 8001
spring:
application:
name: micoreservicecloud-dept //在Eureka注册的名称 三台服务提供者必须一样
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/xiaowu
username: root
password: root
dbcp2:
min-idle: 5
initial-size: 5
max-total: 5
max-wait-millis: 200
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:7001/eureka,http://127.0.0.1:7002/eureka
instance:
instance-id: micoreservicecloud-dept8001 //实例名称 各自服务必须不一样
prefer-ip-address: true #访问路径可以显示IP地址
info:
app.name: micoreservicecloud-dept8001
company.name: www.microservicecloud.com
build.artifactId: $project.artifactId$
build.version: $project.version$
然后在yaml配置中自定义适合的注解 进行测试
(restTemplate+ribbon)和feign负载均衡区别
ribbon是一个基于HTTP和TCP客户端的负载均衡工具,在客户端配置Ribbon服务列表,使用HttpClient或RestTemplate模拟http请求,步骤相当繁琐
Feign是在Ribbon的基础上进行了一次改进,采用接口方式,将需要调用的其他服务的方法抽象,cloud会动态代理成类,进行调用.使用时直接加上@FeignClient注解,类似于@Mapper