上面看到直接通过网关访问微服务是可以实现按区域调用的, 那么微服务之间调用是否也能按区域划分哪?

下面我们使用FeignClient来调用微服务, 就可以配合LoadBalancer实现按区域调用.

首先我们新建一个微服务模块 hello-nameservice, 用来调用 hello-remotename服务. 模块需要使用Feign, 还要开启Feign的负载均衡, pom.xml文件如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>com.cnscud.betazone</groupId>
  7. <artifactId>betazone-root</artifactId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <artifactId>hello-nameservice</artifactId>
  12. <version>0.0.1-SNAPSHOT</version>
  13. <name>hello-nameservice</name>
  14. <description>Demo project for Spring Boot</description>
  15. <dependencies>
  16. <dependency>
  17. <groupId>com.cnscud.betazone</groupId>
  18. <artifactId>hello-remotename-core</artifactId>
  19. <version>0.0.1-SNAPSHOT</version>
  20. </dependency>
  21. <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
  22. <dependency>
  23. <groupId>org.apache.commons</groupId>
  24. <artifactId>commons-lang3</artifactId>
  25. <version>3.0</version>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.cloud</groupId>
  29. <artifactId>spring-cloud-starter-openfeign</artifactId>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.springframework.boot</groupId>
  33. <artifactId>spring-boot-starter-web</artifactId>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.springframework.cloud</groupId>
  37. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.springframework.boot</groupId>
  41. <artifactId>spring-boot-starter-webflux</artifactId>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.springframework.cloud</groupId>
  45. <artifactId>spring-cloud-loadbalancer</artifactId>
  46. </dependency>
  47. </dependencies>
  48. <build>
  49. <plugins>
  50. <plugin>
  51. <groupId>org.springframework.boot</groupId>
  52. <artifactId>spring-boot-maven-plugin</artifactId>
  53. </plugin>
  54. </plugins>
  55. </build>
  56. </project>

注意上面的 webflux , 用于新版本的DiscoveryClient适配.

使用Feign调用微服务

首先声明一个被调用服务的Feign接口, 如下

  1. @FeignClient(value = "betazone-hello-remotename")
  2. public interface FeignRemoteNameService extends RemoteNameService {
  3. @RequestMapping("/remote/id/{id}")
  4. @Override
  5. String readName(@PathVariable("id") int id) ;
  6. }

这个类映射到前面讲过的 "betazone-hello-remotename", 接口格式一致, 使用FeignClient标注.

然后我们实现自己的微服务逻辑:

  1. package com.cnscud.betazone.hellonameservice.feign;
  2. import org.apache.commons.lang3.StringUtils;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.core.env.Environment;
  7. import org.springframework.web.bind.annotation.PathVariable;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RestController;
  10. import java.net.Inet4Address;
  11. import java.net.InetAddress;
  12. import java.net.UnknownHostException;
  13. /**
  14. * Hello Controller from Remote Service.
  15. *
  16. * @author Felix Zhang 2021-06-04 09:29
  17. * @version 1.0.0
  18. */
  19. @RestController
  20. @RequestMapping("remote")
  21. public class HelloNameByRemoteController {
  22. private static Logger logger = LoggerFactory.getLogger(HelloNameByRemoteController.class);
  23. @Autowired
  24. private FeignRemoteNameService feignRemoteNameService;
  25. @Autowired
  26. Environment environment;
  27. @RequestMapping("/id/{userid}")
  28. public String helloById(@PathVariable("userid") String userid) {
  29. logger.debug("call helloById with " + userid);
  30. if (StringUtils.isNotBlank(userid) && StringUtils.isNumeric(userid)) {
  31. return "hello " + feignRemoteNameService.readName(Integer.parseInt(userid)) + getServerName();
  32. }
  33. return "hello guest" + getServerName();
  34. }
  35. //......其他代码
  36. }

这个类里面注入了FeignRemoteNameService服务, Feign会自动初始化.

为了让Feign能用, 我们还必须启用 @EnableFeignClients(basePackages = "com.cnscud.betazone.hellonameservice"), 包名就是你的服务的包名. 声明可以放在HelloNameServiceApplication 类里面.

