Spring Cloud学习系列第二章:使用Feign调用服务

一、Feign简介

  Feign是一个声明式的Web服务客户端。这使得Web服务客户端的写入更加方便 要使用Feign创建一个界面并对其进行注释。它具有可插入注释支持,包括Feign注释和JAX-RS注释。Feign还支持可插拔编码器和解码器。Spring Cloud增加了对Spring MVC注释的支持,并使用Spring Web中默认使用的HttpMessageConvertersSpring Cloud集成Ribbon和Eureka以在使用Feign时提供负载均衡的http客户端。

  上面这段话是官方文档,说的有些啰嗦,简单来说,就是Feign帮我们准备了一套调用Spring Cloud注册服务中心里面的服务,不用再去使用Apache httpclient之类的http客户端工具了,还提供了很多功能,编解码、负载均衡等。

二、创建Feign项目

我们的目的是使用上一章发布的服务,而且是到Eureka Server中去访问服务,所以我们的Feign项目就是比Eureka Server项目多一个Feign的依赖,内容如下。

<?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.nan</groupId>
	<artifactId>feigndemo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>feigndemo</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.6.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>
		<spring-cloud.version>Dalston.SR2</spring-cloud.version>
	</properties>

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

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

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

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


</project>

  项目启动入口要激活Feign client,内容如下。

package com.nan.feigndemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class FeigndemoApplication {

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

}

  接下来就要声明Feign客户端了,目的就是指向我们发布的服务,首先看内容。

package com.nan.feigndemo.client;

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

@FeignClient("HelloService")
public interface HelloServiceClient {

    @RequestMapping("/hello")
    String hello(@RequestParam("name") String name);

}

  很明显,这是一个interface,是一个声明式使用方式。重要的注解,FeignClient,"HelloService"属性就是我们发布的服务的spring.application.name。下面的接口方法就和我们发布的Rest接口一致了,另外RequestMapping不要用在interface上,不然会被spring mvc容器扫描到。

  然后编写我们自己的项目业务,调用这个客户端,内容如下。

package com.nan.feigndemo.controller;

import com.nan.feigndemo.client.HelloServiceClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FeignDemoController {

    @Autowired
    private HelloServiceClient helloServiceClient;

    @RequestMapping("/feigndemo")
    public String feignDemo(@RequestParam("name") String name) {
        return helloServiceClient.hello(name);
    }

}

  简单的controller,就不多废话了。重要的是下面的项目配置。

server:
  port: 8080
spring:
  application:
    name: FeignDemo
eureka:
  client:
    serviceUrl:
      defaultZone: http://node01:8081/eureka/,http://node02:8082/eureka/

 server.port和application.name是我们项目自己的,eureka.client.serviceUrl就是我们的Eureka Server集群模式的地址,然后启动项目,记得Eureka server和HelloService也要启动。然后浏览器访问我们的feigndemo项目,浏览器输入http://localhost:8080/feigndemo?name=秦始皇,浏览器会出现 Hello 秦始皇, this client port is 9091,再刷新页面,会出现 Hello 秦始皇, this client port is 9092。OK,服务调用,负载均衡都已实现,真是太佩服他们了,一步一步简化我们的开发。


  以上就是笔者目前所学,还是很初级的内容,如果有什么错误和遗漏,还请见谅并指正,不胜感激。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值