> 文章列表 > Spring Cloud第二季--OpenFeign和Feign

Spring Cloud第二季--OpenFeign和Feign

Spring Cloud第二季--OpenFeign和Feign

文章目录

  • 一、Feign
  • 二、Feign和OpenFeign的区别
  • 三、案例测试
    • 1、eureka注册中心集群7001/7002
    • 2、两个微服务
    • 3、OpenFeign

一、Feign

Spring Cloud Feign的介绍在Spring Cloud学习–声明式调用(Feign)中详细介绍。

简单回顾下,Feign是一个声明式的WebService客户端。它的使用方法是定义一个服务接口然后在上面添加注解。大白话就是不再使用Ribbon+RestTemplete的方式远程调用微服务接口,而是遵循程序员常用的方式,从Controller层调用Service层的接口,Service的接口替你完成调用的工作(2019年Feign停更)。

两个字,优雅。

二、Feign和OpenFeign的区别

Feign OpenFeign
Feign是Spring Cloud组件中的一个轻量级RESTful的HTTP服务客户端。Feign内置了Ribbon,用来做客户端负载均衡,去调用服务注册中心的服务。Feign的使用方式是:使用Feign的注解定义接口,调用这个接口,就可以调用服务注册中心的服务 OpenFeign是Spring Cloud 在Feign的基础上支持了SpringMVC的注解,如@RequesMapping等等。OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口,并通过动态代理的方式产生实现类,实现类中做负载均衡并调用其他服务。

三、案例测试

1、eureka注册中心集群7001/7002

7001 yml配置

server:port: 7001eureka:instance:hostname: eureka7001.com #eureka服务端的实例名称client:#false表示不向注册中心注册自己。register-with-eureka: false#false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务fetch-registry: falseservice-url:#设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。defaultZone: http://eureka7002.com:7002/eureka/server:enable-self-preservation: false    # 关闭自我保护

7001 启动类

@SpringBootApplication
@EnableEurekaServer
public class EurekaMain{public static void main(String[] args) {SpringApplication.run(EurekaMain.class,args);}
}

7002 配置

server:port: 7002eureka:instance:hostname: eureka7002.com #eureka服务端的实例名称client:#false表示不向注册中心注册自己。register-with-eureka: false#false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务fetch-registry: falseservice-url:#设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。defaultZone: http://eureka7001.com:7001/eureka/

注意: eureka注册中心要互相交互,要注意defaultZone参数的配置,写对!写对!写对!
7002 启动类


@SpringBootApplication
@EnableEurekaServer
public class EurekaMain2 {public static void main(String[] args) {SpringApplication.run(EurekaMain2.class,args);}
}

2、两个微服务

端口8001 配置

server:port: 8001spring:application:name: cloud-payment-servicedatasource:type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型url: jdbc:mysql://localhost:3306/test?autoReconnect=true&useUnicode=trueusername: your-namepassword: your-passwordriver-class-name: com.mysql.jdbc.Drivereureka:client:#表示是否将自己注册进EurekaServer默认为true。register-with-eureka: true#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡fetchRegistry: trueservice-url:defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eurekainstance:instance-id: paymentprefer-ip-address: true    -mybatis:mapperLocations: classpath:mapper/*.xmltype-aliases-package: com.swc.springcloud.entities   -

Controller类

@RestController
@Slf4j
public class PaymentController {@Resourceprivate PaymentService paymentService;@GetMapping(value = "/payment/get/{id}")public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){Payment payment = paymentService.getPaymentById(id);log.info("打印测试查询结果:{}",payment);if (payment != null) {System.out.println("1111");return new CommonResult(200,"8001查询成功",payment);}else{return new CommonResult(444,"8001没有对应记录,查询ID: "+id,null);}}}

Service以及Dao层,mapper.xml暂且不表,端口8002大同小异。

3、OpenFeign

增加依赖:

 <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency>

yml 配置:

server:port: 80eureka:client:register-with-eureka: falseservice-url:defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/

Controller 层

@RestController
public class OrderFeignController {@Resourceprivate PaymentFeignService paymentFeignService;@GetMapping(value = "/consumer/payment/get/{id}")public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){return paymentFeignService.getPaymentById(id);}
}

Service层,添加注解@FeignClient,value的值表示调用微服务的名称

@Component
@FeignClient(value = "cloud-payment-service")
public interface PaymentFeignService
{@GetMapping(value = "/payment/get/{id}")CommonResult<Payment> getPaymentById(@PathVariable("id") Long id);
}

启动类,添加注解@EnableFeignClients,开启注解。

@SpringBootApplication
@EnableFeignClients
public class OrderFeignMain
{public static void main(String[] args){SpringApplication.run(OrderFeignMain.class,args);}
}

启动测试,多次访问,实现了负载均衡。
在这里插入图片描述
在这里插入图片描述