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


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

查看所有标签

猜你喜欢:

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

高分辨率遥感卫星应用

高分辨率遥感卫星应用

张永 / 科学分社 / 2004-1 / 48.00元

高分辨率遥感卫星应用(成像模型处理算法及应用技术),ISBN:9787030128249,作者:张永生等著一起来看看 《高分辨率遥感卫星应用》 这本书的介绍吧!

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

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

UNIX 时间戳转换

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具