上一篇
SpringCloud注册中心服务种类比较多,有Eureka、Zookeeper、Consul、Cloud Foundry等,今天用的工具是Eureka。注册中心的概念不必多讲,直接上干货!
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.3.RELEASEversion>
<relativePath/>
parent>
<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">
<parent>
<artifactId>spring-cloudartifactId>
<groupId>com.spring.cloudgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>eureka-serverartifactId>
<name>eureka-servername>
<description>注册中心description>
<packaging>jarpackaging>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
<spring-cloud.version>Greenwich.RELEASEspring-cloud.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
<exclusions>
<exclusion>
<groupId>org.mockitogroupId>
<artifactId>mockito-coreartifactId>
exclusion>
exclusions>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${spring-cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
<repositories>
<repository>
<id>spring-milestonesid>
<name>Spring Milestonesname>
<url>https://repo.spring.io/milestoneurl>
repository>
repositories>
project>
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
spring.application.name=spring-cloud-eureka-server
server.port = 8001
eureka.instance.hostname = localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone = \
http://${eureka.instance.hostname}:${server.port}/eureka/
注意:
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
是取消Eureka-Server自身注册发现机制,默认为true,如不设置为false,启动就会报错,但是不会挂掉,可以正常使用。
7. 运行EurekaServerApplication.java
访问:http://localhost:8001 ,如下图所示,说明启动成功
例子:三个注册中心
复制 application.properties
文件三份,分别命名为
application-server1.properties
spring.application.name=spring-cloud-eureka-server
server.port = 8001
eureka.instance.hostname = localhost
eureka.client.serviceUrl.defaultZone = \
http://localhost:8002/eureka/, http://localhost:8003/eureka/
application-server2.properties
spring.application.name=spring-cloud-eureka-server
server.port = 8002
eureka.instance.hostname = localhost
eureka.client.serviceUrl.defaultZone = \
http://localhost:8001/eureka/, http://localhost:8003/eureka/
application-server3.properties
spring.application.name=spring-cloud-eureka-server
server.port = 8003
eureka.instance.hostname = localhost
eureka.client.serviceUrl.defaultZone = \
http://localhost:8001/eureka/, http://localhost:8002/eureka/
然后清空application.properties
的内容,打成jar包
在win10 cmd窗口找到jar包位置,开启三个cmd窗口,启动三个jar包,访问http://localhost:8001,http://localhost:8002,http://localhost:8001都可以看到,注册中心集群已启动
java -jar eureka-server.jar --spring.profiles.active=service1
java -jar eureka-server.jar --spring.profiles.active=service2
java -jar eureka-server.jar --spring.profiles.active=service3
下一篇