准备一下应用的配置 application.yml

  1. server:
  2. port: 8101
  3. spring:
  4. application:
  5. name: betazone-hello-nameservice
  6. cloud:
  7. loadbalancer:
  8. ribbon:
  9. enabled: false
  10. eureka:
  11. instance:
  12. prefer-ip-address: true
  13. metadata-map:
  14. zone: main #服务区域
  15. client:
  16. register-with-eureka: true
  17. fetch-registry: true
  18. service-url:
  19. defaultZone: http://localhost:8001/eureka/
  20. logging:
  21. level:
  22. org.springframework.cloud: debug

启动应用, 访问 http://localhost:8101/remote/id/2 , 正常情况下, 访问到的remotename服务是不确定的, 9001或者9002, 看来还是需要做一些设置才能按区域生效.

通用, 回想上一节的内容, 我们使用zone-preference 或者自定义ServiceInstanceListSupplier都可以实现, 这里不在重复.

代码里依然使用了 SamezoneAutoConfiguration 和CustomLoadBalancerConfiguration, 就可以按区域访问了.

我们在复制一份配置,用于beta区域, application-beta.yml 如下

  1. server:
  2. port: 8103
  3. spring:
  4. application:
  5. name: betazone-hello-nameservice
  6. cloud:
  7. loadbalancer:
  8. ribbon:
  9. enabled: false
  10. eureka:
  11. instance:
  12. prefer-ip-address: true
  13. metadata-map:
  14. zone: beta # zone服务区域 beta
  15. client:
  16. register-with-eureka: true
  17. fetch-registry: true
  18. service-url:
  19. defaultZone: http://localhost:8001/eureka/
  20. logging:
  21. level:
  22. org.springframework.cloud: debug

这个实例用于接下来演示区域继承的beta区域用途.

网关里声明betazone-hello-nameservice 微服务

