Spring Cloud入门教程-声明式调用 Feign

上一篇博客讲了如何使用RestTemplate结合Ribbon 调用服务并实现负载均衡。这里要shuo说一下如何使用Feign来远程调度其他服务。

创建新的Moudel eureka-client-feign

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.springcloud.demo</groupId>
		<artifactId>springcloud-demo</artifactId>
		<version>1.0-SNAPSHOT</version>
	</parent>
	<artifactId>eureka-client-feign</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
 
	<name>eureka-client-feign</name>
	<description>Demo project for Spring Boot</description>
 
	<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-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
 
	</dependencies>
 
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
 
</project>

添加配置与eureka-client-ribbon 相同,设置端口为8794

server.port=8794
 
spring.application.name=eureka-client-feign
 
eureka.client.service-url.defaultZone=http://localhost:8791/eureka/

在EurekaClientFeignApplication添加@EnableEurekaClient @EnableFeignClients注解

@EnableEurekaClient
@EnableFeignClients
@SpringBootApplication
@ComponentScan("com.springcloud.demo.eurekaclientfeign")
public class EurekaClientFeignApplication {
 
	public static void main(String[] args) {
		SpringApplication.run(EurekaClientFeignApplication.class, args);
	}
}

创建Feign 配置类 FeignConfig

@Configuration
public class FeignConfig {
    @Bean
    public Retryer feignRetryer() {
        return new Retryer.Default(100, SECONDS.toMillis(1), 5);
    }
 
}

注入Bean 后,Feign 在远程调用失败后hui会jing进行chng重试。

创建FeignService 接口实现feign Client

@FeignClient(value = "eureka-client", configuration = FeignConfig.class)
public interface FeignService {
    @GetMapping("main")
    int main();
}

注意:@FeignClient 声明一个Feign Client,

value 为远程调用服务名,

FeignConfig.class 配置类

main方法通过注解调用 eureka-client 的 “/main” 的API.

这里省略service 层,直接在controller中调用,创建controller:

@RestController
public class MainController {
    @Autowired
    FeignService feignService;
 
    @GetMapping("main")
    public int getMsg() {
        return feignService.main();
    }
}

启动eureka-server ,两个eureka-client 实例,启动eureka-feign ,请求http://localhost:8794/main
多次请求会交替出现8792,8793.


版权声明:本文为CSDN博主「天青色等烟雨11」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_27828675/article/details/83413711

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值