二、spring cloud 注册与发现eureka注册中心
基于2.0 Greenwich高可用eureka注册中心搭建
一、单机版
新建MAVEN父工程demo-parent
删掉src
pom.xml
<packaging>pom</packaging> <!--spring boot ⽗启动器依赖--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> </parent> <dependencyManagement> <dependencies> <!--spring cloud依赖管理,引入了Spring Cloud的版本--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!--web依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--⽇志依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </dependency> <!--测试依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--lombok⼯具--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.4</version> <scope>provided</scope> </dependency> <!-- Actuator可以帮助你监控和管理Spring Boot应⽤--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--热部署--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <!--引⼊Jaxb,开始--> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-core</artifactId> <version>2.2.11</version> </dependency> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>2.2.11</version> </dependency> <dependency> <groupId>org.glassfish.jaxb</groupId> <artifactId>jaxb-runtime</artifactId> <version>2.2.10-b140310.1920</version> </dependency> <dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> <version>1.1.1</version> </dependency> <!--引⼊Jaxb,结束--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-commons</artifactId> </dependency> </dependencies> <build> <plugins> <!--编译插件--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>11</source> <target>11</target> <encoding>utf-8</encoding> </configuration> </plugin> <!--打包插件--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
单例版eureka注册中心,新建model demo-eureka-server-8671
pom.xml
<dependencies> <!--注册中心--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies>
启动类 添加 @EnableEurekaServer 表示开启Eureka注册中心
package com.wg.edu; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; /** * @author Mrwg * @date 2020/6/20 21:49 * @desc */ @SpringBootApplication @EnableEurekaServer public class CloudEurekaServerApplication8761 { public static void main(String[] args) { SpringApplication.run(CloudEurekaServerApplication8761.class,args); } }
application.yml
#eureka server服务端口 server: port: 8761 spring: application: name: cloud-eureka-server # 应用名称,应用名称会在Eureka中作为服务名称 # eureka 客户端配置(和Server交互),Eureka Server 其实也是一个Client eureka: instance: hostname: localhost # 当前eureka实例的主机名 prefer-ip-address: true instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port} client: service-url: # 配置客户端所交互的Eureka Server的地址(Eureka Server集群中每一个Server其实相对于其它Server来说都是Client) # 集群模式下,defaultZone应该指向其它Eureka Server,如果有更多其它Server实例,逗号拼接即可 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ register-with-eureka: false # 集群模式下可以改成true fetch-registry: false # 集群模式下可以改成true
验证
-
执⾏启动类CloudEurekaServerApplication的main函数
-
访问http://127.0.0.1:8761,如果看到如下⻚⾯(Eureka注册中⼼后台),则表明EurekaServer 发布成功
二、高可用注册中心搭建
在现有单机版注册中心上搭建
1、拷贝一份demo-eureka-server-8671 为 demo-eureka-server-8762
2、配置host
127.0.0.1 CloudEurekaServerA
127.0.0.1 CloudEurekaServerB
-
修改项目名称
-
pom.xml项目名称,端口
-
.iml名称
导入IDEA,检查修改内容
1、修改demo-eureka-server-8671 中的pom.xml
-
eureka. instance.hostname= CloudEurekaServerA # 当前eureka实例的主机名
-
eureka.client.service-url.defaultZone=http://CloudEurekaServerB:8762/eureka
-
eureka.client.register-with-eureka= true # 单机版为false
-
eureka.client.fetch-registry= true # 单机版为false
#eureka server服务端口 server: port: 8761 spring: application: name: cloud-eureka-server # 应用名称,应用名称会在Eureka中作为服务名称 # eureka 客户端配置(和Server交互),Eureka Server 其实也是一个Client eureka: instance: hostname: CloudEurekaServerA # 当前eureka实例的主机名 prefer-ip-address: true instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port} client: service-url: # 配置客户端所交互的Eureka Server的地址(Eureka Server集群中每一个Server其实相对于其它Server来说都是Client) # 集群模式下,defaultZone应该指向其它Eureka Server,如果有更多其它Server实例,逗号拼接即可 defaultZone: http://CloudEurekaServerB:8762/eureka register-with-eureka: true # 集群模式下可以改成true fetch-registry: true # 集群模式下可以改成true
1、修改demo-eureka-server-8762 中的pom.xml
-
eureka. instance.hostname= CloudEurekaServerB # 当前eureka实例的主机名
-
eureka.client.service-url.defaultZone=http://CloudEurekaServerA:8761/eureka
-
eureka.client.register-with-eureka= true # 单机版为false
-
eureka.client.fetch-registry= true # 单机版为false
#eureka server服务端口 server: port: 8762 spring: application: name: cloud-eureka-server # 应用名称,应用名称会在Eureka中作为服务名称 # eureka 客户端配置(和Server交互),Eureka Server 其实也是一个Client eureka: instance: hostname: CloudEurekaServerB # 当前eureka实例的主机名 prefer-ip-address: true instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port} client: service-url: # 配置客户端所交互的Eureka Server的地址 defaultZone: http://CloudEurekaServerA:8761/eureka register-with-eureka: true fetch-registry: true
验证,启动demo-eureka-server-8671、demo-eureka-server-8672
访问、cloudeurekaservera:8761 、cloudeurekaservera:8762