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…

相关文章
相关标签/搜索