• <Spring Cloud>入门一 Eureka Server


    1.搭建父工程

      主要是添加版本依赖,此处版本是:

      spring-boot  : 2.0.8.RELEASE

      spring-cloud : Finchley.SR2

    <?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>
        <packaging>pom</packaging>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.8.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>org.maple</groupId>
        <artifactId>spring-cloud-learning</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>spring-cloud-learning</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
            <spring-cloud.version>Finchley.SR2</spring-cloud.version>
        </properties>
    
        <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>
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>5.0.4</version>
                </dependency>
                <dependency>
                    <groupId>com.alibaba</groupId>
                    <artifactId>druid</artifactId>
                    <version>1.0.31</version>
                </dependency>
                <dependency>
                    <groupId>org.mybatis.spring.boot</groupId>
                    <artifactId>mybatis-spring-boot-starter</artifactId>
                    <version>1.3.2</version>
                </dependency>
                <dependency>
                    <groupId>ch.qos.logback</groupId>
                    <artifactId>logback-core</artifactId>
                    <version>1.2.3</version>
                </dependency>
                <dependency>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                    <version>${junit.version}</version>
                </dependency>
                <dependency>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                    <version>${log4j.version}</version>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
    </project>

    2.搭建eureka server

      此处直接搭建集群环境,实现高可用,eureka设计符合AP原则,不同于Zookeeper的CP原则。注重可用性,而不是一直性。

    2.1 pom文件

      引入  spring-cloud-starter-netflix-eureka-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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>spring-cloud-learning</artifactId>
            <groupId>org.maple</groupId>
            <version>0.0.1-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>eureka-server-8761</artifactId>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            </dependency>
        </dependencies>
    </project>

    2.2 编写application.yml

      单机环境:defaultZone:写自己,例如 http://localhost:8761/eureka/

      集群环境:需要填写所有其他 eureka server 的地址

      在window的host文件配置了两个映射

        * 127.0.0.1 eureka-server01

        * 127.0.0.1 eureka-server02

    server:
      port: 8761
    eureka:
      client:
        # 是否需要检索服务
        fetch-registry: false
        # 取消服务器自我注册
        register-with-eureka: false
        # Eureka Server 服务URL,用于客户端注册,集群需要配置其他eureka server 的地址
        service-url:
          defaultZone: http://eureka-server02:8762/eureka/
      # 服务注册中心的主机ip
      instance:
        hostname: eureka-server01
      server:
        # 服务注册清理间隔,当一定时间没有上报,那么自动剔除服务(单位毫秒,默认60*1000)
        eviction-interval-timer-in-ms: 60000
        # 如果为true 当服务down不会剔除,依旧会保存服务信息
        # 是否开启自我保护,当服务一定时间没有上报,假设是因为网络因素,而服务并未出现问题,剔除是不可靠的
        # 开发阶段建议设置为false
        enable-self-preservation: false
    
    # eureka server 应用名称
    spring:
      application:
        name: eureka-server01

    2.3 编写spring boot 启动类

      添加 @EnableEurekaServer 标识该服务是一个 eureka server

    package org.maple;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    
    /**
     * @author mapleins
     * @Date 2019-01-12 17:03
     * @Desc
     **/
    @EnableEurekaServer
    @SpringBootApplication
    public class App_Eureka_Server_8761 {
    
        public static void main(String[] args) {
            SpringApplication.run(App_Eureka_Server_8761.class,args);
        }
    }

    2.4 浏览器访问 eureka 界面

      同时 可以看到另一台 eureka-server02,此处 eureka 服务就搭建完毕

  • 相关阅读:
    字节跳动--今日头条iOS客户端启动速度优化
    RSA加密
    几种浏览器
    Linux定时任务crontab无法执行
    Python报错ImportError: No Module Named Typing的解决
    微信小程序:A、B两个小程序相互跳转,出现点击A小程序底部导航栏菜单,第一次点击无法跳转B小程序,需要点击第二次才会触发跳转到B小程序
    c# core 生成随机图文验证码
    携程Apollo统一配置管理中心
    WPF程序中嵌入winForm窗体
    sqlserver 转 postgresql
  • 原文地址:https://www.cnblogs.com/mapleins/p/10217382.html
Copyright © 2020-2023  润新知