查看原文
其他

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

cuifuan Java知音 2019-06-22

点击上方“Java知音”,选择“置顶公众号”

技术文章第一时间送达!

更多SpringCloud教程,搜索Java知音  

网站菜单栏:JavaWeb-SpringCloud

https://www.javazhiyin.com/javaweb/springcloud


网上教程具体如下图 

注册中心,开了N个端口就创建了N个Module
还有的就是各种创建eureka然后互相注册,对于新手来说是很大的误解
以及在client去注册的时候,注册中心要写几个
下面开始叙述并实际验证下

准备工作

当前的技术以及工具

  • IDEA2018.3

  • JDK1.8

  • Gradle 5.0

  • tomcat 7

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

首先创建公共依赖管理

一步一步创建一个Gradle的初始项目就可以了
配置文件

gradle.perproties 无此文件自行创建

  1. ## dependency versions.

  2. springBootVersion=2.1.2.RELEASE

  3. springCloudVersion=Finchley.RELEASE

  4. ### docker configuration

  5. #gradle docker plugin version

  6. transmodeGradleDockerVersion=1.2

  7. #This configuration is for docker container environment to access the local machine host,in Chinese is "宿主机" ip.

  8. hostMachineIp=127.0.0.1

build.gradle

  1. buildscript {

  2.    repositories {

  3.        maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }

  4.        maven { url "https://oss.sonatype.org/content/groups/public/" }

  5.        maven { url "https://repo.spring.io/libs-milestone/" }

  6.        jcenter()

  7.        mavenCentral()

  8.    }

  9.    dependencies {

  10.        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")

  11.    }

  12. }


  13. allprojects {

  14.    apply plugin: 'java'

  15.    apply plugin: 'org.springframework.boot'

  16.    apply plugin: 'io.spring.dependency-management'


  17.    group = 'store.zabbix'

  18.    version = '0.0.1-SNAPSHOT'

  19.    sourceCompatibility = '1.8'



  20.    repositories {

  21.        maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }

  22.        maven { url "https://oss.sonatype.org/content/groups/public/" }

  23.        maven { url "https://repo.spring.io/libs-milestone/" }

  24.        jcenter()

  25.        mavenCentral()

  26.    }


  27.    dependencies {

  28.        implementation 'org.springframework.boot:spring-boot-starter-web'

  29.        testImplementation "org.springframework.boot:spring-boot-starter-test"

  30.    }



  31.    dependencyManagement {

  32.        imports {

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

  34.        }

  35.    }

  36. }

setting.gradle

  1. rootProject.name = 'springcloud-tools'

  2. def dir = new File(settingsDir.toString())

  3. def projects = new HashSet()

  4. def projectSymbol = File.separator + 'src'


  5. dir.eachDirRecurse { subDir ->

  6.    def subDirName = subDir.canonicalPath

  7.    def isSubProject = true

  8.    if (subDirName.endsWith(projectSymbol)) {

  9.        for (String projectDir in projects) {

  10.            if (subDirName.startsWith(projectDir)) {

  11.                isSubProject = false

  12.                break

  13.            }

  14.        }

  15.        if (isSubProject) {

  16.            projects << subDirName

  17.            def lastIndex = subDirName.lastIndexOf(projectSymbol)

  18.            def gradleModulePath = subDirName.substring(dir.canonicalPath.length(), lastIndex).replace(File.separator, '')

  19.            println "include " + gradleModulePath

  20.            include gradleModulePath

  21.        }

  22.    }

  23. }

  24. //include('tools-eureka')

至此我们创建了一个新的项目,结构图
红色圈内的后续创建

我们开始创建eureka-server

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

  1. dependencies {

  2.    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'

  3.    testImplementation 'org.springframework.boot:spring-boot-starter-test'

  4. }

application.yml

  1. spring:

  2.  application:

  3.    name: eureka-server

  4.  profiles:

  5.    active: server1

application-server1.yml

  1. server:

  2.  port: 8000

  3. eureka:

  4.  client:

  5.    # 表示是否注册自身到eureka服务器

  6.    # register-with-eureka: false

  7.    # 是否从eureka上获取注册信息

  8.    # fetch-registry: false

  9.    service-url:

  10.      defaultZone: http://127.0.0.1:8001/eureka/,http://127.0.0.1:8002/eureka/

