Micronaut 服務(wù)發(fā)現(xiàn)

2023-03-08 17:48 更新

服務(wù)發(fā)現(xiàn)使微服務(wù)能夠在不知道相關(guān)服務(wù)的物理位置或 IP 地址的情況下找到彼此。

Micronaut 集成了多種工具和庫。有關(guān)詳細(xì)信息,請(qǐng)參閱 Micronaut 服務(wù)發(fā)現(xiàn)文檔。

Consul 支持

請(qǐng)參閱 Micronaut Consul 文檔。

Eureka 支持

請(qǐng)參閱 Micronaut Eureka 文檔。

Kubernetes 支持

Kubernetes 是一個(gè)容器運(yùn)行時(shí),具有許多功能,包括集成服務(wù)發(fā)現(xiàn)和分布式配置。

Micronaut 包括與 Kubernetes 的一流集成。有關(guān)詳細(xì)信息,請(qǐng)參閱 Micronaut Kubernetes 文檔。

AWS Route 53 支持

要使用 Route 53 Service Discovery,您必須滿足以下條件:

  • 運(yùn)行某種類型的 EC2 實(shí)例

  • 在 Route 53 中托管域名

  • 擁有較新版本的 AWS-CLI(例如 14+)

假設(shè)你有這些東西,你就準(zhǔn)備好了。它不像 Consul 或 Eureka 那樣花哨,但除了使用 AWS-CLI 進(jìn)行一些初始設(shè)置外,沒有其他軟件運(yùn)行出錯(cuò)。如果您向服務(wù)添加自定義健康檢查,您甚至可以支持健康檢查。如需更多信息,請(qǐng)?jiān)L問 https://docs.aws.amazon.com/Route53/latest/APIReference/overview-service-discovery.html。

以下是步驟:

  1. 使用 AWS-CLI 創(chuàng)建命名空間。您可以根據(jù)您使用的 IP 或子網(wǎng)創(chuàng)建公共或私有的

  2. 使用 AWS-CLI 命令創(chuàng)建具有 DNS 記錄的服務(wù)

  3. 添加健康檢查或自定義健康檢查(可選)

  4. 將服務(wù) ID 添加到您的應(yīng)用程序配置文件中,如下所示:

示例應(yīng)用程序配置

 Properties Yaml  Toml  Groovy  Hocon  JSON 
aws.route53.registration.enabled=true
aws.route53.registration.aws-service-id=srv-978fs98fsdf
aws.route53.registration.namespace=micronaut.io
micronaut.application.name=something
aws:
  route53:
    registration:
        enabled: true
        aws-service-id: srv-978fs98fsdf
        namespace: micronaut.io
micronaut:
  application:
    name: something
[aws]
  [aws.route53]
    [aws.route53.registration]
      enabled=true
      aws-service-id="srv-978fs98fsdf"
      namespace="micronaut.io"
[micronaut]
  [micronaut.application]
    name="something"
aws {
  route53 {
    registration {
      enabled = true
      awsServiceId = "srv-978fs98fsdf"
      namespace = "micronaut.io"
    }
  }
}
micronaut {
  application {
    name = "something"
  }
}
{
  aws {
    route53 {
      registration {
        enabled = true
        aws-service-id = "srv-978fs98fsdf"
        namespace = "micronaut.io"
      }
    }
  }
  micronaut {
    application {
      name = "something"
    }
  }
}
{
  "aws": {
    "route53": {
      "registration": {
        "enabled": true,
        "aws-service-id": "srv-978fs98fsdf",
        "namespace": "micronaut.io"
      }
    }
  },
  "micronaut": {
    "application": {
      "name": "something"
    }
  }
}
  1. 確保您的構(gòu)建文件中具有以下依賴項(xiàng):

 Gradle Maven 
implementation("io.micronaut.aws:micronaut-aws-route53")
<dependency>
    <groupId>io.micronaut.aws</groupId>
    <artifactId>micronaut-aws-route53</artifactId>
</dependency>
  1. 在客戶端,您需要相同的依賴項(xiàng)和更少的配置選項(xiàng):

 Properties Yaml  Toml  Groovy  Hocon  JSON 
