Eclipse(STS) 初次搭建Spring Cloud项目之声明式REST调用+负载均衡实现Feign(四)

一、什么是Feign

  1. Feign是一个http请求调用的轻量级框架,可以以Java接口注解的方式调用Http请求,而不用像Java中通过封装HTTP请求报文的方式直接调用。Feign通过处理注解,将请求模板化,当实际调用的时候,传入参数,根据参数再应用到请求上,进而转化成真正的请求,这种请求相对而言比较直观。
  2. Feign被广泛应用在Spring Cloud 的解决方案中,是学习基于Spring Cloud 微服务架构不可或缺的重要组件。
  3. Feign具有可插拔的注解特性,可使用Feign注解和JAX-RS注解。
  4. Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。

二、下面来看看具体实现

首先创建Feign模块,cloud-demo-feign

下面是项目结构

pom文件如下

<?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.hewl</groupId>
    <artifactId>cloud-demo-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>cloud-demo-feign</artifactId>
  <name>cloud-demo-feign</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
   		<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>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-openfeign</artifactId>
		</dependency>
  </dependencies>
</project>
复制代码

application.yml文件如下

server:
 port: 8811 # 你的端口
spring:
  application:
    name: feign
eureka:
  client:
    service-url:
      defaultZone: http://admin:admin@server1:8761/eureka/,http://admin:admin@server2:8762/eureka/
复制代码

启动类内容如下,加上@EnableFeignClients注解开启Feign的功能

package com.hewl;

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

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class FeignApplication {
	
	public static void main(String[] args) {
		SpringApplication.run(FeignApplication.class, args);
	}
}
复制代码

定义Feign接口,SchedualCilentName

package com.hewl.feign;

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

@FeignClient(value="service-eureka-client")
public interface SchedualCilentName {
	
	@GetMapping(value = "/test")
	public String testFromClientOne(@RequestParam("name") String name);

}
复制代码

定义对外的controller

package com.hewl.controller;

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

import com.hewl.feign.SchedualCilentName;

@RestController
public class TestController {

	@Autowired
	public SchedualCilentName schedualCilentName;
	
	@GetMapping(value="/test")
	public String test (String name) {
		return schedualCilentName.testFromClientOne(name);
	}
}

复制代码

按顺序启动项目

  1. 启动eureka-server
  2. 修改端口分别启动两次eureka-client
  3. 启动feign客户端

访问localhost:8811/test?name=张三,访问成功并且已经成功启动负载均衡

转载于:https://juejin.im/post/5d01e679f265da1b5f265021

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值