分布式系统中,当B的数量越来越多的时候,A只需要从注册中心获取B注册的服务,而不需要直接从B中获取服务,答案显而易见。
application.yml:
eureka:
client:
service-url:
defaultZone: http://114.55.229.69:8761/eureka
register-with-eureka: false #自身不在eureka上注册
server:
enable-self-preservation: false #关闭自我保护(客户端跟服务端长时间连不上,eureka控制台上有红色警告,开发环境设置成false,线上为true)
instance:
hostname: localhost #跳转地址
spring:
application:
name: eureka-server
server:
port: 8761
EurekaApplication:
package com.wangfajun;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer //Eureka服务端
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
Pom.xml:
<?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.wangfajun</groupId> <artifactId>fajun-server-eureka</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>fajun-eureka-server</name> <description>Eureka Server</description> <parent> <groupId>com.wangfajun</groupId> <artifactId>fajun-store</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <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> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Tip:Eureka服务端高可用,生产上建议至少两台以上,相互注册即可
服务端:
机器:114.55.229.69,端口:8761机器
defaultZone: http://118.178.144.64:8762/eureka,http://114.55.88.126:8763/eureka
机器:118.178.144.64,端口:8762机器
defaultZone: http://114.55.229.69:8761/eureka,http://114.55.88.126:8763/eureka
机器:114.55.88.126,端口:8763机器
defaultZone: http://114.55.229.69:8761/eureka,http://118.178.144.64:8762/eureka
客户端:
http://114.55.229.69:8761/eureka,http://118.178.144.64:8762/eureka,http://114.55.88.126:8763/eureka