springcloud2.1.10版本---- 四 Feign(声明式服务调用)

Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。

简而言之:

  • Feign 采用的是基于接口的注解
  • Feign 整合了ribbon,具有负载均衡的能力
  • 整合了Hystrix,具有熔断的能力

首先创建一个工程:



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.10.RELEASE
		 
	
	com.example
	spring-cloud-feign
	0.0.1-SNAPSHOT
	spring-cloud-feign
	Demo project for Spring Boot

	
		1.8
		Greenwich.SR3
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.springframework.cloud
			spring-cloud-starter-netflix-eureka-client
		
		
			org.springframework.cloud
			spring-cloud-starter-openfeign
		
		
		
			org.springframework.boot
			spring-boot-devtools
			runtime
			true
		
		
		
		    org.springframework
		    springloaded
		    1.2.6.RELEASE
		
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				${spring-cloud.version}
				pom
				import
			
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


2 启动类配置

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.ConfigurableEnvironment;

import feign.Request;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class SpringCloudFeignApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringCloudFeignApplication.class, args);
	}
//	@Bean
//	public static Request.Options requestOptions(ConfigurableEnvironment env){
//		return new Request.Options(1,1);
//	}

}

3 服务接口

import java.util.List;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.hdu.server.bean.User;

@FeignClient(name="eureka-provider")
public interface UserService {
	@RequestMapping("/getUser")
	List getUserList();
	
	@RequestMapping("/getUserByName")
	User getUserByName(@RequestParam("name") String name);
	
	@RequestMapping("/getResult")
	String getResult(@RequestBody User user);
}

4 Controller层调用

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hdu.server.bean.User;
import com.hdu.server.service.UserService;

@RestController
public class UserController {
	@Autowired
	private UserService userService;
	
	@GetMapping("/getUserList")
	public List getUserList(){
		return userService.getUserList();
	}
	
	@RequestMapping("/getUserByName")
	public User getUserByName(String name){
		return userService.getUserByName(name);
	}
	
	@RequestMapping("/getResult")
	public String getResult(User user){
		return userService.getResult(user);
	}
}

5 application.yml文件配置

spring:
  application:
    name: feign-consumer
server:
  port: 8500
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/

6 启动feign-consumer并访问http://localhost:8500/getUserList

springcloud2.1.10版本---- 四 Feign(声明式服务调用)_第1张图片

7 feign的服务降级处理

可以通过实现UserService并配置@FeignClient,其实现如下:

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import com.hdu.server.bean.User;
@Component
public class UserServiceFallback implements UserService{

	@Override
	public List getUserList() {
		// TODO Auto-generated method stub
		List list = new ArrayList<>();
		list.add(new User("未知","-1"));
		return list;
	}

	@Override
	public User getUserByName(@RequestParam("name") String name) {
		// TODO Auto-generated method stub
		return new User("未知","-1");
	}

	@Override
	public String getResult(@RequestBody User user) {
		// TODO Auto-generated method stub
		return "error";
	}

}
@FeignClient(name="eureka-provider",fallback=UserServiceFallback.class)
public interface UserService {
	@RequestMapping("/getUser")
	List getUserList();
	
	@RequestMapping("/getUserByName")
	User getUserByName(@RequestParam("name") String name);
	
	@RequestMapping("/getResult")
	String getResult(@RequestBody User user);
}

同时还需要配置feign的请求超时时间等配置。

你可能感兴趣的:(SpringCloud)