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

2023-03-08 17:48 更新

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

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

Consul 支持

請參閱 Micronaut Consul 文檔。

Eureka 支持

請參閱 Micronaut Eureka 文檔。

Kubernetes 支持

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

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

AWS Route 53 支持

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

  • 運行某種類型的 EC2 實例

  • 在 Route 53 中托管域名

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

假設(shè)你有這些東西,你就準備好了。它不像 Consul 或 Eureka 那樣花哨,但除了使用 AWS-CLI 進行一些初始設(shè)置外,沒有其他軟件運行出錯。如果您向服務(wù)添加自定義健康檢查,您甚至可以支持健康檢查。如需更多信息,請訪問 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)建文件中具有以下依賴項:

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

 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 注冊的其他服務(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 控制臺中,但控制臺不支持修改它們。您此時必須使用 AWS-CLI 來實現(xiàn)任何服務(wù)發(fā)現(xiàn)功能。

首先決定你是創(chuàng)建面向公眾的命名空間還是私有命名空間,因為命令不同:

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

當您運行它時,您將獲得一個操作 ID。您可以使用 get-operation CLI 命令檢查狀態(tài):

Get Operation Results

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

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

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

創(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。請記住 CNAME 必須使用加權(quán)路由類型,SRV 必須配置有效端口。

要添加運行狀況檢查,請在 CLI 上使用以下語法:

Specifying a Health Check

Type=string,ResourcePath=string,FailureThreshold=integer

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

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

這也很好,因為 Micronaut 發(fā)出脈動命令讓 AWS 知道實例仍然健康。

如需更多幫助,請運行“aws discoveryservice create-service help”。

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

在 Micronaut 中設(shè)置配置

自動命名注冊

添加配置以使您的應(yīng)用程序注冊到 Route 53 自動發(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()

集成測試

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

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

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

為此,請使用 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") 注入一個客戶端,它將使用上述配置在兩個配置的服務(wù)器之間進行負載平衡。

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

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

請注意,默認情況下不會進行健康檢查來斷言引用的服務(wù)是可操作的。您可以通過啟用健康檢查并選擇性地指定健康檢查路徑(默認為 /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表示是否對該服務(wù)進行健康檢查

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

  • health-check-uri 指定健康檢查請求的端點URI

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


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

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號