SpringCloud Feign声明式服务调用模块

说明

Feign是一个声明式REST的Web服务客户端。它使得Web服务客户端的写入更加方便。使用Feign创建一个界面并对其进行注释;具有可插拔注释支持,包括Feign注释和JAX-RS注释;支持可插拔编码器和解码器;它基于NetFlix Feign实现,整合了Ribbon和Hystrix两者在SpringCloud Feign的实现下,我们只需创建一个接口并以注解的方式配置它,即可完成对服务提供方的接口绑定,简化了在使用SpringCloud Ribbon时自行封装服务调用客户端的开发量。

目标

通过SpringCloud Feign提供的声明式服务绑定功能来实现对该服务接口的调用

快速使用

首先给出项目结构

SpringCloud Feign声明式服务调用模块_第1张图片

1 更新pom.xml



	4.0.0

	yunlingfly
	mavenspringcloudfeigh
	0.0.1-SNAPSHOT
	jar

	mavenspringcloudfeigh
	Demo project for Spring Boot

	
		org.springframework.boot
		spring-boot-starter-parent
		1.5.10.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
		Dalston.SR2
	

	
		
			org.springframework.cloud
			spring-cloud-starter-feign
		

		
			org.springframework.cloud
			spring-cloud-starter-web
		

		
			org.springframework.cloud
			spring-cloud-starter-eureka
		

		
			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 更新application.yml

eureka:
  client:
    serviceUrl:
      defaultZone: http://xxx.xxx.xxx.xxx:8761/eureka/,http://xxx.xxx.xxx.xxx:8762/eureka/
server:
  port: 8766
spring:
  application:
    name: service-feigh
info:
  app:
    name: 一个feigh消费者
    version: 0.0.1

3 编写启动类

package yunlingfly.mavenspringcloudfeigh;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

// 开启Feigh负载均衡
@EnableFeignClients
// 向服务中心注册
@EnableDiscoveryClient
@SpringBootApplication
public class MavenspringcloudfeighApplication {

	public static void main(String[] args) {
		SpringApplication.run(MavenspringcloudfeighApplication.class, args);
	}
}

4 编写调用服务接口

package yunlingfly.mavenspringcloudfeigh.service;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

// 设置请求的应用名
@FeignClient("eureka-client")
@Component
public interface HelloService {
    // 设置请求的方法|接口
    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    String hello(@RequestParam(value = "name") String name);
}

5 编写服务消费者的Controller层

package yunlingfly.mavenspringcloudfeigh.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import yunlingfly.mavenspringcloudfeigh.service.HelloService;

@RestController
public class HelloController {
    @Autowired
    private HelloService helloService;
    @RequestMapping(value = "/feigh",method = RequestMethod.GET)
    public String hello(@RequestParam String name){
        return helloService.hello(name);
    }
}
运行

首先启动Eureka注册中心,然后启动服务提供者(都沿用之前的项目),再启动Feign服务消费者,打开浏览器输入http://localhost:8766/feigh?name=芸灵fly即可看到效果

SpringCloud Feign声明式服务调用模块_第2张图片

注意事项:由于使用接口调用服务,所以Controller层的RequestMapping可以和提供服务的不一样,而Ribbon需要一致,这个特性可以控制后端接口暴露的风险;@FeignClient里的value值不区分大小写


你可能感兴趣的:(Spring,Cloud)