概述
编写Eureka Server集群:
eureka-server总结
编写Eureka Server集群:
1.在POM文件中引入依赖:
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2. 配置application.yml
---
spring:
profiles: peer1
server:
port: 8761
eureka:
instance:
hostname: peer1
client:
service-url:
defaultZone: http://peer2:8762/eureka/ # 注意: peer1要连接peer2的eureka地址
server:
peer-node-read-timeout-ms: 2000
---
spring:
profiles: peer2
server:
port: 8762
eureka:
instance:
hostname: peer2
client:
service-url:
defaultZone: http://peer1:8761/eureka/ # 注意: peer2要连接peer1的eureka地址
server:
peer-node-read-timeout-ms: 2000
3. 在EurekaServerApplication上增加@EnableEurekaServer开启 Eureka Server功能。
4. 在C:WindowsSystem32driversetchosts增加如下设置:
127.0.0.1 peer1
127.0.0.1 peer2
5. 在eureka-server项目中运行,mvn clean package命令打包此项目。
6. cmd进入eureka-server/target目录,运行 java -jar eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1 命令,
在命令行中看到:Started Eureka Server 意味着eureka-server启动成功。
7. cmd进入eureka-server/target目录,运行 java -jar eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2 命令,
在命令行中看到:Started Eureka Server 意味着eureka-server启动成功。
8. 浏览器访问http://peer1:8761/ 在DS Replicas 可以看到 peer2。
9. 浏览器访问 http://peer2:8762/ 在DS Replicas 可以看到 peer1。 代表Eureka server集群搭建成功。
10. 启动eureka-client服务,http://peer1:8761/ 或 http://peer2:8762/
主页上的Instances currently registered with Eureka 选项中都己经有一个实例被注册, Application 为EUREKA-CLIENT, Staus 为UPC 在线) , 端口为8763 。
eureka-client总结
编写Eureka Client:
1.在POM文件中引入依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2. 配置application.yml
spring:
application:
name: eureka-client
server:
port: 8763
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
3. 在EurekaClientApplication上增加@EnableEurekaClient开启 Eureka Client的功能。
在日志中看到: DiscoveryClient_EUREKA-CLIENT/localhost:eureka-client:8762 - registration status: 204 代表服务注册成功。
4. 新建一个HiController。
5. 先启动eureka-server集群。启动步骤见 eureka-server/总结.txt 的 步骤4-7。
6. 启动eureka-client服务后,在浏览器上访问Eureka Server主界面: http://peer1:8761/
在主页上的Instances currently registered with Eureka 选项中己经有一个实例被注册, Application 为EUREKA-CLIENT, Staus 为UPC 在线) , 端口为8763 。
7.在浏览器上访问http://localhost:8763/hi?name=forezp, 浏览器显示如下信息: hi forezp,i am from port:8763
总的pom文件
<?xml version="1.0" encoding="UTF-8"?>
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.0.RELEASE
com.forezp
chapter5-5
0.0.1-SNAPSHOT
chapter5-5
pom
Demo project for Spring Boot
<modules>
<module>eureka-client</module>
<module>eureka-server</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
</properties>
<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>
eureka-server的pom和配置application.yml
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
4.0.0
com.forezp
chapter5-5
0.0.1-SNAPSHOT
com.forezp
eureka-server
0.0.1-SNAPSHOT
eureka-server
<description>Demo project for Spring Boot</description>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
application.yml:
#定义了两个profile 文件,分别为peerl 和peer2 ,它们的hostname 分别为peerl
#和peer2 。在实际开发中,可能是具体的服务器F 地址,它们的端口分别为8761 和8762
#因为是在本地搭建Eureka Server 集群,所以需要修改本地的host文件,windows系统的电脑
#在C:WindowsSystem32driversetchosts 中修改
#127.0.0.1 peer1
#127.0.0.1 peer2
spring:
profiles: peer1
server:
port: 8761
eureka:
instance:
hostname: peer1
client:
service-url:
defaultZone: http://peer2:8762/eureka/ # 注意: peer1要连接peer2的eureka地址
server:
peer-node-read-timeout-ms: 2000
spring:
profiles: peer2
server:
port: 8762
eureka:
instance:
hostname: peer2
client:
service-url:
defaultZone: http://peer1:8761/eureka/ # 注意: peer2要连接peer1的eureka地址
server:
peer-node-read-timeout-ms: 2000
eureka-client的pom和配置application.yml
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
4.0.0
com.forezp
chapter5-5
0.0.1-SNAPSHOT
com.forezp
eureka-client
0.0.1-SNAPSHOT
eureka-client
Demo project for Spring Boot
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
application.yml:
spring:
application:
name: eureka-client
server:
port: 8763
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
最后
以上就是震动橘子为你收集整理的Eureka(eureka)服务集群搭建搭建的全部内容,希望文章能够帮你解决Eureka(eureka)服务集群搭建搭建所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复