SpringCloud:服务消费(Feign)

本文介绍Feign声明式Web客户端的使用方法,包括如何整合Ribbon实现负载均衡及Hystrix实现断路器。通过创建Feign工程,引入核心依赖,配置启动类注解,创建Feign服务接口,定义控制类,最终实现调用远程服务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、Feign简介
Feign是一个声明式的web客户端,封装Htttp请求的整个过程,构造请求、建立连接、解析响应结果等,并且整合了Ribbon来实现负载均衡及Hystrix实现断路器(下一篇文章会讲到),Feing有两个主要注解: @EnableFeignClients 用于开启feign功能,@FeignClient 用于定义feign 接口。

2、创建一个Feign工程
引入核心依赖:spring-cloud-starter-netflix-eureka-client、spring-cloud-starter-openfeign

pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.3.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>service-feign</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>service-feign</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
	</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>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</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-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
		</repository>
	</repositories>

</project>

application.yml

server:
  port: 8003

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8000/eureka

Spring:
  application:
    name: service-feigin

3、启动类加上必要的注解,@EnableDiscoveryClient开启Ribbon,@EnableFeignClients开启Feign

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceFeignApplication {

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

}

4、创建一个Feign服务,@FeignClient声明你要调用的实例名称,@RequestMapping就是这个实例下的具体方法,这里就是说要去调用eureka-client1实例下的/serviceTest/test方法,也就是我们本系列第一章中eureka-client1工程下声明的方法

/**
 * @author XuJD
 * @create 2019-11-26 11:13
 **/
@FeignClient(value = "eureka-client1")
@Service
public interface FeignService {

    @RequestMapping(value = "/serviceTest/test")
    String test();

}

5、定义一个控制类,对外提供接口测试

/**
 * @author XuJD
 * @create 2019-11-26 11:13
 **/
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private FeignService feignService;

    @RequestMapping("/hello")
    public String test(){
        return feignService.test();
    }
}

6、测试
访问 :http://localhost:8003/test/hello,可以看到8001和8002在循环打印

Hello world,this is eureka-client1--8001
Hello world,this is eureka-client1--8002

代码地址:https://github.com/xujiangdong/SpringCloud_Study
都看到这里了,点个star支持下吧,万分感谢!

aml是一种xml格式的语言。 翻译过来大概叫 安全断言(标记)语言。 这里有两个点: 第一是“安全”, 第二是“断言(assertion)”。 用人话翻译saml就是 用安全的方式表达断言一种语言。 先看它的核心概念“断言”。 断言是什么? 就是做出判断的语言。比如一句话: 小明是超级管理员。 这就是一个断言。再来一个例子:小红没有权限读取根目录。这也是一个断言。 这种“做出判断的语句”我们在很多场合都需要用到。 比如你在网上尝试登陆一个服务的时候, 这个服务需要知道你是不是合法的用户。 这个时候如果你能提供一个“安全,可靠,可信任”的断言:“小明有权登陆XX服务”, 那么这个服务就知道你合法了, 于是就能为你提供服务了。 这个例子比较抽象,但基本上能表达断言在实际用例中的作用了。 实际上saml的大部分用例就在于证明你是谁,你拥有什么权限等等了。 saml中大部分主要内容也都是类似于:你是谁, 你有什么。。等等这些简单的语句。 详细内容后面会介绍。 接下来第二个概念就是“安全”了。 你能提供一个断言, 别人能不能假冒你提供一个断言从而骗取服务端的信任呢? 另外服务端为什么会信任你给的断言呢? 这就涉及到安全的问题了。为了防止断言被假冒,篡改。saml中加入了安全措施。 当然现今能抵御假冒,篡改,重放攻击的利器就是公钥-私钥系统了。 通过给断言加上签名和加密,再结合数字证书系统就确保了saml不受攻击。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值