Spring Cloud系列教程三 :声明式服务调用Spring Cloud Fegin(F版)

栏目: IT技术 · 发布时间: 6年前

内容简介:Feign集成了Ribbon,让客户端具有负载均衡的能力,同时简化了开发github地址:https://github.com/erlieStar/spring-cloud-learning1.项目配置如下

Feign集成了Ribbon,让客户端具有负载均衡的能力,同时简化了开发

github地址:https://github.com/erlieStar/spring-cloud-learning

1.项目配置如下

pom.xml

<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>

application.yaml

server:
  port: 9001

spring:
  application:
    name: consumer-feign

eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

2.启动类加上@EnableFeignClients注解开启Feign的功能

3.定义接口,通过@FeignClient(服务名)来指定调用哪个服务

@RestController
@EnableEurekaClient
@EnableFeignClients
@SpringBootApplication
public class ConsumerFeign {

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

    @Autowired
    private SimpleClient simpleClient;

    @FeignClient(value = "producer-simple")
    public interface SimpleClient {
        @RequestMapping("hello")
        String hello(@RequestParam String name);
    }

    @RequestMapping("hello")
    public String hello(@RequestParam String name) {
        return simpleClient.hello(name);
    }
}

启动eureka-service(spring-cloud-eureka)

接着启动2个producer-simple(spring-cloud-ribbon)

初始端口为8001,启动一个实例后,修改端口为8002,再启动一个实例

访问http://localhost:9001/hello?name=xiaoshi

交替显示

hello xiaoshi, I am from port: 8001
hello xiaoshi, I am from port: 8002

欢迎关注

Spring Cloud系列教程三 :声明式服务调用Spring Cloud Fegin(F版)

参考博客

[1]https://www.fangzhipeng.com/springcloud/2018/08/03/sc-f3-feign.html


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Tagging

Tagging

Gene Smith / New Riders / 2007-12-27 / GBP 28.99

Tagging is fast becoming one of the primary ways people organize and manage digital information. Tagging complements traditional organizational tools like folders and search on users desktops as well ......一起来看看 《Tagging》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换