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:\Windows\System32\drivers\etc\hosts

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM