SpringCloud Eurekaサービスの登録とディスカバリー-使いやすい

今日は、Spring CloudのEurekaコンポーネントの使用について説明します

1. Mavenメインプロジェクトを確立する

以下の依存関係をメインプロジェクトpom.xmlに追加します

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</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>Hoxton.SR3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

2. Eurekaサーバーモジュールを確立する

最初のステップは、新しいSpring Bootモジュールを作成することです

ファイル->新規->モジュール-> springInitializr->勾选Eurekaサーバー-> OK

Eurekaサーバーのpom.xml依存関係は次のとおりです

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

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Hoxton.SR3</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

2番目のステップは、Eurekaサーバーのapplication.propertiesファイルに次のコードを追加することです

server.port=8771
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:8771/eureka/
spring.application.name=EurekaService

3番目のステップは、@ EnableEurekaServerアノテーションをApplicationクラスに追加することです。

3. Eurekaクライアントを確立する

最初のステップは、新しいSpring Bootモジュールを作成することです

ファイル->新規->モジュール-> springInitializr->勾选Eureka Discovery Client-> OK

Eurekaクライアントのpom.xml依存関係は次のとおりです

	<dependencies>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.1.2</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Hoxton.SR3</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

2番目のステップは、Eurekaクライアントのapplication.propertiesファイルに次のコードを追加することです

server.port=8772
eureka.client.serviceUrl.defaultZone=http://localhost:8771/eureka/
spring.application.name=commonservice

3番目のステップは、@ EnableEurekaClientアノテーションをApplicationクラスに追加することです。

4. Eurekaサーバーとクライアントを起動します

最終レンダリング

元の記事を公開34件 Like1 Visits 1945

おすすめ

転載: blog.csdn.net/qq_38974638/article/details/103871319