Eureka服務的註冊與發現

1、Eureka介紹

Eureka架構中的三個核心角色:

  • 服務註冊中心
    Eureka的服務端應用,提供服務註冊和發現功能(eureka-server)
  • 服務提供者
    提供服務的應用,可以是SpringBoot應用,也可以是其它任意技術實現,只要對外提供的是Rest風格服務即可。(user-service)
  • 服務消費者
    消費應用從註冊中心獲取服務列表,從而得知每個服務方的信息,知道去哪裏調用服務方。(user-client)

在這裏插入圖片描述

2、基礎環境搭建

創建一個maven工程

工程結構

在這裏插入圖片描述

2.1 eureka server 模塊 Eureka註冊中心

引入的依賴
在這裏插入圖片描述
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.boss</groupId>
    <artifactId>eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-server</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2.2 user-service (服務提供者)引入的依賴

在這裏插入圖片描述

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.boss</groupId>
    <artifactId>user-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>user-service</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <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-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2.3 user-client (服務消費者)引入的依賴

在這裏插入圖片描述
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.boss</groupId>
    <artifactId>user-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>user-client</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <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-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

3、配置註冊中心

3.1 聲明這個應用是一個EurekaServer(註冊中心)

在這裏插入圖片描述

3.2 編寫配置文件

application.yml

server:
  port: 8761                                  # 端口
spring:
  application:
    name: eureka-server                       # 應用名稱,會在Eureka中顯示
  freemarker:
    prefer-file-system-access: false
eureka:
  client:
    register-with-eureka: false               # 是否註冊自己的信息到EurekaServer,默認是true
    fetch-registry: false                     # 是否拉取其它服務的信息,默認是true
    service-url:                              # EurekaServer的地址,現在是自己的地址,如果是集羣,需要加上其它Server的地址。
      defaultZone: http://127.0.0.1:${server.port}/eureka

3.3 啓動Eureka Server

訪問:
http://localhost:8761
在這裏插入圖片描述

4、服務提供者

user-service 對外提供接口服務,可以通過訪問http://localhost:8088/hello 來獲取用戶數據

在服務上添加Eureka的客戶端依賴,客戶端代碼會自動把服務註冊到Eureka Server中。

4.1 啓動類上開啓Eureka客戶端功能

通過添加@EnableDiscoveryClient來開啓Eureka客戶端功能

在這裏插入圖片描述

4.2 配置

application.yml

server:
  port: 8088
spring:
  application:
    name: user-service              # 應用名稱
eureka:
  client:
    service-url:                    # EurekaServer地址
      defaultZone: http://127.0.0.1:8761/eureka
  instance:
    prefer-ip-address: true         # 當其它服務獲取地址時提供ip而不是hostname
    ip-address: 127.0.0.1           # 指定自己的ip信息,不指定的話會自己尋找

4.3 編寫代碼

在這裏插入圖片描述

4.4 測試

訪問實際接口
在這裏插入圖片描述
查看註冊中心,可以發現我們的服務已經註冊上去了
在這裏插入圖片描述

5、服務消費者

user-client 通過註冊中心獲取數據接口,處理後返回給用戶

在服務上添加Eureka的客戶端依賴,客戶端代碼會自動把服務註冊到Eureka Server中。

5.1 啓動類上開啓Eureka客戶端功能

在這裏插入圖片描述

5.2 配置

application.yml

server:
  port: 8089
spring:
  application:
    name: user-client              # 應用名稱
eureka:
  client:
    service-url:                    # EurekaServer地址
      defaultZone: http://127.0.0.1:8761/eureka
  instance:
    prefer-ip-address: true         # 當其它服務獲取地址時提供ip而不是hostname
    ip-address: 127.0.0.1           # 指定自己的ip信息,不指定的話會自己尋找

5.3 編寫接口訪問代碼

package com.boss.userclient.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author LGX_TvT
 * @date 2019-12-03 16:24
 */
@RestController
public class HelloController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;// Eureka客戶端,可以獲取到服務實例信息

    @RequestMapping("/hello")
    public String doClientHello(){
    	// 根據服務名稱,獲取服務實例,因爲只有一個UserService,因此我們直接get(0)獲取
        ServiceInstance serviceInstance = discoveryClient.getInstances("user-service").get(0);
        // 獲取ip和端口信息,拼接url
        String baseURL = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/hello";
        String msg = restTemplate.getForObject(baseURL, String.class);
        return msg + "!!!";
    }
}

5.4 測試

啓動服務消費方,查看註冊中心
可以看到客戶端已經註冊成功
在這裏插入圖片描述
訪問鏈接
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章