登録センターの SpringCloud-microservice-eureka
1.登録センターの建設
1. eureka-server の依存関係
親プロジェクトは spring-cloud-dependencies を導入します。バージョンを自分で選択してください
SpringBoot のバージョンは 2.3.9.RELEASE、SpringCloud のバージョンは Hoxton.SR10 です。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
新しいモジュールを eureka サービスとして作成するには、次の依存関係を導入するだけで済みます。
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
2.スタートアップクラス
spring-cloud-starter-netflix-eureka-server 依存関係には spring-boot-starter-web が含まれます
そのため、Web プロジェクトとして直接開始できます。
起動クラスを作成し、起動クラスに @EnableEurekaServer アノテーションを付けます
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
3.構成
yaml 構成の例は次のとおりです。
server:
port: 8848 # 服务端口
spring:
application:
name: eureka-server # eureka 的服务名称
eureka:
client:
service-url: # eureka 的地址信息
defaultZone: http://127.0.0.1:8848/eureka
その中で、eureka.client の構成は、現在のサービスを登録センターに登録することです。つまり、eureka サービス
eureka サービスをビルドすると、サービス自体も登録されます。
2.登録サービス
すでに構築されている他のサービスに eureka-client 依存関係を追加します。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
eureka.client 構成を追加します。
spring:
application:
name: order-service # 服务名称
eureka:
client:
service-url: # eureka 的地址
defaultZone: http://127.0.0.1:8848/eureka
サービスを開始し、eureka-server アドレスを開いて登録センター関連情報を表示します
たとえば、localhost:8848 を構成すると、ブラウザーはそれにアクセスできます。