博客详情

springcloud(五)--服务消费者Fegin (原创)

作者: 朝如青丝暮成雪
发布时间:2018-08-18 11:07:28  文章分类:springcloud   阅读(1104)  评论(0)

上一篇我们介绍了服务消费者restTemplate+ribbon ,本篇我们介绍下服务消费者Fegin。


Fegin是什么? 


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



sim-consumer 工程 

pom.xml  新加入 spring-cloud-starter-feign 起步依赖


<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>




springboot启动类上,新添加 @EnableFeignClients注解开启Fegin客户端
package com.tingcream.simConsumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
//@EnableEurekaClient //已过时不用了
@EnableDiscoveryClient
@EnableFeignClients //启用Feign客户端
public class SimConsumerApp extends SpringBootServletInitializer{
  
    @Override
	protected SpringApplicationBuilder configure(
			SpringApplicationBuilder builder) {
    	 return builder.sources(SimConsumerApp.class);
	}

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


编写Feign的java Api接口 :

HelloFeignApi.java


package com.tingcream.simConsumer.clientApi;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;


@FeignClient("sim-provider")
public interface HelloFeignApi {
	
	@GetMapping( "/hello")
	public String hello();
	
	@GetMapping( "/hello2")
	public String hello2(@RequestParam("name") String name);

}



sim-provider 服务提供者对应的http REST 接口 

HelloProviderController.java 


package com.tingcream.simProvider.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloProviderController {
	
	
	@Value("${spring.application.name}")
	private String applicationName;
	
	@Value("${server.port}")
	private String port ;
	
	@GetMapping("/hello")
	public String  hello(){
		return "你好,我是:"+applicationName+",服务端口:"+port;
	}
	@GetMapping("/hello2")
	public String  hello2( String name){
		return "你好,"+name+",我是:"+applicationName+",服务端口:"+port;
	}
 
}



sim-consumer 服务消费者 ,使用@AutoWired注入 HelloFeignApi bean后,直接调用其中定义的方法即可。



package com.tingcream.simConsumer.controller;

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

import com.tingcream.simConsumer.clientApi.HelloFeignApi;


@RestController
public class HelloConsumerController {
	
	private  String  PRE_URL="http://SIM-PROVIDER/";
	
	@Autowired
	private RestTemplate restTemplate;
	
	@Autowired
	private HelloFeignApi helloFeignApi;

	@GetMapping("/hello")
	public String  hello(){
	  
	   return 	helloFeignApi.hello();
		
	}
	@GetMapping("/hello2")
	public String  hello2(String name){
		
		return 	helloFeignApi.hello2(name);
		
	}
	 

}


分别启动Eureka Server 、sim-provider 7001 、sim-provider 7002 、 sim-consumer应用。 

浏览器访问http://localhost/hello  , http://localhost/hello2?name=xiaoming  ,刷新页面时,发现Fegin默认自动帮我们配上了负载均衡 。


注意:Feign会基于用户使用的注解声明来进行解析,生成最终http的调用模板,所以 HelloFeignApi 接口中声明hello2方法时,方法形参name前面的@RequestParam("name") 不能省略,否则Feign不能正常工作。 




关键字:  springcloud  Feign
评论信息
暂无评论
发表评论

亲,您还没有登陆,暂不能评论哦! 去 登陆 | 注册

博主信息
   
数据加载中,请稍候...
文章分类
   
数据加载中,请稍候...
阅读排行
 
数据加载中,请稍候...
评论排行
 
数据加载中,请稍候...

Copyright © 叮叮声的奶酪 版权所有
备案号:鄂ICP备17018671号-1

鄂公网安备 42011102000739号