Feign概述
feign简述
Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单;
Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。
Feign使用
创建工程
1.IDEA下通过Spring Initializr>>Cloud Discovery>>Eureka Discovery创建一个工程
2.创建好的工程pom.xml中默认是没有web依赖和feign依赖的,添加依赖即可:<!--web--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--feign--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>3.后续可能用到SLF4J以及lombok,所以顺便添加lombok依赖:
<!--Slf4j/不写getter/setter的插件--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>4.配置文件application.yml(向http://172.96.251.101:8761/eureka注册服务)
spring: application: name: service-feign eureka: client: service-url: defaultZone: http://172.96.251.101:8761/eureka server: port: 8083 management: server: servlet: context-path: /5.SpringBoot启动类上添加Feign注解,因为是一个Eureka客户端,所以也会添加EurekaClient注解:
@SpringBootApplication @EnableEurekaClient @EnableFeignClients public class FeignApplication { public static void main(String[] args) { SpringApplication.run(FeignApplication.class, args); }6.创建包erviceClient,包中创建接口ProductServiceClient.java,作为调用远程服务的service接口:
package com.ux.feign.serviceClient; import com.ux.feign.pojo.Product; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; @FeignClient(name = "WXPROJECT") @Service public interface ProductServiceClient { @GetMapping("/product/productInfo") Product plist(@RequestParam("productId") String productId); } **注意:** ProductServiceClient接口中是访问另一个服务名为WXPROJECT的应用获取productInfo的信息的方法(plist),传递参数为productId,数据返回结果类型为Product,属性如下: @Data//lombok public class Product { private String productId; private String productName; private BigDecimal productPrice; private Integer productStock; private String productDescription; private String productIcon; private String productStatus; private String categoryType; private Date createTime; private Date updateTime;}
7.创建实体类Product,属性同上。
8.创建Service类:ProduceService.java,起作用是调用ProductServiceClient接口中的方法。
package com.ux.feign.service; import com.ux.feign.pojo.Product; import com.ux.feign.serviceClient.ProductServiceClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; @Service public class ProduceService { @Autowired ProductServiceClient productServiceClient; @GetMapping("/plist") public Product plist(@RequestParam("productId") String productId) { return productServiceClient.plist(productId); } }9.创建Controller:
package com.ux.feign.controller; import com.ux.feign.pojo.Product; import com.ux.feign.service.ProduceService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController @Slf4j public class ProductController { @Autowired ProduceService produceService; //http://localhost:8083/product/list?productId=11 @RequestMapping("/product/list") public Product list(@RequestParam("productId") String productId) { Product result = produceService.plist(productId); log.info("~~~feign实现服务间的调用result={}", result); return result; } }
小结
小结
上面的工程总结下来就是:
通过使用Feign,来调用远程服务中的product/productInfo方法(这一步通过ProductServiceClient接口来做),而后在工程中想要获取远程productInfo,只需要调用ProductServiceClient中对应的方法即可。
1.ProductServiceClient接口>>>调用远程服务 2.ProduceService调用ProductServiceClient接口 3.ProductController调用ProduceService说明
1.工程使用Intellij IDEA 2017.02创建
2.SpringBoot版本为2.0