springcloud学习-eureka服务中心

springcloud学习-eureka服务中心

1 新建一个空的工程,然后右键工程,进入“New Module”视图,如下:

springcloud学习-eureka服务中心

2 选择jdk版本,一般情况默认自己的jdk即可。

3 点击“Next”,输入对应的工程名称(eurekaserver),选择对应的工程构建方式,这里选择“Gradle Project”,如果gradle不是太熟悉,可以自行百度或者参考后续的博文。

springcloud学习-eureka服务中心

4 点击“Next”,选择左侧的Cloud Discovery,并勾选Eureka Server

springcloud学习-eureka服务中心

5 点击“Next”,然后Finish即可,这时idea已经帮助你完成一些基本的操作。展开对应的eurekaserver module,找到resources目录,添加application.yml,用来配置工程,配置如下:

server:
  port: 8001
  sessionTimeout: 15
  tomcat:
    max-threads: 800
    uri-encoding: UTF-8

spring:
  application:
    name: eurekaServer

security:
  basic:
    enabled: true
  user:
    name: root
    password: melo

eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://root:[email protected]:8001/eureka/

以上主要配置的为eureka节点:

    对应registerWitheureka 表示是否将本应用注册到eureka服务中心

    对应fetchRegistry 表示是否为对应的服务提供者(默认是true)

6 对应build.gradle文件如下:

buildscript {
   ext {
      springBootVersion = '1.5.10.RELEASE'
   }
   repositories {
      mavenCentral()
   }
   dependencies {
      classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
   }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
   mavenCentral()
}


ext {
   springCloudVersion = 'Edgware.SR1'
}

dependencies {
   compile "org.springframework.boot:spring-boot-starter-web:$springBootVersion"
   compile('org.springframework.cloud:spring-cloud-starter-eureka-server')
   testCompile('org.springframework.boot:spring-boot-starter-test')
}

dependencyManagement {
   imports {
      mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
   }
}

7 对应的主程序类代码如下:

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

    主要新增的:@EnableEurekaServer注释,然后正常启动springboot,在浏览器输入http://localhost:8001

springcloud学习-eureka服务中心

如上图表示已经成功完成基本eurekaserver的注册中心功能。


以上是个人学习过程,如有错误,欢迎指正,谢谢大家!