springcloud 当前服务端口号_Spring Cloud学习笔记--声名式服务调用(Feign)

62cf9263a8e2427c7445f27725b7d33e.png
Feign is a declarative web service client. It makes writing web service clients easier. To use Feign create an interface and annotate it. It has pluggable annotation support including Feign annotations and JAX-RS annotations. Feign also supports pluggable encoders and decoders. Spring Cloud adds support for Spring MVC annotations and for using the same HttpMessageConverters used by default in Spring Web. Spring Cloud integrates Ribbon and Eureka to provide a load balanced http client when using Feign. [1]

Feign是一个声明式Web Service客户端。它可以让编写Web Service客户端更加简单。

不使用Feign:

34f3c99d9cc1a74ffc7b8898be2100b9.png

使用Feign以后:

b0437e1522c7bc8bdd987c6aa26ffe2e.png

注册中心,服务提供者如何搭建请参考这篇文章:

五雨:Spring Cloud学习笔记--负载均衡(Ribbon)​zhuanlan.zhihu.com
c12d96b0ad1a6d7cc3374f49c5cedcff.png

我们来改造一下服务调用者:

首先,pom文件需要增加spring-cloud-starter-openfeign的引用。

<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-ribbon</artifactId>
    <version>1.3.0.RC1</version>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

服务调用者的yml文件:

server:
  #服务调用者端口号
  port: 50002
spring:
  application:
    name: ServerClient
eureka:
  client:
    service-url:
      #将当前服务也注册到注册中心
      defaultZone: http://eurekaserver1:30001/eureka/

主启动文件增加EnableFeignClients注解:

48aab696de00f72c6153ae33b96587a8.png

定义一个ClientTestService接口,代码如下:

package com.springclouddemo.springcloudclient.Service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient(value = "ServerProvide")
public interface ClientTestService {
    @GetMapping("/ServerTest")
    String ClientTestServiceMethod();
}

其中通过@FeignClient(value = "ServerProvide") 来绑定服务名,当前我绑定的是ServerProvide的服务。

Controller代码如下:

package com.springclouddemo.springcloudclient.controller;

import com.springclouddemo.springcloudclient.Service.ClientTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Configuration
public class FeignController {
    @Autowired
    ClientTestService clientTestService;

    @GetMapping("/getTestMsgByFeign")
    public String findById(){
        return clientTestService.ClientTestServiceMethod();
    }
}

启动运行测试一下。

484cc57b53e4fa2fce1920f9907a4065.png

7a4f323f9ed7ad453517a6bf451b38fb.png

咦,为什么没有配置ribbon,依旧还有负载均衡的策略?

因为feign是对ribbon的封装,当前例子采用了默认的负载均衡策略(轮询)。

feign对HyStrix的支持也非常友好,我们看下怎么做。

首先在yml中开启断路器。

yml配置文件如下:

server:
  #服务调用者端口号
  port: 50002
spring:
  application:
    name: ServerClient
eureka:
  client:
    service-url:
      #将当前服务也注册到注册中心
      defaultZone: http://eurekaserver1:30001/eureka/
feign:
  hystrix:
    enabled: true

创建ClientTestService的实现类,代码如下:

package com.springclouddemo.springcloudclient.Service;

import org.springframework.stereotype.Component;

@Component
public class ClientTestServiceFallback implements ClientTestService{
    @Override
    public String ClientTestServiceMethod() {
        return "网络繁忙,请稍后再试。(这里是服务调用者使用Feign的快速失败给出的提示)";
    }
}

修改ClientTestService,代码如下:

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient(value = "ServerProvide",fallback = ClientTestServiceFallback.class)
public interface ClientTestService {
    @GetMapping("/ServerTest")
    String ClientTestServiceMethod();
}

运行一下。

95296e02596e6f5365f2e141f0905018.png

我把端口号为40001的服务停止,模拟宕机

21f6f465ce707ef28d09733b275d0676.png

可以看出,HyStrix已经起了作用。

参考

  1. ^Declarative REST Client(官方文档) https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-feign.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值