我们准备一下用于查询姓名的微服务.

首先定义一下服务的接口, 新建一个空的Maven模块hello-remotename-core, 里面新建一个类:

public interface RemoteNameService {

    String readName(int id) ;
}

接下来的微服务都实现这个简单的接口作为示范.

然后创建一个服务模块hello-remotename, 依然使用 Spring Initializr, 选择 "Spring Web", "Eureka Discovery Client" 2个模块.

其中的pom文件如下:

<?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-remotename</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hello-remotename</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> <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> </dependencies> </project>

此模块依赖接口模块, 用于实现接口. 然后我们实现一个服务的Controller, 如下:

package com.cnscud.betazone.helloremotename.controller;

import com.cnscud.betazone.helloremotename.core.service.RemoteNameService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
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;
import java.util.HashMap;
import java.util.Map; @RestController
@RequestMapping("remote")
public class RemoteNameServiceController implements RemoteNameService {
private static Logger logger = LoggerFactory.getLogger(RemoteNameServiceController.class); @Autowired
Environment environment; final static String defaultName = "guest";
static Map<Integer, String> names = new HashMap<>(); static {
names.put(1, "Felix");
names.put(2, "World");
names.put(3, "Sea");
names.put(4, "Sky");
names.put(5, "Mountain");
} @Override
@RequestMapping("/id/{id}")
public String readName(@PathVariable("id") int id) { if( names.get(id) == null ) {
return defaultName + getServerName();
}
else
{
return names.get(id) + getServerName();
}
} public String readServicePort() {
return environment.getProperty("local.server.port");
} public String readServiceIp() {
InetAddress localHost = null;
try {
localHost = Inet4Address.getLocalHost();
}
catch (UnknownHostException e) {
logger.error(e.getMessage(), e);
} return localHost.getHostAddress(); // 返回格式为:xxx.xxx.xxx
} public String getServerName() {
return " [remotename: " + readServiceIp() + ":" + readServicePort() + "]";
} }

RemoteNameServiceController实现了RemoteNameService 接口, 为了后续方便区分是哪个实例在服务, 返回的信息里增加了IP和端口信息.

然后声明application.yml, 在9001端口启动

server:
port: 9001 spring:
application:
name: betazone-hello-remotename 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

可以看到, 此实例端口为9001, 服务区域zone设置为 main.

然后在复制一个为 application-beta.yml, 修改如下

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

此实例设置端口为9002, 服务区域为 beta.

启动第一个Application, 然后复制配置, 修改profile为beta , 启动第二个实例.

此时去Eureka查看, 可以看到betazone-hello-remotename有2个服务, 使用xml查看 http://localhost:8001/eureka/apps , 可以看到不同的metadata.

点击访问 http://localhost:9001/remote/id/2http://localhost:9002/remote/id/2 则可以看到我们刚刚运行的服务.

项目代码: https://github.com/cnscud/javaroom/tree/main/betazone2/hello-remotename

接下来我们看看使用gateway代理服务的效果...

Spring Cloud分区发布实践(2) 微服务的更多相关文章

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

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

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

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

  3. Spring Cloud分区发布实践(4) FeignClient

    上面看到直接通过网关访问微服务是可以实现按区域调用的, 那么微服务之间调用是否也能按区域划分哪? 下面我们使用FeignClient来调用微服务, 就可以配合LoadBalancer实现按区域调用. ...

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

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

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

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

  6. 基于Spring Cloud和Netflix OSS构建微服务,Part 2

    在上一篇文章中,我们已使用Spring Cloud和Netflix OSS中的核心组件,如Eureka.Ribbon和Zuul,部分实现了操作模型(operations model),允许单独部署的微 ...

  7. 今天介绍一下自己的开源项目,一款以spring cloud alibaba为核心的微服务架构项目,为给企业与个人提供一个零开发基础的微服务架构。

    LaoCat-Spring-Cloud-Scaffold 一款以spring cloud alibab 为核心的微服务框架,主要目标为了提升自己的相关技术,也为了给企业与个人提供一个零开发基础的微服务 ...

  8. spring cloud 入门,看一个微服务框架的「五脏六腑」

    Spring Cloud 是一个基于 Spring Boot 实现的微服务框架,它包含了实现微服务架构所需的各种组件. 注:Spring Boot 简单理解就是简化 Spring 项目的搭建.配置.组 ...

  9. 使用 Spring Cloud Stream 构建消息驱动微服务

    相关源码: spring cloud demo 微服务的目的: 松耦合 事件驱动的优势:高度解耦 Spring Cloud Stream 的几个概念 Spring Cloud Stream is a ...

随机推荐

  1. git stash回退

    目录 1.起因 2. 问题出现 3.修复 4. 注意 1.起因 这个问题要从今天刚遇到的事儿说起,昨晚代码出了个乌龙事件,本来正在dev分支进行新功能的开发,但是测试出现的问题是在release 分支 ...

  2. 服务器硬件和RAID配置

    服务器 硬件和RAID配置 目录 一.RAID 磁盘阵列介绍 1.1.RAID 0(条带化存储) 1.2.RAID 1(镜像存储) 1.3.RAID 5 1.4.RAID 6 1.5.RAID 1+0 ...

  3. 『无为则无心』Python序列 — 17、Python字符串操作常用API

    目录 1.字符串的查找 @1.find()方法 @2.index()方法 @3.rfind()和rindex()方法 @4.count()方法 2.字符串的修改 @1.replace()方法 @2.s ...

  4. 创建Akamai cdn api授权

    注:通过Akamai Cli purge和通过Akamai API进行刷新之前,都要事先创建类似于如下的刷新的凭据,这两种刷新方式所创建的凭据是相同的. 目的:创建Akamai CDN API授权以便 ...

  5. 第14章:部署Java网站项目案例

    1 说明 (1) 项目迁移到k8s平台的流程 1) 制作镜像 dockerfile.docker+jenkins持续集成.镜像分类:基础镜像.中间镜像.项目镜像 2) 控制器管理pod 控制器管理po ...

  6. POJ 1584 A Round Peg in a Ground Hole 判断凸多边形 点到线段距离 点在多边形内

    首先判断是不是凸多边形 然后判断圆是否在凸多边形内 不知道给出的点是顺时针还是逆时针,所以用判断是否在多边形内的模板,不用是否在凸多边形内的模板 POJ 1584 A Round Peg in a G ...

  7. 资源:Git快速下载路径

    Git快速下载地址: 地址:https://npm.taobao.org/mirrors/git-for-windows/

  8. 视图:DBA_TAB_PARTITIONS 分区表视图

    Column Datatype 释义 Description TABLE_OWNER VARCHAR2(128) 表的owner Owner of the table TABLE_NAME VARCH ...

  9. IP地址与子网化分

    IP地址与子网掩码 一.IP地址的组成 二.IP地址的分类                            1)A.B.C三类地址的组成                            2 ...

  10. passwd 简单记录

    passwd [选项] 登录名 -e,--expire 强制用户密码过期 这时候需要使用root账户给tel用户重新设置密码 -l,--lock 锁定指定用户密码 -u, --unlock 给指定账户 ...