Mac OS中配置hosts:
>sudo vi /etc/hosts
127.0.0.1 peer1
127.0.0.1 peer2
127.0.0.1 peer3
1. pom.xml
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>net.fsd</groupId> <artifactId>sfgeurekasvr</artifactId> <version>0.0.1-SNAPSHOT</version> <name>sfgeurekasvr</name> <url>http://maven.apache.org</url> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-cloud.version>Finchley.RELEASE</spring-cloud.version> <skipTests>true</skipTests> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <!-- <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </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> </project> |
2. main
@EnableEurekaServer @SpringBootApplication @RestController public class EurekaMainApplication { @Value("${eureka.instance.hostname}") String eurekaIp; @Value("$server.port") String port; public static void main(String[] args) { SpringApplication.run(EurekaMainApplication.class, args); } @RequestMapping(value = { "/ip" }, method = RequestMethod.GET) public String findIp() { System.out.println("--------------------------ip:" + eurekaIp + "; port:" + port); return eurekaIp; } } |
3. application-peer1.yml
server: port: 8761 spring: application: name: eureka-p1 profiles: peer1 eureka: instance: hostname: peer1 client: register-with-eureka: true fetch-registry: true serviceUrl: defaultZone: http://peer2:8762/eureka/,http://peer3:8763/eureka/ |
4. application-peer2.yml
server: port: 8762 spring: application: name: eureka-p2 profiles: peer2 eureka: instance: hostname: peer2 client: register-with-eureka: true fetch-registry: true serviceUrl: defaultZone: http://peer1:8761/eureka/,http://peer3:8763/eureka/ |
5. application-peer3.yml
server: port: 8763 spring: application: name: eureka-p3 profiles: peer3 eureka: instance: hostname: peer3 client: register-with-eureka: true fetch-registry: true serviceUrl: defaultZone: http://peer2:8762/eureka/,http://peer1:8761/eureka/ |
6. project
![](https://i-blog.csdnimg.cn/blog_migrate/2f068e6a76ae7449f9bd8cf94a737777.png)
7. pic
![](https://i-blog.csdnimg.cn/blog_migrate/f33d94609ff20d06819e71d0fe65d1d8.png)