欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringCloud框架服务消费者-Feign

程序员文章站 2022-03-30 10:20:07
1.Feign介绍Feign是一个声明式的web service客户端,它使得编写web service客户端更为容易。创建接口,为接口添加注解,即可使用Feign。Feign可以使用Feign注解或者JAX-RS注解,还支持热插拔的编码器和解码器。Spring Cloud为Feign添加了Spring MVC的注解支持,并整合了Ribbon和Eureka来为使用Feign时提供负载均衡。......

1.Feign介绍

Feign是一个声明式的web service客户端,它使得编写web service客户端更为容
易。创建接口,为接口添加注解,即可使用Feign。Feign可以使用Feign注解或者
JAX-RS注解,还支持热插拔的编码器和解码器。Spring Cloud为Feign添加了
Spring MVC的注解支持,并整合了Ribbon和Eureka来为使用Feign时提供负载均衡。

译自:http://projects.spring.io/spring-cloud/docs/1.0.3/springcloud.
html#spring-cloud-feign

2.示例

创建一个Maven项目,并在pom.xml添加如下内容:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="ht
tp://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://m
aven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
	<artifactId>microservice-consumer-movie-feign</artifactId>
	<packaging>jar</packaging>
<parent>
<groupId>com.itmuch.cloud</groupId>
	<artifactId>spring-cloud-microservice-study</artifactId>
	<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-eureka</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-actuator</artifactId>
	</dependency>
</dependencies>
</project>
  • 启动类:MovieFeignApplication.java
/**
* 使用@EnableFeignClients开启Feign
* @author eacdy
*/
@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class MovieFeignApplication {
	public static void main(String[] args) {
		SpringApplication.run(MovieFeignApplication.class, args);
	}
}
  • 实体类:User.java
public class User {
	private Long id;
	private String username;
	private Integer age;
	...
	// getters and setters
}
  • Feign测试类:UserFeignClient.java
/**
* 使用@FeignClient("microservice-provider-user")注解绑定microserv
ice-provider-user服务,还可以使用url参数指定一个URL。
* @author eacdy
*/
@FeignClient(name = "microservice-provider-user")
public interface UserFeignClient {
	@RequestMapping("/{id}")
	public User findByIdFeign(@RequestParam("id") Long id);
	}
}
  • Feign的测试类:FeignController.java
@RestController
public class FeignController {
	@Autowired
	private UserFeignClient userFeignClient;
	@GetMapping("feign/{id}")
		public User findByIdFeign(@PathVariable Long id) {
		User user = this.userFeignClient.findByIdFeign(id);
		return user;
	}
}
  • application.yml
server:
	port: 8020
spring:
	application:
		name: microservice-consumer-movie-feign
eureka:
	client:
		serviceUrl:
			defaultZone: http://discovery:8761/eureka/
instance:
	preferIpAddress: true
ribbon:
	eureka:
		enabled: true 
# 默认为true。如果设置为false,Ribbon将不会从Eureka中获得服务列表,
#而是使用静态配置的服务列表。
#静态服务列表可使用:<client>.ribbon.listOfServers来指定。
#参考:http://projects.spring.io/spring-cloud/docs/1.0.3/spring-cloud.html#spring-cloud-ribbonwithout-eureka
### 参考:https://spring.io/guides/gs/client-side-load-balancing/

Feign代码地址:
http://git.oschina.net/itmuch/spring-cloud-study/tree/master/microserviceconsumer-
movie-feign https://github.com/eacdy/spring-cloudstudy/
tree/master/microservice-consumer-movie-feign

本文地址:https://blog.csdn.net/u014131617/article/details/85991058

相关标签: springcloud