• SpringCloud学习笔记:服务注册与发现Eureka(2)


    1. Eureka简介

      Eureka是一个用于服务注册和发现的组件,分为Eureka Server和Eureka Client,Eureka Server为Eureka服务注册中心,Eureka Client为Eureka客户端。

      Eureka是SpringCloud首选推荐的注册与服务发现组件,与SpringCloud其他组件可以无缝对接。

      Eureka的基本框架主要包括3种角色:

      ◊ Register Service:服务注册中心,是一个Eureka Server,提供服务注册与发现功能;

      ◊ Provider Service:服务提供者,是一个Eureka Client,提供服务;

      ◊ Consumer Service:服务消费者,是一个Eureka Client,消费服务。

      服务消费基本过程:

      (1)首先需要一个服务注册中心Eureka Server;

      (2)服务提供者Eureka Client向服务注册中心Eureka Server注册,将自己的信息(服务名、服务IP地址)通过REST API的形式提交注册周年更新Eureka Server;

      (3)服务消费者Eureka Client向服务注册中心Eureka Server注册,服务消费者获取服务注册列表信息。该列表信息包含所有向服务注册中心Eureka Server注册的服务信息;

      (4)服务消费者Eureka Client获取服务注册列表信息之后,获得服务提供者的IP地址,通过HTTP远程调度来消费服务提供者的服务。

    2. Eureka示例项目

      项目结构:采用Maven多Module结构

      

      主Maven项目pom.xml:

    <?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>
    
        <groupId>libing</groupId>
        <artifactId>libing-eureka</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>pom</packaging>
    
        <name>libing-eureka</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.1.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
            <spring-cloud.version>Finchley.M9</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>
            </dependencies>
        </dependencyManagement>
    
        <modules>
            <module>eureka-server</module>
            <module>eureka-client</module>
        </modules>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <optional>true</optional>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <fork>true</fork>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
        <repositories>
            <repository>
                <id>spring-milestones</id>
                <name>Spring Milestones</name>
                <url>https://repo.spring.io/milestone</url>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
            </repository>
        </repositories>
    
    </project>
    pom.xml

    2.1 Eureka Server

      Eureka Server依赖项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">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>priv.libing</groupId>
        <artifactId>eureka-server</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>eureka-server</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>libing</groupId>
            <artifactId>libing-eureka</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </parent>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            </dependency>
        </dependencies>
    
    </project>
    pom.xml

      application.yml:

    server:
      port: 8761
    eureka:
      instance:
        hostname: localhost
      client:
        fetch-registry: false            # 禁用向自己注册
        register-with-eureka: false      # 只维护实例,不去检索服务
        service-url:
          default-zone: http://${eureka.instance.hostname}:${server.port}/eureka/

      注:默认Eureka Server会向自己注册

      EurekaServerApplication.java:

    package priv.libing.eurekaserver;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    
    @EnableEurekaServer
    @SpringBootApplication
    public class EurekaServerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(EurekaServerApplication.class, args);
        }
    
    }

      其中:注解@EnableEurekaServer开启Eureka Server功能。

      启动程序,在浏览器中访问Eureka主界面 http://localhost:8761/

    2.2 Eureka Client

      Eureka Client引用项:

        ◊ spring-cloud-starter-netflix-eureka-client

        ◊ spring-boot-starter-web

    <?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>
    
        <groupId>priv.libing</groupId>
        <artifactId>eureka-client</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>eureka-client</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>libing</groupId>
            <artifactId>libing-eureka</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </parent>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>
    
    </project>
    pom.xml

      application.yml:

    eureka:
      client:
        service-url:
          default-zone: http://locahost:8761/eureka/
    server:
      port: 8762
    spring:
      application:
        name: eureka-client

      EurekaClientApplication.java:

    package priv.libing.eurekaclient;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    
    @EnableEurekaClient
    @SpringBootApplication
    public class EurekaClientApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(EurekaClientApplication.class, args);
        }
        
    }

      其中,注解@EnableEurekaClient开启Eureka Client功能。

      启动运行eureka-server及eureka-client:

      以上示例代码:libing-eureka.zip

    3. Eureka Server集群

      在实际项目中,微服务实例较多,需要对Eureka Server进行高可用集群。

      本地搭建Eureka Server集群,修改C:WindowsSystem32driversetchosts

    127.0.0.1       slave1
    127.0.0.1       slave2

      eureka-server调整结构:

      

      application.yml:

    spring:
      application:
        name: eureka-server

      application-slave1.yml:

    server:
      port: 8761
    eureka:
      instance:
        hostname: slave1
      client:
        service-url:
          default-zone: http://slave2:8762/eureka/

      application-slave2.yml:

    server:
      port: 8762
    eureka:
      instance:
        hostname: slave2
      client:
        service-url:
          default-zone: http://slave1:8761/eureka/

      eureka-client项目application.yml:

    eureka:
      client:
        service-url:
          default-zone: http://slave1:8761/eureka/, http://slave2:8762/eureka/
    server:
      port: 8763
    spring:
      application:
        name: eureka-client

      (1)eureka-server运行:mvn:package

    java -jar F:eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=slave1
    java -jar F:eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=slave2

      (2)eureka-server运行:IDEA配置一个项目多实例

      

      Single instance only:取消勾选

      Active profiles:slave1或slave2,分别配置,运行两个实例。

      以上示例代码:libing-eureka-slave.zip

  • 相关阅读:
    高德地图iOS SDK限制地图的缩放比例
    c++如何遍历删除map/vector里面的元素
    cocos2d-x与UIKit混合编程实现半透明效果
    cocos2d-x中的坑
    [redis]-redis查看key的大小(方法一在centos亲测通过)
    openssl 从cer文件中提取公钥
    SSL证书去除rsa私钥密码保护(.pem)
    RHEL8 CentOS8 下安装 MySQL 8.0<亲测>
    centos7+nginx使用Certbot让网站拥有https
    idea将javaweb项目部署到tomcat
  • 原文地址:https://www.cnblogs.com/libingql/p/8830256.html
Copyright © 2020-2023  润新知