微服務:整合 Spring Cloud Eureka - 服務注冊 Eureka Client


目錄

   微服務:整合 Spring Cloud Eureka - 注冊中心 Eureka Server 

   微服務:整合 Spring Cloud Eureka - 服務注冊 Eureka Client  

   微服務:整合 Spring Cloud Eureka - 服務發現 DiscoveryClient 

   微服務:整合 Spring Cloud Eureka - 服務消費以及Ribbon簡單使用 

   微服務:整合 Spring Cloud Eureka - 高可用集群  

   微服務:整合 Spring Cloud Eureka - .NET Core Mvc Api (C#) 

   微服務:整合 Spring Cloud Eureka - 服務治理機制  

   微服務:整合 Spring Cloud Eureka - 服務事件監聽  

   微服務:整合 Spring Cloud Eureka - 高級屬性Region、Zone

   微服務:整合 Spring Cloud Eureka - Rest接口文檔 

   微服務:整合 Spring Cloud Eureka - Security 安全保護

一、前言

在上一篇博客-《微服務:整合 Spring Cloud Eureka - 注冊中心 中,小編介紹了如何一步一步搭建微服務Eureka注冊中心,本篇博客將介紹如何將服務注冊到Eureka注冊中心。

 

二、服務提供者搭建

  在完成了服務注冊中心的搭建之后,接下來我們嘗試將一個Spring Boot應用假如Eureka的服務治理體系中。

1、項目結構圖

 

 2、父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>com.demo</groupId>
    <artifactId>spring-cloud-register</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>demo-register</module>
        <module>demo-service-provider</module>
        <module>demo-service-consumer</module>
    </modules>

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
                <version>2.2.4.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

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

3、demo-service-provider 的 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">
    <parent>
        <artifactId>spring-cloud-register</artifactId>
        <groupId>com.demo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>demo-service-provider</artifactId>

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

</project>

4、demo-service-provider的啟動類:ServiceProviderApplication

package com.demo.service.provider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

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

 

5、ProviderHelloController

package com.demo.service.provider.controller;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
public class ProviderHelloController {

    @RequestMapping("/sayhello/{name}")
    public String sayHello(@PathVariable("name")String name){
        return "hello ," + name;
    }
}

 

6、配置 application.yml

server:
  port: 8101

spring:
  application:
    name: demo-service-provider

eureka:
  instance:
    lease-renewal-interval-in-seconds: 20
  client:
    register-with-eureka: true
    fetch-registry: true
    instance-info-replication-interval-seconds: 30
    registry-fetch-interval-seconds: 10
    serviceUrl:
      defaultZone: http://localhost:8001/register/eureka/

上述配置指定了應用端口號為8101,服務名稱為demo-service-provider(別的微服務可以通過這個名稱從注冊中心獲取demo-service-provider提供的服務),剩下的為Eureka相關配置,含義如下:

  1、eureka.instance.lease-renewal-interval-in-seconds,向Eureka 服務端發送心跳的間隔時間,單位為秒,用於服務續約。這里配置為20秒,即每隔20秒向febs-register發送心跳,表明當前服務沒有宕機;
  2、eureka.client.register-with-eureka, 為true時表示將當前服務注冊到Eureak服務端;
  3、eureka.client.fetch-registry,為true時表示從Eureka 服務端獲取注冊的服務信息;
  4、eureka.client.instance-info-replication-interval-seconds,新實例信息的變化到Eureka服務端的間隔時間,單位為秒;
  5、eureka.client.registry-fetch-interval-seconds,默認值為30秒,即每30秒去Eureka服務端上獲取服務並緩存,這里指定為10秒的原因是方便開發時測試,實際可以指定為默認值即可;
  6、eureka.client.serviceUrl.defaultZone,指定Eureka服務端地址。

 

三、運行效果

1、先啟動Eureka服務注冊中心 - demo-register,具體配置可參考微服務中間件:注冊中心 - Spring Cloud Eureka 。

 

 因為現在還沒有啟動服務提供者 - demo-service-provider,所以 Eureka服務注冊中心還沒有任何服務實例。

2、啟動服務提供者 - demo-service-provider

     啟動成功之后,打開瀏覽器,輸入地址:http://localhost:8101/hello/sayhello/jack

 

 此時我們在打開Eureka注冊中心:http://localhost:8001/register/

 

 我們可以看到Eureka注冊中心已經有一個服務實例,UP (1) - localhost:demo-service-provider:8101 其中UP (1)就標識有一個服務實例在線。

 

下一篇將會介紹微服務中的服務發現與消費!

 


免責聲明!

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



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