String Cloud 搭建eureka注册中心(Java 11),集成开发工具IDEA

一、利用IDEA生成项目结构

1、创建新项目,选择Spring Initializr,如图所示

 

2、点击Next,设置配置项目基本信息和Java版本,如图所示

 

3、点击Next,选择项目类型 ,如图所示

4、 点击Next,如图所示

5、点击Finish,完成项目结构生成,如图所示(注意:此处resources目录下的application文件格式做了修改,可根据喜好自由设置,无特别含义)

 

二、项目调整

通过以上步骤创建的项目是无法正常运行的,这里对项目做一下调整。

1、application文件添加配置信息,初始化文件为空,配置信息如下,可根据喜好自由调整

server:
  port: 10000

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

2、调整pom文件,引入相关相关依赖包,配置如下

特别说明:之所以需要引入bind相关的包是因为Java9以及之后的版本把此相关的包给处理掉了。

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-server</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
        <spring-cloud.version>Greenwich.RC2</spring-cloud.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- Java9以及之后版本需要引入 start -->
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.1.2</version>
        </dependency>

        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.1</version>
        </dependency>
        <!-- end -->

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>

</project>

3、修改Main Class,如下所示

特别说明:初始化的主类是没有注解@EnableEurekaServer的,需要手动添加

package com.example.eurekaserver;

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

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }

}

三、启动项目

经过以上步骤,基本的项目就搭建完成了,运行后在浏览器地址栏输入地址:

http://localhost:10000 

即可访问到如下页面 

以上,一个简单的Spring Cloud 基于Eureka的注册中心服务就搭建完成了。

四、总结

通过以上步骤可以看到,搭建过程很简单,很多童鞋可能会觉着这么简单事情不至于亲自操练一遍。其实不然,看似简单的东西往往蕴含着深刻的道理。切勿眼高手低,看一眼就放弃实际操作的过程。

在这里我们需要明白一个道理,我们并不是为了某事二去做某事,我们是为了提高自己,技术能力也好,解决问题的能力也罢,践行之后我们才能体会这背后正真的乐趣。 

欢迎童鞋们一起留言讨论学习,把技术死磕到底!

发布了23 篇原创文章 · 获赞 24 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_35971547/article/details/86482573
今日推荐