springBoot与springcloud整合之注册中心(eureka)

前言

最近在做springboot整合springcloud,环境:springboot 2.1.5 springcloud Greenwich.RELEASE ,需要说明一点,springboot与springcloud的版本匹配很重要,不匹配有很多问题。

父pom文件

<?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>
    <packaging>pom</packaging>
    <modules>
        <module>eureka</module>  
        <module>consumers</module>
        <module>provider</module>
        <module>consumer-feign</module>
        <module>service-zuul</module>
        <module>service-config</module>
        <module>service-client</module>
    </modules>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath/>
    </parent>

    <groupId>com.example</groupId>
    <artifactId>boot-cloud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>boot-cloud</name>
    <description>Demo project for Spring Boot</description>

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

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.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>
    </dependencies>

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

</project>

注册中心(eureka)工程

pom文件

<?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">
    <parent>
        <artifactId>boot-cloud</artifactId>
        <groupId>com.example</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka</artifactId>

    <dependencies>
        <!--netflix-eureka-server服务端注册中心-->
        <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>
    </dependencies>
</project>

yml配置文件

#端口号
server:
  port: 8090

#应用名称
spring:
 application:
  name: eureka-server

eureka:
  instance:
   hostname: localhost

  client:
   register-with-eureka: false # 代表不向注册中心注册自己
   fetch-registry: false # 维护服务实例,不需要检索服务

#eureka监控平台地址
   serviceUrl:
    defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka

启动类

package com.test;

import org.springframework.boot.SpringApplication;

/**
 * @Date 2019/12/24  11:44
 * @Desc  注册中心服务
 */
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer // 开启eureka注册中心服务
public class EurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }
}

eureka启动

本地访问: http://localhost:8090
springBoot与springcloud整合之注册中心(eureka)_第1张图片
到这里就简单介绍了一下springcloud的注册中心,最难的就是springboot与springcloud的版本匹配。

你可能感兴趣的:(springcloud)