使用eclipse创建spring cloud的eureka客户端和eureka服务端

我也是初学spring cloud,其中也遇到了坑,通过查资料并没有爬出坑,最后通过阅读spring cloud的中文官方文档解决,所以希望刚学spring cloud的朋友先阅读官方文档。官方文档地址:https://springcloud.cc/spring-cloud-dalston.html#_circuit_breaker_hystrix_clients
或者在浏览器中直接搜索spring cloud就可以了使用eclipse创建spring cloud的eureka客户端和eureka服务端
接下来我们进入今天的正题

1、创建maven父工程

File—>new—>other—>Maven—>Maven project
使用eclipse创建spring cloud的eureka客户端和eureka服务端
点击“next”
使用eclipse创建spring cloud的eureka客户端和eureka服务端
使用eclipse创建spring cloud的eureka客户端和eureka服务端

2、创建子项目

打开项目中的pom.xml文件选择overview
使用eclipse创建spring cloud的eureka客户端和eureka服务端
第一种创建方式
点击create直接在父工程中创建
使用eclipse创建spring cloud的eureka客户端和eureka服务端
使用eclipse创建spring cloud的eureka客户端和eureka服务端
使用eclipse创建spring cloud的eureka客户端和eureka服务端
使用同样的方法创建eurekaclient。创建完成的项目结构如图
使用eclipse创建spring cloud的eureka客户端和eureka服务端

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配置

首先看一下官方文档是怎么说的
使用eclipse创建spring cloud的eureka客户端和eureka服务端
需要在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>

往下看
使用eclipse创建spring cloud的eureka客户端和eureka服务端
在src/mian/java中创建启动类
new—>Package创建包
使用eclipse创建spring cloud的eureka客户端和eureka服务端

创建启动类
new—>class
使用eclipse创建spring cloud的eureka客户端和eureka服务端
将代码复制过来

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);
    }

}

接着往下看,我们可以看到
使用eclipse创建spring cloud的eureka客户端和eureka服务端
这个需要我们在src/main/resources中创建application.yml
点击new—>File
使用eclipse创建spring cloud的eureka客户端和eureka服务端
将代码复制进去

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/进入注册中心
使用eclipse创建spring cloud的eureka客户端和eureka服务端

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   #在注册中心显示的名字

客户端完成

刷新注册中心,
使用eclipse创建spring cloud的eureka客户端和eureka服务端

结束了,有什么问题可以直接评论,我会及时和你沟通,大家相互促进成长。