zoukankan      html  css  js  c++  java
  • SpringCloud

    SpringCloud 的服务注册和发现是由Eureka来完成。

    1.eureka server

    1.1 依赖

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

     1.2 @EnableEurekaServer 注册服务

    @SpringBootApplication
    @EnableEurekaServer //开启服务注册中心
    public class EurekaServerApplication
    {
    
        public static void main(String[] args)
        {
            SpringApplication.run(EurekaServerApplication.class, args);
        }
    }

    1.3. 配置

    server:
      port: 8761
    eureka:
      instance:
        hostname: localhost
      client:
        # 表示本应用是否是向注册中心注册自己
        register-with-eureka: false
        # 是否检索服务
        fetch-registry: false

    2. eureka client

    2.1 依赖

    <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>

    2.2  @EnableDiscoveryClient

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

    2.3 配置

    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka/
    # 客户端名称 instance: hostname: client-1 spring: application:
    # 注册到服务端名称 name: eureka
    -client
    # 客户端端口 server: port:
    8081

     3.高可用

    3.1 服务端配置(配置3个服务端)

    server1

    #服务名
    spring.application.name=eureka-server
    
    #注册中心-01 端口
    server.port=1001
    
    #注册中心主机名
    eureka.instance.hostname=server1
    
    #指定另外的注册中心
    eureka.client.serviceUrl.defaultZone=http://server2:1002/eureka/,http://server3:1003/eureka/

    server2

    #服务名
    spring.application.name=eureka-server
    
    #注册中心-02 端口
    server.port=1002
    
    #注册中心主机名
    eureka.instance.hostname=server2
    
    #指定另外的注册中心
    eureka.client.serviceUrl.defaultZone=http://server1:1001/eureka/,http://server3:1003/eureka/

    server3

    #服务名
    spring.application.name=eureka-server
    
    #注册中心-03 端口
    server.port=1003
    
    #注册中心主机名
    eureka.instance.hostname=server3
    
    #指定另外的注册中心
    eureka.client.serviceUrl.defaultZone=http://server1:1001/eureka/,http://server2:1002/eureka/

    3.2 客户端配置

    #设置服务名
    spring.application.name=eureka-client
    
    #指定服务注册中心地址
    eureka.client.service-url.defaultZone=http://server1:1001/eureka/,http://server2:1002/eureka/,http://server3:1003/eureka/

    4.启动时配置端口

    java -jar eureka-service-1.0-SNAPSHOT.jar –server.port=8080 
  • 相关阅读:
    MySQL视图更新
    JavaScript经典作用域问题
    进程间通信的几种方式
    Vue(MVVM)、React(MVVM)、Angular(MVC)对比
    CDN(Content Delivery Network)技术原理概要
    单点登录实现原理(SSO)
    composer 实现自动加载原理
    PHP 反射的简单使用
    php7安装php-redis扩展
    Git 简单入门(二)
  • 原文地址:https://www.cnblogs.com/appleat/p/9983800.html
Copyright © 2011-2022 走看看