1.feign是spring cloud服务间相互调用的组件,声明式、模板化的HTTP客户端。类似的HttpURLConnection、Apache HttpComponnets、OkHttp3 、Netty都实现相同功能。
目录结构
pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.test</groupId> <artifactId>feigtest</artifactId> <version>0.0.1-SNAPSHOT</version> <name>feigtest</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR1</spring-cloud.version> <fastjson.version>1.2.32</fastjson.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <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> <!--引入这个包才能使用fegin--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> </dependency> <!--<dependency>--> <!--<groupId>com.iot.crm.common</groupId>--> <!--<artifactId>crm-common</artifactId>--> <!--<version>1.0-SNAPSHOT</version>--> <!--</dependency>--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson.version}</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project>
application.yml
spring:
application:
name: service-feigntest
server:
port: 8702
eureka:
client:
serviceUrl:
#指向注册中心
defaultZone: http://192.168.111.133:8888/eureka/
instance:
# 每间隔1s,向服务端发送一次心跳,证明自己依然”存活“
lease-renewal-interval-in-seconds: 1
# 告诉服务端,如果我2s之内没有给你发心跳,就代表我“死”了,将我踢出掉。
lease-expiration-duration-in-seconds: 2
IHiService.java
package com.feigtest; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.PostMapping; @FeignClient("service-hi") public interface IHiService { @PostMapping("/hi") public String say(); }
FeigTestController.java
package com.feigtest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @Title: * @Auther: pujiahong * @Date: 2019/6/11 20:48 * @Version: 1.0 * @Description: */ @RestController public class FeigTestController { @Autowired IHiService hiService; @RequestMapping("/hifeign") public String getFeignService(){ return hiService.say(); } }
FeigtestApplication
package com.feigtest; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients; @EnableEurekaClient @SpringBootApplication @EnableFeignClients public class FeigtestApplication { public static void main(String[] args) { SpringApplication.run(FeigtestApplication.class, args); } }