zoukankan      html  css  js  c++  java
  • springcloud~Eureka实例搭建

    服务端

    build.gradle配置

    dependencies {
    	compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-server')
    	testCompile('org.springframework.boot:spring-boot-starter-test')
    }
    
    dependencyManagement {
    	imports {
    		mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    	}
    }
    

    bootstrap.yml相关配置

    server.port: 8761
    management.port: 8762
    
    eureka:
      instance:
        hostname: localhost
      client:
        registerWithEureka: false
        fetchRegistry: false
        serviceUrl:
          defaultZone: http://localhost:8761/eureka/
    

    启动代码

    package lind.sprindemo.eurekaServer;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    
    @EnableEurekaServer
    @EnableDiscoveryClient
    @SpringBootApplication
    public class EurekaServerApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(EurekaServerApplication.class, args);
    	}
    }
    

    客户端

    向我们配置中心的客户端同时也是eureka的一个客户端,例如一个订单服务,它的配置存储在配置中心,它如果希望公开自己,就需要是eureka的一个客户端。

    例子

    graph TD A[订单服务]-->B(注册) B-->C(eureka) A-->D(获取配置) D-->E(clientserver)

    高可用的eureka集群

    主要是在eureka里注册另一个eureka,两个eureka实例相互注册,实现一个最高用的集群。

    同一时刻,只有一个eureka里有服务,而当这个eureka挂了之后,会把服务自动同步到另一个节点上,这就是高可用。

    1. 配置代码
    server:
      port: ${PORT:8761}
    management:
      port: ${BG_PORT:8762}
    application:
      name: ${NAME:eurekaserver}
    spring:
      profiles:
        active: dev
    ---
    eureka:
      profile: dev
      instance:
        hostname: ${application.name}
        perferIpAddress: true #基于IP地址注册
      client:
        registerWithEureka: false #false表示不向注册中心注册自己。
        fetchRegistry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
        serviceUrl:
          defaultZone: ${URL:http://${eureka.instance.hostname}:${server.port}/eureka/}
    
    
    1. Dockerfile内容

    可以从远程下载你的jar包,如果你的网速足够好的话

    FROM fabric8/java-jboss-openjdk8-jdk:1.4
    ENV JAVA_APP_JAR="app.jar"
    ENV JAVA_MAX_MEM_RATIO=40
    ENV AB_OFF ""
    ADD  --chown=jboss:jboss ./jar/eurekaServer-0.0.2.jar /deployments/app.jar
    
    1. docker-compose实现eureka集群
    version: "3.3"
    services:
    
      eurekaserver1:
        build: ./eureka-server
        restart: on-failure
        ports:
          - "6761:6761"
          - "6762:6762"
        networks:
          - dev
        environment:
          - PORT=6761
          - BG_PORT=6762
          - NAME=eureka1
          - URL=http://eureka2:6761/eureka #集群地址配置
    
      eurekaserver2:
        build: ./eureka-server
        restart: on-failure
        ports:
          - "5761:5761"
          - "5762:5762"
        networks:
          - dev
        environment:
          - PORT=5761
          - BG_PORT=5762
          - NAME=eureka2
          - URL=http://eureka1:6761/eureka #集群地址配置
    

    运行docker-compose up -d 就会先构建eureka的镜像,然后运行你的两个eureka实例了。

  • 相关阅读:
    辅助方法
    扩展方法学习发展之路
    对IOC模式与容器Autofac在MVC里面的一些用法
    Qlikview Session Recovery
    SNOY VAIO 索尼电脑亮度自动变暗
    EXCEL2010数据挖掘插件 下载地址
    Ubuntu20.04 源码安装完应用 如何将应用程序添加到启动器
    flask + flask_restplus+ blueprints 脚手架
    闭包
    操作方法
  • 原文地址:https://www.cnblogs.com/lori/p/9205054.html
Copyright © 2011-2022 走看看