Quietly tell you

没有什么技术是一篇文档解决不了的,如果有,那就两篇。

Feign!!一个绝妙的远程服务调用框架

Feign!,在spring cloud有集成,他是一个快速构建远程服务访问的框架。用它可以轻松地快速访问远程service。就像cxf一样,但比cxf更小巧精悍!

下面记录一下在一个分布式场景中的feign的应用。

条件:

有serviceId为“TestService”的一个微服务,并且它的请求uri是“test”(http://TestService/test)

步骤:

1、创建spring boot 项目,集成eureka client并链接到eureka注册中心。

2、编辑pom文件,引入feign依赖

		<!-- 集成Feign Result Full 接口快速开发框架 -->
		<dependency>
		    <groupId>org.springframework.cloud</groupId>
		    <artifactId>spring-cloud-starter-feign</artifactId>
		    <version>1.3.0.RELEASE</version>
		</dependency>

3、在启动类中添加@EnableFeignClients注解

package com.aiyi.springclouda.server;


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;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ClientApplication {
	
	public static void main(String[] args) {
		SpringApplication.run(ClientApplication.class, args);
	}
}

4、建立UserDao接口类,使该类直接调用远程http://TestService/test 的RestFul服务实现功能。

package com.aiyi.springclouda.server.dao;

import java.util.List;

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

import com.aiyi.springclouda.server.po.User;

@FeignClient("TestService")
public interface UserDao {

	@RequestMapping(value = "test", method = RequestMethod.GET)
	public List<User> listAll();
}

5、最后一步!写个Controller测试一下把!

package com.aiyi.springclouda.server.controller;


import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aiyi.springclouda.server.dao.UserDao;

@Controller
public class TestController {

	@Resource
	private UserDao userDao;
	
	
	@GetMapping("test")
	@ResponseBody
	public Object test(){
		return userDao.listAll();
	}
}

非常轻便!就像直接cxf一样直接调用接口类即可!spring 会自动将带有@EnableFeignClients注解的接口类进行实例化!

完整的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.aiyi.springclouda</groupId>
	<artifactId>client</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>server</name>
	<description>Demo project for Spring Boot</description>
	
	<!-- 声明为 Spring Boot 项目 -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.4.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>
	</properties>

	<dependencies>
	
		
		<!-- WEB 支持模块 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		
		<!-- HTML 页面模板模块 -->
		<dependency>  
		    <groupId>org.springframework.boot</groupId>  
		    <artifactId>spring-boot-starter-thymeleaf</artifactId>  
		</dependency>
		
		<!-- 工程健康监控模块 -->
		<dependency>  
		    <groupId>org.springframework.boot</groupId>  
		    <artifactId>spring-boot-starter-actuator</artifactId>  
		</dependency>
		
		<!-- Eureka 分布式集成模块 -->
		<dependency>
		    <groupId>org.springframework.cloud</groupId>
		    <artifactId>spring-cloud-starter-eureka</artifactId>
		    <version>1.3.0.RELEASE</version>
		</dependency>
		
		<!-- 集成Feign Result Full 接口快速开发框架 -->
		<dependency>
		    <groupId>org.springframework.cloud</groupId>
		    <artifactId>spring-cloud-starter-feign</artifactId>
		    <version>1.3.0.RELEASE</version>
		</dependency>
		
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>

 

点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注