likes
comments
collection
share

Spring Cloud 单服务多端口启动

作者站长头像
站长
· 阅读数 27

IDEA 微服务单项目多端口启动

网上教程具体如下图 Spring Cloud 单服务多端口启动 注册中心,开了 N 个端口就创建了 N 个 Module 还有的就是各种创建 eureka 然后互相注册,对于新手来说是很大的误解 以及在 client 去注册的时候,注册中心要写几个 下面开始叙述并实际验证下

1.准备工作

当前的技术以及工具

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

需要你对基本的微服务有一点点的了解,如果不知道什么是微服务,百度基本学习下也不会花很长时间

2.首先创建公共依赖管理

Spring Cloud 单服务多端口启动 一步一步创建一个 Gradle 的初始项目就可以了 配置文件

gradle.perproties 无此文件自行创建

## 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.gradle

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.gradle

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')

至此我们创建了一个新的项目,结构图 Spring Cloud 单服务多端口启动 红色圈内的后续创建

3.创建eureka-server

build.gradle 其依赖已在父类公共管理 这里只需要声明现在所需要的依赖即可

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

application.yml

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

}

4.创建 eureka-client

快速创建一个 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

5.启动eureka-server

新建启动类并配置 Spring Cloud 单服务多端口启动 Spring Cloud 单服务多端口启动

第一次启动启动类之后会存在一个启动配置 如上图一样去复制一个,然后在 options 里指定一下你需要启动的项目资源配置文件

-Dspring.profiles.active=server2

启动配置的名字可以自定义 建议带上端口

Spring Cloud 单服务多端口启动

上图得知我们已经启动了 3 个端口,并互相注册了 Spring Cloud 单服务多端口启动

已经相互注册成功了 接下来我们把注释的开启

# 表示是否注册自身到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

之后把三个端口的都重启下 Spring Cloud 单服务多端口启动

可以看到不会注册自己的,我想基础区别也在这个界面了

启动顺序 eureka-server => eureka-client

6.启动 eureka-client

Spring Cloud 单服务多端口启动

Spring Cloud 单服务多端口启动

看到这里我们访问的是http://127.0.0.1:8002/ 虽然我们配置的是 8000 端口 但还是在 8002 端口注册了,也就是这也是 eureka 互相注册之后达到的高可用的效果,集群,我们可以把 8000 和 8001 端口宕掉,不影响使用

7.提示

  1. 上面的启动配置是需要启动几个端口就要配置几个
  2. 项目跑起来的时候有时候会抛些错误,试着重启下,访问下如果正常就可以 ,一般就是超时或者自己寻找不到注册自己的服务中心
  3. VM options:-Dspring.profiles.active=xxx,启动类这里配置的是你 application-xxx.yml 名字里的 xxx,-D 是用 java 默认原生属性 3.除了上面那样指定配置文件,还可以用 Program arguments 来指定 Spring Cloud 单服务多端口启动

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

本文参考: