IDEA 微服務單項目多端口啓動
作者:小知
來源:https://www.javazhiyin.com/28318.html
網上教程具體如下圖
註冊中心,開了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來指定
源碼地址:https://github.com/cuifuan/springcloud-tools
本文參考
https://github.com/happyyangyuan/springcloud-quickstart
http://www.cnblogs.com/hfultrastrong/p/8547236.html