1. 程式人生 > 實用技巧 >使用eclipse建立spring cloud的eureka客戶端和eureka服務端

使用eclipse建立spring cloud的eureka客戶端和eureka服務端

轉:https://blog.csdn.net/qq_42382972/article/details/85843080

我這裡用的是自帶spring外掛的eclipse

下面是分享的eclipse下載連結

連結:https://pan.baidu.com/s/18tGSAb1ZwM2LIXirZX5nyw

提取碼:jj12

1、建立maven父工程

File—>new—>other—>Maven—>Maven project

點選“next”

2、建立子專案

開啟專案中的pom.xml檔案選擇overview

第一種建立方式
點選create直接在父工程中建立



使用同樣的方法建立eurekaclient。建立完成的專案結構如圖

3、父工程pom.xml配置

在spring cloud官方文件上面有一段maven父工程pom.xmlde示例配置,可以直接引用過來。

<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.test</groupId>
  <artifactId>common</artifactId>
  <version>0.0
.1-SNAPSHOT</version> <packaging>pom</packaging> <modules> <module>eurekaserver</module> <module>eurekaclient</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3
.5.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

我們可以看得到,spring-boot-starter-parent的版本是1.3.5.RELEASE,版本有些低我們將它替換到2.0.2.RELEASE的版本,同時需要替換spring-cloud-dependencies的版本,替換配置如下:

<parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>2.0.2.RELEASE</version>
       <relativePath /> <!-- lookup parent from repository -->
   </parent>

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

官方文件中缺少很多引用,需要根據專案進行新增,但首先我們需要先新增spring-boot-starter-web jar包。因為剛開始我沒有引用這個jar包,導致客戶端無法註冊到註冊中心中。

4、eurekaserver Pom.xml配置

首先看一下官方文件是怎麼說的

需要在eurekaserver的pom.xml檔案中配置:eurekaserver的jar包。
注意:專案應該是jar,如果你的<packaging>war</packaging>,需要你修改成<packaging>jar</packaging>

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.test</groupId>
  <artifactId>eurekaserver</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  
  <parent>
    <groupId>com.test</groupId>
    <artifactId>common</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  
  <name>eurekaserver </name>
  
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-eureka-server</artifactId>
      <version>1.4.3.RELEASE</version>
    </dependency>
  </dependencies>
  
  <build>
    <finalName>eurekaserver</finalName>
  </build>
</project>

往下看

在src/mian/java中建立啟動類
new—>Package建立包

建立啟動類
new—>class

將程式碼複製過來

package com.teat.eurekaserver;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class Application {

    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(true).run(args);
    }

}

接著往下看,我們可以看到

這個需要我們在src/main/resources中建立application.yml
點選new—>File

將程式碼複製進去

server:
  port: 8761   #埠號

eureka:
  instance:
    hostname: localhost   #主機ip
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:  #註冊中心地址
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

在啟動類中執行程式
右鍵點選啟動類—>Run as —>java application
在瀏覽器中輸入http://localhost:8761/進入註冊中心

4、eurekaclient 配置

和伺服器的配置一樣,根據官方文件操作即可。直接看配置
pom.xml

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.test</groupId>
  <artifactId>eurekaclient</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  
  <parent>
    <groupId>com.test</groupId>
    <artifactId>common</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  
  <name>eurekaclient</name>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-eureka</artifactId>
      <version>1.4.3.RELEASE</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>eurekaclient</finalName>
  </build>
</project>

啟動類

package com.test.eurekaclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;

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

application.yml

server:
  port: 8762    #埠號

eureka:
  client:
    serviceUrl:   #註冊中心地址
      defaultZone: http://localhost:8761/eureka/
spring:
  application:
    name: xyj-common   #在註冊中心顯示的名字

客戶端完成

重新整理註冊中心,

1、建立maven父工程

File—>new—>other—>Maven—>Maven project

點選“next”

2、建立子專案

開啟專案中的pom.xml檔案選擇overview

第一種建立方式
點選create直接在父工程中建立



使用同樣的方法建立eurekaclient。建立完成的專案結構如圖

3、父工程pom.xml配置

在spring cloud官方文件上面有一段maven父工程pom.xmlde示例配置,可以直接引用過來。

<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.test</groupId>
  <artifactId>common</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  <modules>
  	<module>eurekaserver</module>
  	<module>eurekaclient</module>
  </modules>
  <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>1.3.5.RELEASE</version>
       <relativePath /> <!-- lookup parent from repository -->
   </parent>

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

<dependencies>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-config</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>
	<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

<build>
	<plugins>
           <plugin>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-maven-plugin</artifactId>
           </plugin>
	</plugins>
</build>
</project>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

我們可以看得到,spring-boot-starter-parent的版本是1.3.5.RELEASE,版本有些低我們將它替換到2.0.2.RELEASE的版本,同時需要替換spring-cloud-dependencies的版本,替換配置如下:

<parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>2.0.2.RELEASE</version>
       <relativePath /> <!-- lookup parent from repository -->
   </parent>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.RELEASE</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

官方文件中缺少很多引用,需要根據專案進行新增,但首先我們需要先新增spring-boot-starter-web jar包。因為剛開始我沒有引用這個jar包,導致客戶端無法註冊到註冊中心中。

4、eurekaserver Pom.xml配置

首先看一下官方文件是怎麼說的

需要在eurekaserver的pom.xml檔案中配置:eurekaserver的jar包。
注意:專案應該是jar,如果你的<packaging>war</packaging>,需要你修改成<packaging>jar</packaging>

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.test</groupId>
  <artifactId>eurekaserver</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  
  <parent>
    <groupId>com.test</groupId>
    <artifactId>common</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  
  <name>eurekaserver </name>
  
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-eureka-server</artifactId>
      <version>1.4.3.RELEASE</version>
    </dependency>
  </dependencies>
  
  <build>
    <finalName>eurekaserver</finalName>
  </build>
</project>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

往下看

在src/mian/java中建立啟動類
new—>Package建立包

建立啟動類
new—>class

將程式碼複製過來

package com.teat.eurekaserver;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class Application {

    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(true).run(args);
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

接著往下看,我們可以看到

這個需要我們在src/main/resources中建立application.yml
點選new—>File

將程式碼複製進去

server:
  port: 8761   #埠號

eureka:
  instance:
    hostname: localhost   #主機ip
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:  #註冊中心地址
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

在啟動類中執行程式
右鍵點選啟動類—>Run as —>java application
在瀏覽器中輸入http://localhost:8761/進入註冊中心

4、eurekaclient 配置

和伺服器的配置一樣,根據官方文件操作即可。直接看配置
pom.xml

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.test</groupId>
  <artifactId>eurekaclient</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  
  <parent>
    <groupId>com.test</groupId>
    <artifactId>common</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  
  <name>eurekaclient</name>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-eureka</artifactId>
      <version>1.4.3.RELEASE</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>eurekaclient</finalName>
  </build>
</project>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

啟動類

package com.test.eurekaclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

application.yml

server:
  port: 8762    #埠號

eureka:
  client:
    serviceUrl:   #註冊中心地址
      defaultZone: http://localhost:8761/eureka/
spring:
  application:
    name: xyj-common   #在註冊中心顯示的名字
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

客戶端完成

重新整理註冊中心,

結束了,有什麼問題可以直接評論,我會及時和你溝通,大家相互促進成長。