SpringCloud【一】單服務多端口啓動

IDEA 微服務單項目多端口啓動

網上教程具體以下圖 html

註冊中心,開了N個端口就建立了N個Module
還有的就是各類建立eureka而後互相註冊,對於新手來講是很大的誤解
以及在client去註冊的時候,註冊中心要寫幾個
下面開始敘述並實際驗證下

準備工做

當前的技術以及工具java

  • IDEA2018.3
  • JDK1.8
  • Gradle 5.0
  • tomcat 7

須要你對基本的微服務有一點點的瞭解,若是不知道什麼是微服務,百度基本學習下也不會花很長時間git

首先建立公共依賴管理

一步一步建立一個Gradle的初始項目就能夠了
配置文件

gradle.perproties 無此文件自行建立github

## dependency versions.
springBootVersion=2.1.2.RELEASE
springCloudVersion=Finchley.RELEASE
### docker configuration
#gradle docker plugin version
transmodeGradleDockerVersion=1.2
#This configuration is for docker container environment to access the local machine host,in Chinese is "宿主機" ip.
hostMachineIp=127.0.0.1
複製代碼

build.gradleweb

buildscript {
    repositories {
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
        maven { url "https://oss.sonatype.org/content/groups/public/" }
        maven { url "https://repo.spring.io/libs-milestone/" }
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

allprojects {
    apply plugin: 'java'
    apply plugin: 'org.springframework.boot'
    apply plugin: 'io.spring.dependency-management'

    group = 'store.zabbix'
    version = '0.0.1-SNAPSHOT'
    sourceCompatibility = '1.8'


    repositories {
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
        maven { url "https://oss.sonatype.org/content/groups/public/" }
        maven { url "https://repo.spring.io/libs-milestone/" }
        jcenter()
        mavenCentral()
    }

    dependencies {
        implementation 'org.springframework.boot:spring-boot-starter-web'
        testImplementation "org.springframework.boot:spring-boot-starter-test"
    }


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

setting.gradlespring

rootProject.name = 'springcloud-tools'
def dir = new File(settingsDir.toString())
def projects = new HashSet()
def projectSymbol = File.separator + 'src'

dir.eachDirRecurse { subDir ->
    def subDirName = subDir.canonicalPath
    def isSubProject = true
    if (subDirName.endsWith(projectSymbol)) {
        for (String projectDir in projects) {
            if (subDirName.startsWith(projectDir)) {
                isSubProject = false
                break
            }
        }
        if (isSubProject) {
            projects << subDirName
            def lastIndex = subDirName.lastIndexOf(projectSymbol)
            def gradleModulePath = subDirName.substring(dir.canonicalPath.length(), lastIndex).replace(File.separator, '')
            println "include " + gradleModulePath
            include gradleModulePath
        }
    }
}
//include('tools-eureka')
複製代碼

至此咱們建立了一個新的項目,結構圖
docker

紅色圈內的後續建立

咱們開始建立eureka-server

build.gradle
其依賴已在父類公共管理
這裏只須要聲明如今所須要的依賴便可tomcat

dependencies {
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
複製代碼

application.ymlbash

spring:
  application:
    name: eureka-server
  profiles:
    active: server1
複製代碼

application-server1.yml服務器

server:
  port: 8000
eureka:
  client:
    # 表示是否註冊自身到eureka服務器
    # register-with-eureka: false
    # 是否從eureka上獲取註冊信息
    # fetch-registry: false
    service-url:
      defaultZone: http://127.0.0.1:8001/eureka/,http://127.0.0.1:8002/eureka/
複製代碼

application-server2.yml

server:
  port: 8001
eureka:
  client:
    # 表示是否註冊自身到eureka服務器
    # register-with-eureka: false
    # 是否從eureka上獲取註冊信息
    # fetch-registry: false
    service-url:
      defaultZone: http://127.0.0.1:8000/eureka/,http://127.0.0.1:8002/eureka/
#spring:
# application:
# name: eurka-server2
複製代碼

applicayion-server3.yml

server:
  port: 8002
eureka:
  client:
    # 表示是否註冊自身到eureka服務器
    # register-with-eureka: false
    # 是否從eureka上獲取註冊信息
    # fetch-registry: false
    service-url:
      defaultZone: http://127.0.0.1:8001/eureka/,http://127.0.0.1:8000/eureka/
#spring:
# application:
# name: eurka-server3
複製代碼

ToolsEurekaApplication.java

核心:註解@EnableEurekaServer

@EnableEurekaServer
@SpringBootApplication
public class ToolsEurekaApplication {

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

}
複製代碼

建立eureka-client來註冊到eureka-server

快速建立一個Gradle的SpringBoot項目

build.gradle

dependencies {
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
}
複製代碼

ToolsEurekaClientApplication.java

核心註解:@EnableEurekaClient

package store.zabbix.toolseurekaclient;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableEurekaClient
@RestController
public class ToolsEurekaClientApplication {

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

    @Value("${server.port}")
    private int port;

    @GetMapping("test")
    public String showPort(){
        return "my port is "+port ;
    }

}

複製代碼

application.yml

eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:8000/eureka/
server:
  port: 8762
spring:
  application:
    name: tools-eureka-client
複製代碼

開始啓動eureka-server

新建啓動類並配置

第一次啓動啓動類以後會存在一個啓動配置
如上圖同樣去複製一個,而後在options裏指定一下你須要啓動的項目資源配置文件

-Dspring.profiles.active=server2
複製代碼

啓動配置的名字能夠自定義
建議帶上端口

上圖得知咱們已經啓動了3個端口,並互相註冊了

已經相互註冊成功了
接下來咱們把註釋的開啓

# 表示是否註冊自身到eureka服務器 
# register-with-eureka: false
# 是否從eureka上獲取註冊信息
# fetch-registry: false 
複製代碼

修改後的application.yml

#server:
# port: 8761
eureka:
# instance:
# hostname: server1
  client:
    register-with-eureka: false
    fetch-registry: false
# service-url:
# defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
##server.port: 指明瞭應用啓動的端口號
##eureka.instance.hostname: 應用的主機名稱
##eureka.client.registerWithEureka: 值爲false意味着自身僅做爲服務器,不做爲客戶端
##eureka.client.fetchRegistry: 值爲false意味着無需註冊自身
##eureka.client.serviceUrl.defaultZone: 指明瞭應用的URL
#spring:
# application:
# name: eurka-server
spring:
  application:
    name: eureka-server
  profiles:
    active: server1
複製代碼

以後把三個端口的都重啓下

能夠看到不會註冊本身的,我想基礎區別也在這個界面了

啓動順序 eureka-server => eureka-client

啓動eureka-client

看到這裏咱們訪問的是http://127.0.0.1:8002/
雖然咱們配置的是8000端口
但仍是在8002端口註冊了,也就是這也是eureka互相註冊以後達到的高可用的效果,集羣,咱們能夠把8000和8001端口宕掉,不影響使用

提示

  1. 上面的啓動配置是須要啓動幾個端口就要配置幾個
  2. 項目跑起來的時候有時候會拋些錯誤,試着重啓下,訪問下若是正常就能夠 ,通常就是超時或者本身尋找不到註冊本身的服務中心
  3. VM options:-Dspring.profiles.active=xxx,啓動類這裏配置的是你application-xxx.yml名字裏的xxx,-D是用java默認原生屬性 3.除了上面那樣指定配置文件,還能夠用Program arguments來指定

4.源碼地址:github.com/cuifuan/spr…

本文參考
github.com/happyyangyu… www.cnblogs.com/hfultrastro…

相關文章
相關標籤/搜索