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

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

内容简介: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


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

查看所有标签

猜你喜欢:

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

百面机器学习

百面机器学习

诸葛越、葫芦娃 / 人民邮电出版社 / 2018-8-1 / 89.00元

人工智能领域正在以超乎人们想象的速度发展,本书赶在人工智能彻底占领世界之前完成编写,实属万幸。 书中收录了超过100道机器学习算法工程师的面试题目和解答,其中大部分源于Hulu算法研究岗位的真实场景。本书从日常工作、生活中各种有趣的现象出发,不仅囊括了机器学习的基本知识 ,而且还包含了成为出众算法工程师的相关技能,更重要的是凝聚了笔者对人工智能领域的一颗热忱之心,旨在培养读者发现问题、解决问......一起来看看 《百面机器学习》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

URL 编码/解码
URL 编码/解码

URL 编码/解码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具