Spring Cloud分区发布实践(2) 微服务
我们准备一下用于查询姓名的微服务.
首先定义一下服务的接口, 新建一个空的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/2 或 http://localhost:9002/remote/id/2 则可以看到我们刚刚运行的服务.
项目代码: https://github.com/cnscud/javaroom/tree/main/betazone2/hello-remotename
接下来我们看看使用gateway代理服务的效果...
最新文章
- [翻译]通过使用正确的search arguments来提高SQL Server数据库的性能
- Node.js 爬虫初探
- Android之ProgressBar
- tinyMCE自定义添加图片插件
- (转)《深入理解java虚拟机》学习笔记5——Java Class类文件结构
- PHP【第一篇】安装
- 旧版Xcode下载地址
- php 连接mysql数据库并显示数据 实例 转载
- Linux如何用QQ?Linux下QQ使用的几种方案
- java.lang.NoSuchMethodError: org.springframework.boot.builder.SpringApplicationBuilder.<;init>;([Ljava
- Ubuntu: 安装自带的中文输入法
- netty-socketio(一)之helloworld,与springboot整合
- Nginx中文url出现404问题
- [ VB ] If 运算符 [ C# ] 条件运算符 (?:)
- 新建web项目myeclipse基本设置
- 配置java环境jdk
- Java基础学习笔记(四)
- C# Newtonsoft.Json解析数组的小例子[转]
- C#访问MySQL数据库帮助类
- S1 商品信息管理系统
热门文章
- 基于GIS的国土空间规划平台建设
- Bean初始化操作initMethod、@PostConstruct和InitializingBean
- MaterialDesignInXamlToolkit“无法绑定到目标方法,因其签名或安全透明度与委托类型的签名或安全透明度不兼容”异常的解决思路
- 关于XXE漏洞
- centos 8.3安装 一键安装部署gitlab
- Ubuntu创建图标
- uni-app app端 人脸识别
- 扩大UIPageViewController的点击范围
- 74cms v5.0.1 前台sql注⼊复现
- 从GAN到WGAN的来龙去脉