/
<?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.imooc</groupId> <artifactId>client</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>client</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.M3</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.M2</spring-cloud.version> </properties> <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> </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> <repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> <pluginRepository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </project>
eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ # instance: # hostname: clientName spring: application: name: client
package com.imooc.client; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class ClientApplication { public static void main(String[] args) { SpringApplication.run(ClientApplication.class, args); } }
这里以三台高可用来举例
Eureka Server 1
eureka: client: service-url: defaultZone: http://localhost:8762/eureka/, http://localhost:8763/eureka/ register-with-eureka: false server: enable-self-preservation: false spring: application: name: eureka server: port: 8761
Eureka Server 2
eureka: client: service-url: defaultZone: http://localhost:8761/eureka/, http://localhost:8763/eureka/ register-with-eureka: false server: enable-self-preservation: false spring: application: name: eureka server: port: 8762
Eureka Server 3
eureka: client: service-url: defaultZone: http://localhost:8761/eureka/, http://localhost:8762/eureka/ register-with-eureka: false server: enable-self-preservation: false spring: application: name: eureka server: port: 8763
Eureka Client
eureka: client: service-url: defaultZone: http://localhost:8761/eureka/, http://localhost:8762/eureka/, http://localhost:8763/eureka/ # instance: # hostname: clientName spring: application: name: client
附
分布式系统中为啥需要服务发现,这个问题我也反复思考过。后来也在网上找到答案了。 现在A是一个,B是多节点的(也就是集群)。怎么A去找B呢,那么就是自己写匹配,一个一个写。但是由于B不仅多,还可能会动态变化,把B的IP地址写到A里面,是不好的,因为地址可能少,也可能变。 现在的话,有注册中心,会自动的注册上去。