IDEA 微服务单项目多端口启动
网上教程具体如下图
注册中心,开了 N 个端口就创建了 N 个 Module
还有的就是各种创建 eureka 然后互相注册,对于新手来说是很大的误解
以及在 client 去注册的时候,注册中心要写几个
下面开始叙述并实际验证下
当前的技术以及工具
需要你对基本的微服务有一点点的了解,如果不知道什么是微服务,百度基本学习下也不会花很长时间
一步一步创建一个 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);
}
}
快速创建一个 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
启动配置的名字可以自定义
建议带上端口
已经相互注册成功了
接下来我们把注释的开启
# 表示是否注册自身到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
看到这里我们访问的是http://127.0.0.1:8002/
虽然我们配置的是 8000 端口
但还是在 8002 端口注册了,也就是这也是 eureka 互相注册之后达到的高可用的效果,集群,我们可以把 8000 和 8001 端口宕掉,不影响使用
4.源码地址:https://github.com/cuifuan/springcloud-tools
本文参考: