微服务:Eureka配置集群环境
一、注册中心编码
1.使用idea创建一个spring boot项目,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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.</modelVersion> <groupId>com.eureka</groupId>
<artifactId>ser</artifactId>
<version>0.0.-SNAPSHOT</version>
<packaging>jar</packaging> <name>ser</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.这里开发3个样例,组成集群。3个样例的pom不变,application.yml如下:
#样例1:
server:
port: eureka:
instance:
hostname: centos7-
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://centos7-02:8888/eureka/,http://centos7-03:8888/eureka/
#样例2:
server:
port: eureka:
instance:
hostname: centos7-
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://centos7-01:8888/eureka/,http://centos7-03:8888/eureka/
#样例3:
server:
port: eureka:
instance:
hostname: centos7-
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://centos7-01:8888/eureka/,http://centos7-02:8888/eureka/
3.这些样例的java代码一样:
package com.eureka.ser; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication
@EnableEurekaServer
public class SerApplication { public static void main(String[] args) {
SpringApplication.run(SerApplication.class, args);
}
}
4.打包,要在项目的根路径
二、部署运行
1.这里开启3台虚拟机,域名分布为centos7-01 centos7-02 centos7-03,上面的yml文件的hostname与之对应。将这3个jar文件分别运行
2.查看结果,在本机访问虚拟机时,需要关闭虚拟机的防火墙(或者开放端口);
把url换为centos7-02 centos7-03,会看到类似的结果,说明3台服务已经协调运行了
三、客户端注册
1. 新创建一个spring boot项目,其中pom文件如上,yml和java代码如下,然后按照上面的方式打包运行
eureka:
client:
serviceUrl:
defaultZone: http://centos7-02:8888/eureka/ ###这里只向centos7-02注册,会向另外两台会同步过去
server:
port: spring:
application:
name: eureka-cli
package com.cloud.eurekacli01; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import java.text.SimpleDateFormat;
import java.util.Date; @SpringBootApplication
@EnableEurekaClient
@RestController
public class EurekaCli01Application { public static void main(String[] args) {
SpringApplication.run(EurekaCli01Application.class, args);
} @RequestMapping("/")
public String index(){
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD hh:mm:ss");
String time = sdf.format(new Date());
return "current time is "+time;
}
}
2. 查看注册结果,打开3个中任意一个注册界面,都会有如下结果,说明客户端已经成功在集群上注册了
3.访问客户端 (客户端是在本地启动的)
微服务:Eureka配置集群环境的更多相关文章
- 交付Dubbo微服务到kubernetes集群
1.基础架构 1.1.架构图 Zookeeper是Dubbo微服务集群的注册中心 它的高可用机制和k8s的etcd集群一致 java编写,需要jdk环境 1.2.节点规划 主机名 角色 ip hdss ...
- (转)实验文档2:实战交付一套dubbo微服务到kubernetes集群
基础架构 主机名 角色 ip HDSS7-11.host.com k8s代理节点1,zk1 10.4.7.11 HDSS7-12.host.com k8s代理节点2,zk2 10.4.7.12 HDS ...
- 庐山真面目之六微服务架构Consul集群、Ocelot网关集群和Nginx版本实现
庐山真面目之六微服务架构Consul集群.Ocelot网关集群和Nginx版本实现 一.简介 在上一篇文章<庐山真面目之五微服务架构Consul集群.Ocelot网关和Nginx版本实 ...
- 庐山真面目之七微服务架构Consul集群、Ocelot网关集群和IdentityServer4版本实现
庐山真面目之七微服务架构Consul集群.Ocelot网关集群和IdentityServer4版本实现 一.简介 在上一篇文章<庐山真面目之六微服务架构Consul集群.Ocelot网 ...
- springcloud微服务基于redis集群的单点登录
springcloud微服务基于redis集群的单点登录 yls 2019-9-23 简介 本文介绍微服务架构中如何实现单点登录功能 创建三个服务: 操作redis集群的服务,用于多个服务之间共享数据 ...
- RocketMQ的安装配置:配置jdk环境,配置RocketMQ环境,配置集群环境,配置rocketmq-console
RocketMQ的安装配置 演示虚拟机环境:Centos64-1 (D:\linuxMore\centos6_64) root / itcast : 固定IP 192.168.52.128 一,配置J ...
- Zookeeper 配置集群环境详解
在Linux环境下安装zookeeper 在Linux环境下安装zookeeper 1. 将zookeeper-3.4.13.tar.gz复制到linux操作系统 2. 通过p ...
- Elasticsearch配置集群环境
环境选择: 1.方案一:准备三台机器 每一台机器一个节点 2.方案二:准备一台机器 启动三个节点,用端口号区分即可 3.ES启 ...
- 8.实战交付一套dubbo微服务到k8s集群(1)之Zookeeper部署
1.基础架构 主机名 角色 ip HDSS7-11.host.com K8S代理节点1,zk1 10.4.7.11 HDSS7-12.host.com K8S代理节点2,zk2 10.4.7.12 H ...
随机推荐
- 剑指offer 面试41题
面试41题: 题目:数据流中的中位数 题:如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值.如果从数据流中读出偶数个数值,那么中位数就是所有数值 ...
- notepad++运行python代码
notepad++运行代码 在菜单栏中点击运行或按F5,在弹出的对话框中输入下面命令 cmd /k E:\py3.6\python.exe "$(FULL_CURRENT_PATH)&quo ...
- JDK1.8(JRE)和eclipse-jee不匹配解决放
想要用eclipse-jee的话,需要jdk1.8一下版本才能用. 1.需要下载jdk1.7 2.把jdk1.7安装(不需要设置环境变量). 3.在项目上右击选择properties 4.选择Java ...
- c# 虚方法(virtual)与 多态(Polymorphism)
using System; using System.Collections.Generic; using System.Linq; using System.Text; //虚方法(virtual) ...
- Linux Shell编程 test命令
概述 test 命令是Shell 脚本中用来进行条件判断的. test命令示例 按照文件类型进行判断 测试选项 作 用 -b 文件 判断该文件是否存在,并且是否为块设备文件(是块设备文件为真) -c ...
- HTML学习笔记(上)
1. HTML介绍 1.1 什么是HTML HyperText Markup Language,超文本标记语言.简单来说,HTML文件本质上就是一个文本文件,但是这个文本文件是带有标签的. 不同的标签 ...
- CSS3动画库animate.css
在线演示 本地下载
- myisam表修复
数据库myisam引擎表损坏修复步骤: 1.进入到表目录文件下 # myisamchk -of comments.MYI 2. # myisamchk -r comments.MYI 3. # ...
- FreeBSD 安装过程
FreeBSD安装步骤: 回车 按默认回车 输入服务器的计算机名 去掉games,加上src安装如下图 Lib32 ports src这三项一定要安装上 回车 选择Manual 进入以后点create ...
- centos7下安装tomcat7
1 安装说明安装环境:CentOS-7.0.1611安装方式:源码安装软件:apache-tomcat-7.0.75.tar.gz 下载地址:http://tomcat.apache.org/down ...