SpringCloud-----fegin服務消費者及負載均衡
阿新 • • 發佈:2018-12-04
1、什麼是Feign
Feign是一個宣告式的偽Http客戶端,它使得寫Http客戶端變得更簡單。
使用Feign,只需要建立一個介面並註解。
它具有可插拔的註解特性,可使用Feign 註解和JAX-RS註解。
Feign支援可插拔的編碼器和解碼器。
Feign預設集成了Ribbon,並和Eureka結合,
預設實現了負載均衡的效果。
簡而言之:
- Feign 採用的是基於介面的註解
- Feign 整合了ribbon
2、pom.xml
<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> <groupId>com.cloud</groupId> <artifactId>consumer-fegin</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>consumer-fegin</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 消費端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.RC1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
application.yml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8888/eureka/
server:
port: 8765
spring:
application:
name: consumer-feign
MemberFegin.java
package com.cloud.fegin; import java.util.List; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; @FeignClient(value = "service-member") public interface MemberFegin { @GetMapping("/getMemberAll") public List<String> getList(); }
OrderFeginController.java
package com.cloud.fegin.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import com.cloud.fegin.MemberFegin; @RestController public class OrderFeginController { @Autowired private MemberFegin memberFegin; @GetMapping("/list") public List<String> list(){ System.out.println("order fgin 工程呼叫member工程"); List<String> list = memberFegin.getList(); return list; } }
FeginApp.java
package com.cloud.fegin;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class FeginApp {
public static void main(String[] args) {
SpringApplication.run(FeginApp.class, args);
}
}
啟動兩個服務提供者,啟動feig消費端,訪問介面進行測試