01、EurekaServer安装

文章目录

    • 0.官方文档
  • 1.pom文件添加依赖
  • 2.使用@EnableEurekaServer注解
  • 3.配置application.yml 独立模式
  • 4.配置application.yml 对等模式
  • 5.高可用模式
  • 6.首选ip地址
  • 8.注册地址
  • 9.bug unavailable-replicas

0.官方文档

本文使用了Brixton.SR7
https://cloud.spring.io/spring-cloud-static/Brixton.SR7/
https://cloud.spring.io/spring-cloud-static/Dalston.SR5/multi/multi_spring-cloud.html

1.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>
	<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.7</java.version>
    </properties>

  <parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.3.5.RELEASE</version>
    </parent>
	<groupId>com.example</groupId>
	<artifactId>springclouddemo-eureka</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springcloud1.3demo</name>
	<description>Demo project for Spring Boot</description>
	
	 <dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Brixton.RELEASE</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	

	<dependencies>
		
		 <dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka-server</artifactId>
		</dependency>
	</dependencies>

	<build>
	    <finalName>eureka</finalName>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

2.使用@EnableEurekaServer注解

@SpringBootApplication
@EnableEurekaServer
public class Application {

    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(true).run(args);
    }

}

3.配置application.yml 独立模式

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

4.配置application.yml 对等模式

application-peer1.yml
      
spring:
  application:
    name: eureka

server:
  port: 8001

eureka:
  instance:
     appname: ${spring.application.name}
     hostname: peer1
  client:
    serviceUrl:
      defaultZone: http://peer2:8002/eureka/

application-peer2.yml

spring:
  application:
    name: eureka

server:
  port: 8002

eureka:
  instance:
    appname: ${spring.application.name}
    hostname: peer2
  client:
    serviceUrl:
      defaultZone: http://peer1:8001/eureka/

启动

java -jar eureka.jar --spring.profiles.active=peer1
java -jar eureka.jar --spring.profiles.active=peer2

5.高可用模式

application-eureka0.yml
spring:
  application:
    name: eureka

server:
  port: 8000

eureka:
  instance:
    appname: ${spring.application.name}
    hostname: eureka0
  client:
    serviceUrl:
      defaultZone: http://eureka1:8001/eureka/,http://eureka2:8002/eureka/
      
application-eureka1.yml

spring:
  application:
    name: eureka
server:
  port: 8001

eureka:
  instance:
    appname: ${spring.application.name}
    hostname: eureka1
  client:
    serviceUrl:
      defaultZone: http://eureka0:8000/eureka/,http://eureka2:8002/eureka/
      

application-eureka2.yml
spring:
  application:
    name: eureka
server:
  port: 8002

eureka:
  instance:
    appname: ${spring.application.name}
    hostname: eureka2
  client:
    serviceUrl:
      defaultZone: http://eureka0:8000/eureka/,http://eureka1:8001/eureka/

启动

java -jar eureka.jar --spring.profiles.active=eureka0
java -jar eureka.jar --spring.profiles.active=eureka1
java -jar eureka.jar --spring.profiles.active=eureka2

6.首选ip地址

Prefer IP Address

eureka:
  instance:
    prefer-ip-address: true

7、 访问;

http://localhost:8761

8.注册地址

http://localhost:8761/eureka/

9.bug unavailable-replicas

9.1原代码StatusUtil

public class StatusUtil {
   
     
    private static final Logger logger = LoggerFactory.getLogger(StatusUtil.class);

    private final String myAppName;
    private final PeerAwareInstanceRegistry registry;
    private final PeerEurekaNodes peerEurekaNodes;

    public StatusUtil(EurekaServerContext server) {
   
     
        this.myAppName = server.getApplicationInfoManager().getInfo().getAppName();
        this.registry = server.getRegistry();
        this.peerEurekaNodes = server.getPeerEurekaNodes();
    }

    public StatusInfo getStatusInfo() {
   
     
        StatusInfo.Builder builder = StatusInfo.Builder.newBuilder();
        // Add application level status
        StringBuilder upReplicas = new StringBuilder();
        StringBuilder downReplicas = new StringBuilder();

        StringBuilder replicaHostNames = new StringBuilder();

        for (PeerEurekaNode node : peerEurekaNodes.getPeerEurekaNodes()) {
   
     
            if (replicaHostNames.length() > 0) {
   
     
                replicaHostNames.append(", ");
            }
            replicaHostNames.append(node.getServiceUrl());
            if (isReplicaAvailable(myAppName, node.getServiceUrl())) {
   
     
                upReplicas.append(node.getServiceUrl()).append(',');
            } else {
   
     
                downReplicas.append(node.getServiceUrl()).append(',');
            }
        }

        builder.add("registered-replicas", replicaHostNames.toString());
        builder.add("available-replicas", upReplicas.toString());
        builder.add("unavailable-replicas", downReplicas.toString());

        return builder.build();
    }

    private boolean isReplicaAvailable(String myAppName, String url) {
   
     

        try {
   
     
            String givenHostName = new URI(url).getHost();
            Application app = registry.getApplication(myAppName, false);
            if (app == null) {
   
     
                return false;
            }
            for (InstanceInfo info : app.getInstances()) {
   
     
                if (info.getHostName().equals(givenHostName)) {
   
     
                    return true;
                }
            }
            givenHostName = new URI(url).getHost();
        } catch (Throwable e) {
   
     
            logger.error("Could not determine if the replica is available ", e);
        }
        return false;
    }
}

9.2确保三个节点的spring.application.name相同

多个节点的name应该相同
spring:
  application:
    name: eureka

9.3确保eureka.instance.hostname和eureka.client.serviceUrl.defaultZone中的一个host相同

eureka:
  instance:
     hostname: peer1

eureka:
  client:
    serviceUrl:
      defaultZone: http://peer2:8002/eureka/

版权声明:本文不是「本站」原创文章,版权归原作者所有 | 原文地址: