Spring Cloud(Finchley.RELEASE版本)微服务学习实践:3.2服务消费者-Feign

环境:

jdk1.8;spring boot2.0.3;spring cloud(Finchley.RELEASE版本);Maven3.3

摘要说明:

Spring Cloud FeignSpring Cloud Feign是一套基于Netflix Feign实现的声明式服务调用客户端。它使得编写Web服务客户端变得更加简单。通过创建接口并用注解来配置它既可完成对Web服务接口的绑定。它具备可插拔的注解支持,包括Feign注解、JAX-RS注解。它也支持可插拔的编码器和解码器。Spring Cloud Feign还扩展了对Spring MVC注解的支持,同时还整合了Ribbon和Eureka来提供均衡负载的HTTP客户端实现。

总之,Feign是基于Ribbon实现的Feign使得接口更加规范化,更有阅读性并且更加简单化;

步骤:

1.创建FeignFeign的服务消费者)子项目

通过SPRING INITIALIZR工具选择Cloud Routing:Feign模块构建feign子项目引入依赖(pom.xnl):

<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>
	<parent>
		<groupId>pers.cc</groupId>
		<artifactId>springCloud</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>feign</artifactId>
	<name>feign</name>
	<description>Feign消费者</description>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-openfeign</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-netflix-eureka-client</artifactId>
		</dependency>
	</dependencies>
</project>

2.配置FeignFeign的服务消费者)子项目

使用@EnableDiscoveryClient和@EnableFeignClients注解开启feign的服务消费者配置;

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

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

配置application.properties

#配置服务及端口
spring.application.name=feign-consumer
server.port=4001
#注册到注册中心
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/

3.开发Feign接口

使用Feign与Ribbon不同的是需要调用前实现规范化接口,使用@FeignClient和Spring Mvc注解来绑定服务提供方的REST接口

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

//根据服务名称来指定服务提供方
@FeignClient("eureka-client")
public interface TestService {
	// 通过注解指定访问方式,访问路径,访问参数
	@RequestMapping(method = RequestMethod.GET, value = "/add")
	Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b);

}

然后就是实现controller来调用接口:

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

import pers.cc.springCloud.feign.test.service.TestService;

@RestController
public class TestController {
	@Autowired
	TestService testService;

	@GetMapping("/test")
	public Integer test() {
		return testService.add(12, 6);
	}
}

4.调用Feign

先后启动:

eurekaServer

服务提供者(eurekaDiscovery):eureka-client(2001)

服务提供者(eurekaDiscovery):eureka-client(2002)

服务消费者(feign):feign-consumer(4001)

多次输入http://localhost:4001/test;查看eurekaDiscovery两个服务提供者都会出现调用达到负载均衡的作用;

5.源码地址

github地址:https://github.com/cc6688211/springCloud.git

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值