aws.route53.discovery.client.enabled=true
aws.route53.discovery.client.aws-service-id=srv-978fs98fsdf
aws.route53.discovery.client.namespace-id=micronaut.io
aws:
  route53:
    discovery:
      client:
        enabled: true
        aws-service-id: srv-978fs98fsdf
        namespace-id: micronaut.io
[aws]
  [aws.route53]
    [aws.route53.discovery]
      [aws.route53.discovery.client]
        enabled=true
        aws-service-id="srv-978fs98fsdf"
        namespace-id="micronaut.io"
aws {
  route53 {
    discovery {
      client {
        enabled = true
        awsServiceId = "srv-978fs98fsdf"
        namespaceId = "micronaut.io"
      }
    }
  }
}
{
  aws {
    route53 {
      discovery {
        client {
          enabled = true
          aws-service-id = "srv-978fs98fsdf"
          namespace-id = "micronaut.io"
        }
      }
    }
  }
}
{
  "aws": {
    "route53": {
      "discovery": {
        "client": {
          "enabled": true,
          "aws-service-id": "srv-978fs98fsdf",
          "namespace-id": "micronaut.io"
        }
      }
    }
  }
}

然后,您可以使用 DiscoveryClient API 查找通過 Route 53 注冊(cè)的其他服務(wù)。例如:

Sample code for client

DiscoveryClient discoveryClient = embeddedServer.getApplicationContext().getBean(DiscoveryClient.class);
List<String> serviceIds = Flux.from(discoveryClient.getServiceIds()).blockFirst();
List<ServiceInstance> instances = Flux.from(discoveryClient.getInstances(serviceIds.get(0))).blockFirst();

創(chuàng)建命名空間

命名空間類似于常規(guī)的 Route53 托管區(qū)域,它們出現(xiàn)在 Route53 控制臺(tái)中,但控制臺(tái)不支持修改它們。您此時(shí)必須使用 AWS-CLI 來實(shí)現(xiàn)任何服務(wù)發(fā)現(xiàn)功能。

首先決定你是創(chuàng)建面向公眾的命名空間還是私有命名空間,因?yàn)槊畈煌?/p>

Creating Namespace

$ aws servicediscovery create-public-dns-namespace --name micronaut.io --create-request-id create-1522767790 --description adescriptionhere

or

$ aws servicediscovery create-private-dns-namespace --name micronaut.internal.io --create-request-id create-1522767790 --description adescriptionhere --vpc yourvpcID

當(dāng)您運(yùn)行它時(shí),您將獲得一個(gè)操作 ID。您可以使用 get-operation CLI 命令檢查狀態(tài):

Get Operation Results

$ aws servicediscovery get-operation --operation-id asdffasdfsda

您可以使用此命令獲取您進(jìn)行的任何返回操作 ID 的調(diào)用的狀態(tài)。

命令的結(jié)果將告訴您命名空間的 ID。把它寫下來,你會(huì)在接下來的步驟中需要它。如果您遇到錯(cuò)誤,它會(huì)說明錯(cuò)誤是什么。

創(chuàng)建服務(wù)和 DNS 記錄

下一步是創(chuàng)建服務(wù)和 DNS 記錄。

Create Service

$ aws create-service --name yourservicename --create-request-id somenumber --description someservicedescription --dns-config NamespaceId=yournamespaceid,RoutingPolicy=WEIGHTED,DnsRecords=[{Type=A,TTL=1000},{Type=A,TTL=1000}]

DnsRecord 類型可以是 A(ipv4)、AAAA(ipv6)、SRV 或 CNAME。 RoutingPolicy 可以是 WEIGHTED 或 MULTIVALUE。請(qǐng)記住 CNAME 必須使用加權(quán)路由類型,SRV 必須配置有效端口。

要添加運(yùn)行狀況檢查,請(qǐng)?jiān)?nbsp;CLI 上使用以下語法:

Specifying a Health Check

Type=string,ResourcePath=string,FailureThreshold=integer

類型可以是“HTTP”、“HTTPS”或“TCP”。您只能對(duì)公共命名空間使用標(biāo)準(zhǔn)健康檢查。請(qǐng)參閱私有命名空間的自定義健康檢查。資源路徑應(yīng)該是一個(gè) URL,如果正常則返回 200 OK。

