Reference article: the Spring Cloud Feign design principles
1.feign component is between spring cloud service call each other, declarative, template-based HTTP client. Similar HttpURLConnection, Apache HttpComponnets, OkHttp3, Netty will achieve the same functionality.
Directory Structure
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
the Spring:
the Application:
name: Service-feigntest
Server:
Port: 8702
Eureka:
Client:
serviceUrl:
# to registration centers
defaultzone: http://192.168.111.133:8888/eureka/
instance:
# intervals of 1s, sent once to the server heartbeat, prove that they are still "alive"
Lease-Renewal-in-seconds the interval the-: 1
# tell the server if I did not give you the heartbeat within 2s, on behalf of my "dead", I will kick off.
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); } }