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

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

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

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.cnscud.betazone</groupId>
<artifactId>betazone-root</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <artifactId>hello-nameservice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hello-nameservice</name>
<description>Demo project for Spring Boot</description> <dependencies>
<dependency>
<groupId>com.cnscud.betazone</groupId>
<artifactId>hello-remotename-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.0</version>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

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

使用Feign调用微服务

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

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

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

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

package com.cnscud.betazone.hellonameservice.feign;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException; /**
* Hello Controller from Remote Service.
*
* @author Felix Zhang 2021-06-04 09:29
* @version 1.0.0
*/
@RestController
@RequestMapping("remote")
public class HelloNameByRemoteController { private static Logger logger = LoggerFactory.getLogger(HelloNameByRemoteController.class); @Autowired
private FeignRemoteNameService feignRemoteNameService; @Autowired
Environment environment; @RequestMapping("/id/{userid}")
public String helloById(@PathVariable("userid") String userid) {
logger.debug("call helloById with " + userid); if (StringUtils.isNotBlank(userid) && StringUtils.isNumeric(userid)) {
return "hello " + feignRemoteNameService.readName(Integer.parseInt(userid)) + getServerName();
} return "hello guest" + getServerName();
} //......其他代码 }

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

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

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

server:
port: 8101 spring:
application:
name: betazone-hello-nameservice
cloud:
loadbalancer:
ribbon:
enabled: false eureka:
instance:
prefer-ip-address: true
metadata-map:
zone: main #服务区域
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8001/eureka/ logging:
level:
org.springframework.cloud: debug

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

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

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

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

server:
port: 8103 spring:
application:
name: betazone-hello-nameservice
cloud:
loadbalancer:
ribbon:
enabled: false eureka:
instance:
prefer-ip-address: true
metadata-map:
zone: beta # zone服务区域 beta
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8001/eureka/ logging:
level:
org.springframework.cloud: debug

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

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

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

      routes:
- id: default
uri: lb://betazone-hello-nameservice
predicates:
- Path=/api/**
filters:
- StripPrefix=1
- id: remotename
uri: lb://betazone-hello-remotename
predicates:
- Path=/remoteapi/**
filters:
- 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. DOS命令行(1)——Windows目录与文件应用操作

    cd 1.使用cd快速切换到指定盘符与目录中 命令格式1:cd [/d] [<盘符>][<路径>] 或 chdir [/d] [<盘符>][<路径>] ...

  2. windows 上 OpenSSH 服务 启用秘钥登录(微软真心逆天)

    windows 上 OpenSSH 服务 启用秘钥登录(微软真心逆天) windows 安装 OpenSSH 服务 最近需要在windows 服务器上部署自动发布程序,那么就需要用到 scp 和 ss ...

  3. 详解Redis主从复制原理

    文章首发于公众号 "蘑菇睡不着" 前言 Redis 的主从复制和 MySQL 差不多,主要起着 数据备份,读写分离等作用.所以说主从复制对 Redis 来说非常重要,而无论是面试还 ...

  4. 单链表(LinkedList)

    与数组相似,链表也是一种线性数据结构.这里有一个例子:   正如你所看到的,链表中的每个元素实际上是一个单独的对象,而所有对象都通过每个元素中的引用字段链接在一起.   链表有两种类型:单链表和双链表 ...

  5. 详解 MD5 信息摘要算法

    对于软件研发人员来说 MD5 不是一个陌生的词汇,平时的软件研发中,经常使用 MD5 校验消息是否被篡改.验证文件完整性,甚至将MD5当作加密算法使用. MD5虽不陌生,但不是所有研发人员都了解其算法 ...

  6. WebService:WebService+Springboot常用注解

    首先推荐webservice文章不错的博主:https://www.iteye.com/blog/yufenfei-1685249 这位博主主要讲了WebService的CXF的jar包运用,很实用 ...

  7. 2shell中处理字符串,字符串的截取、替换

    0.字符串的小知识点 1.字符串的截取 1.1从指定位置开始截取 1.2 从指定字符(子字符串)开始截取 1.3字符串截取的总结 1.4 按指定要求截取 2.字符串的拼接 3.字符串的替换 0.字符串 ...

  8. linux菜鸡学习之路

    Linux入门 Linux 介绍 1.Linux怎么读 2.Linux是一款操作系统,免费,开源,安全,高效,稳定,处理高并发非常强悍. Linux文件系统目录 基本介绍 linux的文件系统树状目录 ...

  9. Jenkins+Sonar 项目构建前代码审查

    一.sonar简介 1.概述 Sonar (SonarQube)是一个开源平台,用于持续检查代码质量,不只是一个质量数据报告工具,更是代码质量管理平台. 支持Java, C#, C/C++, PL/S ...

  10. 详解Spring中Bean的作用域与生命周期

    摘要:在利用Spring进行IOC配置时,关于bean的配置和使用一直都是比较重要的一部分,同时如何合理的使用和创建bean对象,也是小伙伴们在学习和使用Spring时需要注意的部分,所以这一篇文章我 ...