SpringCloud之Feign

附:SpringCloud之系列教程汇总跳转地址

Feign简介

Feign 是一个声明式的 Web Service 客户端。使用 Feign 能让编写 Web Service 客户端更加简单,SpringCloud 对 Feign 进行了封装,使其支持了 Spring MVC 标准注解和 HttpMessageConverters。

Feign 的使用方法是定义一个接口,然后在其上边添加 @FeignClient 注解。

简而言之:

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

Feign实践

在上一篇的基础上,新建一个Feign的maven子项目,项目结构

FeignController

package com.yj.feign.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.yj.feign.service.FeignService;

@RestController
public class FeignController {

	@Autowired
	private FeignService feignService;

	@GetMapping(value = "/hiFeign")
	public String hiFeign(@RequestParam String name) {
		return feignService.hiFeign(name);
	}
}

FeignService

package com.yj.feign.service;

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

@FeignClient(value = "eureka-client")
public interface FeignService {
	@RequestMapping(value = "/hiEureka", method = RequestMethod.GET)
	String hiFeign(@RequestParam(value = "name") String name);
}

FeignApplication

package com.yj.feign;

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 FeignApplication 
{
	public static void main(String[] args) {
		SpringApplication.run(FeignApplication.class, args);
	}
}

application.properties

server.port=8006

spring.application.name=feign
eureka.client.serviceUrl.defaultZone=http://admin:123456@192.168.37.139:8001/eureka,http://admin:123456@192.168.37.139:8002/eureka
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.cloud.client.ipAddress}:${server.port}
eureka.instance.hostname=${spring.cloud.client.ipAddress}

pom.xml

<?xml version="1.0"?>
<project
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
	xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>

	<parent>
		<groupId>com.yj</groupId>
		<artifactId>SpringCloud</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>

	<artifactId>Feign</artifactId>
	<name>Feign</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

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

	<build>
		<finalName>${artifactId}</finalName>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<skip>true</skip>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<configuration>

					<archive>
						<manifest>
							<mainClass>com.yj.feign.FeignApplication</mainClass>
							<addClasspath>true</addClasspath>
							<classpathPrefix>lib</classpathPrefix>
						</manifest>
						<manifestEntries>
							<Class-Path>./</Class-Path>
						</manifestEntries>
					</archive>
					<excludes>
						<exclude>config/**</exclude>
						<exclude>**.xml</exclude>
						<exclude>**.properties</exclude>
					</excludes>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-assembly-plugin</artifactId>
				<configuration>
					<appendAssemblyId>false</appendAssemblyId>
					<descriptors>
						<descriptor>src/main/build/package.xml</descriptor>
					</descriptors>
				</configuration>
				<executions>
					<execution>
						<id>make-assembly</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
		<testResources>
			<testResource>
				<directory>src/main/resources</directory>
			</testResource>
		</testResources>
	</build>
</project>

部署启动,Feign项目也被注册到Eureka上面了

访问http://192.168.37.139:8006/hiFeign?name=yj,交替显示hi yj ,i am from port:8003,hi yj ,i am from port:8004 

附:SpringCloud之系列教程汇总跳转地址 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猎户星座。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值