對(duì)于自定義健康檢查,您只需指定 --health-check-custom-config FailureThreshold=integer ,它也適用于私有命名空間。

這也很好,因?yàn)?nbsp;Micronaut 發(fā)出脈動(dòng)命令讓 AWS 知道實(shí)例仍然健康。

如需更多幫助,請(qǐng)運(yùn)行“aws discoveryservice create-service help”。

如果成功,您將從該命令返回一個(gè)服務(wù) ID 和一個(gè) ARN。記下來,它將進(jìn)入 Micronaut 配置。

在 Micronaut 中設(shè)置配置

自動(dòng)命名注冊(cè)

添加配置以使您的應(yīng)用程序注冊(cè)到 Route 53 自動(dòng)發(fā)現(xiàn):

Registration Properties

 Properties Yaml  Toml  Groovy  Hocon  JSON 
aws.route53.registration.enabled=true
aws.route53.registration.aws-service-id=<enter the service id you got after creation on aws cli>
aws.route53.discovery.namespace-id=<enter the namespace id you got after creating the namespace>
aws:
  route53:
    registration:
      enabled: true
      aws-service-id: <enter the service id you got after creation on aws cli>
    discovery:
      namespace-id: <enter the namespace id you got after creating the namespace>
[aws]
  [aws.route53]
    [aws.route53.registration]
      enabled=true
      aws-service-id="<enter the service id you got after creation on aws cli>"
    [aws.route53.discovery]
      namespace-id="<enter the namespace id you got after creating the namespace>"
aws {
  route53 {
    registration {
      enabled = true
      awsServiceId = "<enter the service id you got after creation on aws cli>"
    }
    discovery {
      namespaceId = "<enter the namespace id you got after creating the namespace>"
    }
  }
}
{
  aws {
    route53 {
      registration {
        enabled = true
        aws-service-id = "<enter the service id you got after creation on aws cli>"
      }
      discovery {
        namespace-id = "<enter the namespace id you got after creating the namespace>"
      }
    }
  }
}
{
  "aws": {
    "route53": {
      "registration": {
        "enabled": true,
        "aws-service-id": "<enter the service id you got after creation on aws cli>"
      },
      "discovery": {
        "namespace-id": "<enter the namespace id you got after creating the namespace>"
      }
    }
  }
}

發(fā)現(xiàn)客戶端配置

Discovery Properties

 Properties Yaml  Toml  Groovy  Hocon  JSON 
aws.route53.discovery.client.enabled=true
aws.route53.discovery.client.aws-service-id=<enter the service id you got after creation on aws cli>
aws:
  route53:
    discovery:
      client:
        enabled: true
        aws-service-id: <enter the service id you got after creation on aws cli>
[aws]
  [aws.route53]
    [aws.route53.discovery]
      [aws.route53.discovery.client]
        enabled=true
        aws-service-id="<enter the service id you got after creation on aws cli>"
aws {
  route53 {
    discovery {
      client {
        enabled = true
        awsServiceId = "<enter the service id you got after creation on aws cli>"
      }
    }
  }
}
{
  aws {
    route53 {
      discovery {
        client {
          enabled = true
          aws-service-id = "<enter the service id you got after creation on aws cli>"
        }
      }
    }
  }
}
{
  "aws": {
    "route53": {
      "discovery": {
        "client": {
          "enabled": true,
          "aws-service-id": "<enter the service id you got after creation on aws cli>"
        }
      }
    }
  }
}

您還可以通過獲取 bean“Route53AutoNamingClient”來調(diào)用以下方法:

Discovery Methods

// if serviceId is null it will use property "aws.route53.discovery.client.awsServiceId"
Publisher<List<ServiceInstance>> getInstances(String serviceId)
// reads property "aws.route53.discovery.namespaceId"
Publisher<List<String>> getServiceIds()

集成測(cè)試

