IDEA 微服务单项目多端口启动
网上教程具体如下图

注册中心,开了N个端口就创建了N个Module
还有的就是各种创建eureka然后互相注册,对于新手来说是很大的误解
以及在client去注册的时候,注册中心要写几个
下面开始叙述并实际验证下
准备工作
当前的技术以及工具
- IDEA2018.3
- JDK1.8
- Gradle 5.0
- tomcat 7
需要你对基本的微服务有一点点的了解,如果不知道什么是微服务,百度基本学习下也不会花很长时间
首先创建公共依赖管理

一步一步创建一个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')
至此我们创建了一个新的项目,结构图

红色圈内的后续创建
我们开始创建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);
}
}
创建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端口宕掉,不影响使用
提示
- 上面的启动配置是需要启动几个端口就要配置几个
- 项目跑起来的时候有时候会抛些错误,试着重启下,访问下如果正常就可以 ,一般就是超时或者自己寻找不到注册自己的服务中心
- VM options:-Dspring.profiles.active=xxx,启动类这里配置的是你application-xxx.yml名字里的xxx,-D是用java默认原生属性
3.除了上面那样指定配置文件,还可以用Program arguments来指定
image
4.源码地址:https://github.com/cuifuan/springcloud-tools
本文参考
https://github.com/happyyangyuan/springcloud-quickstart
http://www.cnblogs.com/hfultrastrong/p/8547236.html
网友评论