application-server2.yml

  1. server:

  2.  port: 8001

  3. eureka:

  4.  client:

  5.    # 表示是否注册自身到eureka服务器

  6.    # register-with-eureka: false

  7.    # 是否从eureka上获取注册信息

  8.    # fetch-registry: false

  9.    service-url:

  10.      defaultZone: http://127.0.0.1:8000/eureka/,http://127.0.0.1:8002/eureka/

  11. #spring:

  12. #  application:

  13. #    name: eurka-server2

applicayion-server3.yml

  1. server:

  2.  port: 8002

  3. eureka:

  4.  client:

  5.    # 表示是否注册自身到eureka服务器

  6.    # register-with-eureka: false

  7.    # 是否从eureka上获取注册信息

  8.    # fetch-registry: false

  9.    service-url:

  10.      defaultZone: http://127.0.0.1:8001/eureka/,http://127.0.0.1:8000/eureka/

  11. #spring:

  12. #  application:

  13. #    name: eurka-server3

ToolsEurekaApplication.java

核心:注解@EnableEurekaServer

  1. @EnableEurekaServer

  2. @SpringBootApplication

  3. public class ToolsEurekaApplication {


  4.    public static void main(String[] args) {

  5.        SpringApplication.run(ToolsEurekaApplication.class, args);

  6.    }


  7. }

创建eureka-client来注册到eureka-server

快速创建一个Gradle的SpringBoot项目

build.gradle

  1. dependencies {

  2.    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'

  3. }

ToolsEurekaClientApplication.java

核心注解:@EnableEurekaClient

  1. package store.zabbix.toolseurekaclient;


  2. import org.springframework.beans.factory.annotation.Value;

  3. import org.springframework.boot.SpringApplication;

  4. import org.springframework.boot.autoconfigure.SpringBootApplication;

  5. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

  6. import org.springframework.web.bind.annotation.GetMapping;

  7. import org.springframework.web.bind.annotation.RestController;


  8. @SpringBootApplication

  9. @EnableEurekaClient

  10. @RestController

  11. public class ToolsEurekaClientApplication {


  12.    public static void main(String[] args) {

  13.        SpringApplication.run(ToolsEurekaClientApplication.class, args);

  14.    }


  15.    @Value("${server.port}")

  16.    private int port;


  17.    @GetMapping("test")

  18.    public String showPort(){

  19.        return "my port is "+port ;

  20.    }


  21. }

application.yml

  1. eureka:

  2.  client:

  3.    service-url:

  4.      defaultZone: http://127.0.0.1:8000/eureka/

  5. server:

  6.  port: 8762

  7. spring:

  8.  application:

  9.    name: tools-eureka-client

开始启动eureka-server

新建启动类并配置

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

  1. -Dspring.profiles.active=server2

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

上图得知我们已经启动了3个端口,并互相注册了

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

  1. # 表示是否注册自身到eureka服务器

  2. # register-with-eureka: false

  3. # 是否从eureka上获取注册信息

  4. # fetch-registry: false  

修改后的application.yml

  1. #server:

  2. #  port: 8761

  3. eureka:

  4. #  instance:

  5. #    hostname: server1

  6.  client:

  7.    register-with-eureka: false

  8.    fetch-registry: false

  9. #    service-url:

  10. #      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

  11. ##server.port: 指明了应用启动的端口号

  12. ##eureka.instance.hostname: 应用的主机名称

  13. ##eureka.client.registerWithEureka: 值为false意味着自身仅作为服务器,不作为客户端

  14. ##eureka.client.fetchRegistry: 值为false意味着无需注册自身

  15. ##eureka.client.serviceUrl.defaultZone: 指明了应用的URL

  16. #spring:

  17. #  application:

  18. #    name: eurka-server

  19. spring:

  20.  application:

  21.    name: eureka-server

  22.  profiles:

  23.    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来指定

源码地址:https://github.com/cuifuan/springcloud-tools

本文参考

https://github.com/happyyangyuan/springcloud-quickstart http://www.cnblogs.com/hfultrastrong/p/8547236.html


更多Java技术文章,尽在【Java知音】网站。

网址:www.javazhiyin.com  ,搜索Java知音可达!



如果你觉得文章不错,欢迎点赞分享到朋友圈

    您可能也对以下帖子感兴趣

    文章有问题?点此查看未经处理的缓存