您现在的位置是:首页 > 学无止境 > Spring Cloud

Spring cloud入门基础(1)服务的eureka注册中心

贾继涛 2019-01-21 1293人围观

 1.首先看下我整理的项目目录结构如下

 12.png

2.创建File->New->Directory创建文件夹hello-spring-cl-den创建pom.xml里边内容较多不在全部粘贴出

<groupId>com.jjt.cloud</groupId>
  <artifactId>hello-spring-cl-den</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <packaging>pom</packaging>
  <name>hello-spring-cl-den</name>
  <description>微服务根POM</description>
  <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.0.4.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
      <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
      <zipkin.version>2.10.1</zipkin.version>
  </properties>
  <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>

3. 创建File->New->Directory创建文件夹hello-spring-cl-eureka目录结构如下

 截图02.png

4.pom.xml文件

<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>com.jjt.cloud</groupId>
        <artifactId>hello-spring-cl-den</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../hello-spring-cl-den/pom.xml</relativePath>
    </parent>
    <artifactId>hello-spring-cl-eureka</artifactId>
    <packaging>jar</packaging>
    <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.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <!--zipkin链路追踪-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.jjt.hello.hello.spring.cloud.EurekaServerApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

5.java启动文件

package com.jjt.hello.hello.spring.cloud;

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

6.application.yml文件

spring:
  application:
        name: hello-spring-cl-eureka
server:
    port: 8761
eureka:
    client:
        serviceUrl:
            defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
        #由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己
        registerWithEureka: false
        #由于注册中心的职责就是维护服务实例,它并不需要去检索服务,所以也设置为false
        fetchRegistry: false
    instance:
      hostname: 127.0.0.1

 

7.启动地址 http://127.0.0.1:8761/


我的代码公开在码云 其中包括pom中 父依赖



https://gitee.com/jiajitao/springCloudFirst


您的支持