一、注冊中心編碼
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.0</modelVersion> <groupId>com.eureka</groupId> <artifactId>ser</artifactId> <version>0.0.1-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-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</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: 8888 eureka: instance: hostname: centos7-01 client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://centos7-02:8888/eureka/,http://centos7-03:8888/eureka/
#樣例2:
server: port: 8888 eureka: instance: hostname: centos7-02 client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://centos7-01:8888/eureka/,http://centos7-03:8888/eureka/
#樣例3:
server: port: 8888 eureka: instance: hostname: centos7-03 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: 80 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.訪問客戶端 (客戶端是在本地啟動的)