让我们回到之前的hello-gateway项目, 修改application.yml等三个文件, routes节点下变更为:

  1. routes:
  2. - id: default
  3. uri: lb://betazone-hello-nameservice
  4. predicates:
  5. - Path=/api/**
  6. filters:
  7. - StripPrefix=1
  8. - id: remotename
  9. uri: lb://betazone-hello-remotename
  10. predicates:
  11. - Path=/remoteapi/**
  12. filters:
  13. - StripPrefix=1

这样网关就代理了betazone-hello-nameservice服务, 路径为/api . 重新启动三个gateway应用的实例, 试着访问

到此, 我们的目的达到, 可以按区域来划分服务了.

特殊备注: 如果某个微服务缺少某个区域的实例, 此项目用的ServiceInstanceListSupplier会自动使用所有实例, 那此时zone的继承就继承的是被使用的实例的zone了, 而不是网关的zone设置了.

项目图如下:

实际应用场景?

假设我们有个网站, 正常访问是 http://www.cnscud.com , 线上预发布时 http://beta.cnscud.com , 就可以设置两个区域, 通过Nginx映射, 就可以映射到不同的网关, 就自然可以区分了.

项目源码: https://github.com/cnscud/javaroom/tree/main/betazone2

接下来我们试试定制自己的自己定制一下ServiceInstanceListSupplier ......

Spring Cloud分区发布实践(4) FeignClient的更多相关文章

  1. Spring Cloud分区发布实践(1) 环境准备

    最近研究了一下Spring Cloud里面的灰度发布, 看到各种各样的使用方式, 真是纷繁复杂, 眼花缭乱, 不同的场景需要不同的解决思路. 那我们也来实践一下最简单的场景: 区域划分: 服务分为be ...

  2. Spring Cloud分区发布实践(6)--灰度服务-根据Header选择实例区域

    此文是一个完整的例子, 包含可运行起来的源码. 此例子包含以下部分: 网关层实现自定义LoadBalancer, 根据Header选取实例 服务中的Feign使用拦截器, 读取Header Feign ...

  3. Spring Cloud分区发布实践(3) 网关和负载均衡

    注意: 因为涉及到配置测试切换, 中间环节需按此文章操作体验, 代码仓库里面的只有最后一步的代码 准备好了微服务, 那我们就来看看网关+负载均衡如何一起工作 新建一个模块hello-gateway, ...

  4. Spring Cloud分区发布实践(5)--定制ServiceInstanceListSupplier

    现在我们简单地来定制二个 ServiceInstanceListSupplier, 都是zone-preference的变种. 为了方便, 我重新调整了一下项目的结构, 把一些公用的类移动到hello ...

  5. Spring Cloud分区发布实践(2) 微服务

    我们准备一下用于查询姓名的微服务. 首先定义一下服务的接口, 新建一个空的Maven模块hello-remotename-core, 里面新建一个类: public interface RemoteN ...

  6. spring cloud微服务实践二

    在上一篇,我们已经搭建了spring cloud微服务中的注册中心.但只有一个注册中心还远远不够. 接下来我们就来尝试提供服务. 注:这一个系列的开发环境版本为 java1.8, spring boo ...

  7. 厉害了,Spring Cloud Alibaba 发布 GA 版本!

    ? 小马哥 & Josh Long ? 喜欢写一首诗一般的代码,更喜欢和你共同 code review,英雄的相惜,犹如时间沉淀下来的对话,历久方弥新. 相见如故,@杭州. 4 月 18 日, ...

  8. Spring Cloud Alibaba发布第二个版本,Spring 发来贺电

    还是熟悉的面孔,还是熟悉的味道,不同的是,这次的配方升级了. 今年10月底,Spring Cloud联合创始人Spencer Gibb在Spring官网的博客页面宣布:阿里巴巴开源 Spring Cl ...

  9. Spring Boot 2.x 已经发布了很久,现在 Spring Cloud 也发布了 基于 Spring Boot 2.x 的 Finchley 版本,现在一起为项目做一次整体框架升级。

    升级前 => 升级后 Spring Boot 1.5.x => Spring Boot 2.0.2 Spring Cloud Edgware SR4 => Spring Cloud ...

随机推荐

  1. linux常用命令及一些静态动态库相关知识

    1 查找然后grep,最后在复制到特定目录 find . -depth -name *.java | xargs grep -i lijiangtao | awk -F ":" ' ...

  2. JavaScript的介绍概括

    1.js是一种轻型的解释性的脚本语言,称为web脚本语言. 2.js的执行原理:当客户端向服务器端请求某个页面时,浏览器端将整个页面中包含JavaScript的脚本代码作为响应内容,发送到客户端的机器 ...

  3. Java并发之ReentrantLock源码解析(一)

    ReentrantLock ReentrantLock是一种可重入的互斥锁,它的行为和作用与关键字synchronized有些类似,在并发场景下可以让多个线程按照一定的顺序访问同一资源.相比synch ...

  4. AI框架中图层IR的分析

    摘要:本文重点分析一下AI框架对IR有什么特殊的需求.业界有什么样的方案以及MindSpore的一些思考. 本文分享自华为云社区<MindSpore技术专栏 | AI框架中图层IR的分析> ...

  5. excel VBA中正则模块vbscript.regexp的用法

    一.是一个对象,用于执行 正则表达式! 二.有三个属性:      1. Global属性: True or False, 指明模式是匹配整个字符串中所有与之相符的地方还是只匹配第一次出现的地方.默认 ...

  6. 常见DDoS攻击

    导航: 这里将一个案例事项按照流程进行了整合,这样观察起来比较清晰.部分资料来自于Cloudflare 1.DDoS介绍 2.常用DDoS攻击 3.DDoS防护方式以及产品 4.Cloudflare ...

  7. 95、配置ntp服务器

    95.1.ntp简介: ntp服务使用的是udp的123端口,如果开启了防火墙要记得放开这个端口: NTP(Network Time Protocol,网络时间协议)是用来使网络中的各个计算机时间同步 ...

  8. 45、django工程(URLconf)

    45.1.django URLconf 路由系统介绍: 1.说明: URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL模式以及要为该URL模式调用的视图函数之间的映射表, ...

  9. Linux从头学02:x86中内存【段寻址】方式的来龙去脉

    作 者:道哥,10+年的嵌入式开发老兵. 公众号:[IOT物联网小镇],专注于:C/C++.Linux操作系统.应用程序设计.物联网.单片机和嵌入式开发等领域. 公众号回复[书籍],获取 Linux. ...

  10. 明明是企业管理软件,CRM系统为何被抵触?

    小编在昨天的文章<CRM系统为什么没有达到预期效果?>中曾说过,CRM客户管理系统没有达到预期效果的其中一个原因是CRM系统的使用率太低,而根本的原因是员工的抵触.明明是企业管理大师,CR ...