如果您設(shè)置環(huán)境變量 AWS_SUBNET_ID 并在您的主目錄中配置有效的憑證(在 ~/.aws/credentials 中),您可以運(yùn)行集成測(cè)試。您還需要一個(gè)托管在 Route53 上的域。此測(cè)試將創(chuàng)建一個(gè) t2.nano 實(shí)例、一個(gè)命名空間、服務(wù),并將該實(shí)例注冊(cè)到服務(wù)發(fā)現(xiàn)。當(dāng)測(cè)試完成時(shí),它將刪除/終止它啟動(dòng)的所有資源。

手動(dòng)服務(wù)發(fā)現(xiàn)配置

如果您不希望涉及像 Consul 這樣的服務(wù)發(fā)現(xiàn)服務(wù)器,或者您與無法向 Consul 注冊(cè)的第三方服務(wù)交互,您可以手動(dòng)配置通過服務(wù)發(fā)現(xiàn)可用的服務(wù)。

為此,請(qǐng)使用 micronaut.http.services 設(shè)置。例如:

Manually configuring services

 Properties Yaml  Toml Groovy  Hocon  JSON 
micronaut.http.services.foo.urls[0]=http://foo1
micronaut.http.services.foo.urls[1]=http://foo2
micronaut:
  http:
    services:
      foo:
        urls:
          - http://foo1
          - http://foo2
[micronaut]
  [micronaut.http]
    [micronaut.http.services]
      [micronaut.http.services.foo]
        urls=[
          "http://foo1",
          "http://foo2"
        ]
micronaut {
  http {
    services {
      foo {
        urls = ["http://foo1", "http://foo2"]
      }
    }
  }
}
{
  micronaut {
    http {
      services {
        foo {
          urls = ["http://foo1", "http://foo2"]
        }
      }
    }
  }
}
{
  "micronaut": {
    "http": {
      "services": {
        "foo": {
          "urls": ["http://foo1", "http://foo2"]
        }
      }
    }
  }
}

然后,您可以使用 @Client("foo") 注入一個(gè)客戶端,它將使用上述配置在兩個(gè)配置的服務(wù)器之間進(jìn)行負(fù)載平衡。

將@Client 與服務(wù)發(fā)現(xiàn)一起使用時(shí),必須在 kebab-case 的注釋中指定服務(wù) ID。然而,上述示例中的配置可以采用駝峰式大小寫。

您可以通過指定環(huán)境變量(例如 MICRONAUT_HTTP_SERVICES_FOO_URLS=http://prod1,http://prod2)在生產(chǎn)環(huán)境中覆蓋此配置

請(qǐng)注意,默認(rèn)情況下不會(huì)進(jìn)行健康檢查來斷言引用的服務(wù)是可操作的。您可以通過啟用健康檢查并選擇性地指定健康檢查路徑(默認(rèn)為 /health)來改變它:

Enabling Health Checking

 Properties Yaml  Toml  Groovy  Hocon  JSON 
micronaut.http.services.foo.health-check=true
micronaut.http.services.foo.health-check-interval=15s
micronaut.http.services.foo.health-check-uri=/health
micronaut:
  http:
    services:
      foo:
        health-check: true
        health-check-interval: 15s
        health-check-uri: /health
[micronaut]
  [micronaut.http]
    [micronaut.http.services]
      [micronaut.http.services.foo]
        health-check=true
        health-check-interval="15s"
        health-check-uri="/health"
micronaut {
  http {
    services {
      foo {
        healthCheck = true
        healthCheckInterval = "15s"
        healthCheckUri = "/health"
      }
    }
  }
}
{
  micronaut {
    http {
      services {
        foo {
          health-check = true
          health-check-interval = "15s"
          health-check-uri = "/health"
        }
      }
    }
  }
}
{
  "micronaut": {
    "http": {
      "services": {
        "foo": {
          "health-check": true,
          "health-check-interval": "15s",
          "health-check-uri": "/health"
        }
      }
    }
  }
}
  • health-check表示是否對(duì)該服務(wù)進(jìn)行健康檢查

  • health-check-interval 是檢查之間的間隔

  • health-check-uri 指定健康檢查請(qǐng)求的端點(diǎn)URI

Micronaut 啟動(dòng)一個(gè)后臺(tái)線程來檢查服務(wù)的健康狀態(tài),如果任何配置的服務(wù)以錯(cuò)誤代碼響應(yīng),它們就會(huì)從可用服務(wù)列表中刪除。


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)