eureka搭建指南

2020-03-31  本文已影响0人  三分归元币

高可用注册中心

相比于zk,eureka关注于高可用性。所以,eureka在一致性上不太强制,可以说是注册中心最优秀的选择了。
强一致性的系统就像是,你向集群写入一条数据,集群需要完全同步或者同步半数以上,才给你返回个ack。
高可用性提现在于,服务注册之后注册的节点数据同步完成了,但是并未完全同步节点即可完成注册;而且每个注册节点是相互独立的,不存在选举可以跨机房部署。


eureka.png

server端搭建

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.myregister</groupId>
    <artifactId>myeurekaserver</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>myeurekaserver</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR3</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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </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>
server.port=8080
spring.application.name=eureka-server

#代表不向注册中心注册自己
eureka.client.register-with-eureka=false
#不需要去检索服务信息
eureka.client.fetch-registry=false

package com.myregister.myeurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class MyeurekaserverApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyeurekaserverApplication.class, args);
    }

}

client端搭建

pom文件同上

server.port=8081
spring.application.name=eureka-client

eureka.instance.appname=eureka-client
eureka.instance.hostname=localhost
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:8080/eureka
eureka.instance.app-group-name=DEFAULT
package com.midware;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

启动后校验

输入 http://localhost:8080/

image.png

使用REST接口检查服务信息

http://localhost:8080/eureka/apps

image.png
上一篇下一篇

猜你喜欢

热点阅读