使用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
客户端完成
刷新注册中心,
结束了,有什么问题可以直接评论,我会及时和你沟通,大家相互促进成长。