欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

简单学习SpringCloud微服务-- Eureka

程序员文章站 2022-06-16 13:17:57
SpringBoot多模块微服务项目搭建创建Maven父工程创建Eureka模块pom.xml如下:application.yml如下:启动类中加入@EnableEurekaServer注解启动测试创建Maven父工程

创建Maven父工程

<?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>

    <groupId>SpringCloudTest</groupId>
    <artifactId>EurekaTest</artifactId>
    <version>1.0-SNAPSHOT</version>

	<!-- 添加子模块 -->
    <modules>
        <module>eurekaclient</module>
        <module>zuultest</module>
        <module>ribbon</module>
        <module>feign</module>
        <module>eurakaservers</module>
    </modules>
	
	<!-- SpringBoot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    
	<!-- 公用properties -->
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR8</spring-cloud.version>
    </properties>

	<!-- 公用依赖 -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </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>
                <version>2.3.2.RELEASE</version>
            </plugin>
        </plugins>
    </build>
</project>

创建Eureka模块

简单学习SpringCloud微服务-- Eureka
简单学习SpringCloud微服务-- Eureka

简单学习SpringCloud微服务-- Eureka

pom.xml如下:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!--继承父模块-->
    <parent>
        <groupId>SpringCloudTest</groupId>
        <artifactId>EurekaTest</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <groupId>SpringCloudTest</groupId>
    <artifactId>eurakaservers</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eurakaservers</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR8</spring-cloud.version>
    </properties>

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

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

</project>

application.yml如下:

server:
  port: 8080

#主机名
eureka:
  instance:
    hostname: localhost

  client:
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/   #服务注册地址
    register-with-eureka: false  
    fetch-registry: false

启动类中加入@EnableEurekaServer注解

简单学习SpringCloud微服务-- Eureka

启动测试

浏览器输入 localhost:8080
简单学习SpringCloud微服务-- Eureka
出现如图即为成功。
官方文档: https://projects.spring.io/spring-cloud/spring-cloud.html#spring-cloud-eureka-server.

本文地址:https://blog.csdn.net/TYblackandwhite/article/details/109614533