SpringCloud服务注册与发现之服务调用-Feign

5 篇文章 0 订阅
2 篇文章 0 订阅

一.Feign

Feign使得 Java HTTP 客户端编写更方便。Feign 灵感来源于RetrofitJAXRS-2.0WebSocketFeign 最初是为了降低统一绑定Denominator 到 HTTP API 的复杂度,不区分是否支持 Restful。

SpringCloud对Feign进行了整合,并且使用起来非常简单方便,接下来使用上一篇文章中的工程作为基础进行讲解。

二.Feign的使用

创建新的SpringBoot项目作为FeignClient。

1.添加依赖

<groupId>com.ywp</groupId>
	<artifactId>FeignClient</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>FeignClient</name>
	<description>Demo for FeignClient</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.1.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>Finchley.M7</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>
		<!-- Feign依赖 -->
		<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.boot</groupId>
			<artifactId>spring-boot-starter-actuator</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>

	<repositories>
		<repository>
			<id>spring-snapshots</id>
			<name>Spring Snapshots</name>
			<url>https://repo.spring.io/snapshot</url>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</repository>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>

2.修改application.properties

   

#服务端口
server.port=6002
#eureka主机名,会在控制页面中显示
eureka.instance.hostname=localhost
#eureka服务器地址
eureka.client.service-url.defaultZone=http://localhost:7001/eureka/
#服务名
spring.application.name=feign-01

3.添加注解

在启动类FeignClientApplication上添加@EnableFeignClients注解

@SpringBootApplication
@EnableFeignClients
@EnableConfigurationProperties
public class FeignClientApplication {

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

4.添加Service发起调用

创建FileService接口,添加@FeignClient注解,并且添加接口方法getInfo

@FeignClient(value = "fileServer")
public interface FileService {

	@RequestMapping(value = "/static/info", method = RequestMethod.POST)
	public String getInfo();
}

@FeignClient:

 

value:需要访问的服务名,即服务的spring.application.name

@RequestMapping: 与被调服务对应的接口相同,此处表示请求路径为/static/info,请求方法为POST

 

添加Controller调用getInfo()方法用于测试。

@Controller
public class MainController {
	@Autowired
	FileService fileService;

	@RequestMapping(value = "fileServerInfo", method = RequestMethod.GET)
	@ResponseBody
	public String fileServerInfo(HttpServletResponse response) {
		return fileService.getInfo();
	}
}
 

5.测试

1) 依次启动EurekaServer、FileServer、FeignClient。

2) 访问Eureka地址http://localhost:7001/ ,查看是否注册成功

3) 访问http://localhost:6002/fileServerInfo

可以发现FeignClient收到/fileServerInfo请求后调用了FileServer的/static/info接口,并且将返回数据响应到浏览器端。

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值