springcloud學習2:使用feign進行微服務之間的調用
一、簡單說明
在spring cloud 中有兩種服務調用方式,一種是ribbon+restTemplate ,另一種是feign。相對來說,feign因為注解使用起來更簡便。而restTemplate需要我們自定義一個RestTemplate,手動注入,並設置成LoadBalance。
eign是聲明式的web service客戶端 ,使用feign簡化了微服務之間的調用,可以像調用接口一樣調用別的服務。
使用feign的前提是服務提供者和消費者都注冊到了eureka中,在服務消費者中集成feign,調用注冊到eureka中的服務。
二、在服務消費者工程中集成feign
集成feign到工程中,pom文件如下
<?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>
<groupId>com.lyy</groupId>
<artifactId>demo_consumer_user</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.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>
<spring-cloud.version>Finchley.M8</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--feign的配置-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--熱部署配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.16</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>
在工程的啟動類上加@EnableFeignClients
注解,說明這是一個feign客戶端
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class ConsumerUserApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerUserApplication.class,args);
}
}
創建一個接口,在接口上使用@FeignClient
注解,如下
@FeignClient("demo-provider-user")
public interface IUserInfoClient {
@GetMapping("/provider/user/findAll")
List<User> findAll();
}
其中FeignClient注解的默認參數是要調用的服務在eureka server中的服務id。在接口中定義抽象方法,打上對應的springmvc注解,就可以連接到目標服務中的指定接口上,這里的@GetMapping("/provider/user/findAll")中的參數就是這個接口在目標服務中的請求url
這樣就集成完成了,在服務消費者方只要調用這個接口中的方法就完成了對目標微服務的調用。
public class UserController {
@Autowired
private IUserInfoClient userInfoClient;
@GetMapping("/findAll")
public List<User> findAll(){
List<User> all = userInfoClient.findAll();
for (User user : all) {
System.out.println(user);
}
return all;
}
}