idea快速搭建spring cloud注冊中心與注冊的方法
spring cloud快速搭建
Spring Cloud是一個微服務(wù)框架,它基于spring boot, Spring Cloud提供的全套的分布式系統(tǒng)解決方案 。
首先我們使用gradle來創(chuàng)建:

選擇JDK以及勾選Java,然后下一步

起包名已經(jīng)項目名,下一步:

選擇我們本地的gradle包,一直下一步,點擊build.gradle并添加我們的依賴:
group 'com.gaofei'
version '1.0-SNAPSHOT'
//gradle使用的插件
apply plugin: 'java'
//gradle使用spring-boot打包更方便
apply plugin: 'spring-boot'
//jdk的版本號
sourceCompatibility = 1.8
//本項目的
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
//由于本次創(chuàng)建gradle未出現(xiàn)src,由以下代碼來解決
task "create-dirs" << {
sourceSets*.java.srcDirs*.each {
it.mkdirs()
}
sourcScts*.resources.srcDirs*.each{
it.midirs()
}
}
//編譯構(gòu)建時的配置
buildscript {
ext{
springBootVersion='1.5.10.RELEASE' //springBootVersion是自己定義的變量 里面寫的是springboot插件的版本
}
repositories {
maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
jcenter()
mavenCentral()
maven{ url "http://repo.spring.io/snapshot" }
maven{ url "http://repo.spring.io/milestone" }
maven{ url "http://repo.spring.io/release" }
maven{ url 'http://repo.spring.io/plugins-snapshot' }
}
dependencies{
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")//指的是springboot的一個插件
}
}
//統(tǒng)一所有項目的配置 就是對所有的模塊進行統(tǒng)一配置 所有以后的模塊都不用再配置
allprojects {
group 'com.gaofei' //分組
version '1.0-SNAPSHOT' //版本號
ext{
springCloudVersion='Edgware.SR2'
}
//所有項目都會引用的阿里云里的maven
repositories {
maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
jcenter()
mavenCentral()
maven{ url "http://repo.spring.io/snapshot" }
maven{ url "http://repo.spring.io/milestone" }
maven{ url "http://repo.spring.io/release" }
maven{ url 'http://repo.spring.io/plugins-snapshot' }
}
}
//統(tǒng)一所有子項目的配置
subprojects {
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'spring-boot'
dependencies {
compile('org.springframework.boot:spring-boot-starter-web'){
//使用undertow來代替tomacat
exclude module:"spring-boot-starter-tomcat"
}
//替代tomcat
compile 'org.springframework.boot:spring-boot-starter-undertow'
//健康檢查
compile 'org.springframework.boot:spring-boot-starter-actuator'
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
}
//版本控制插件
dependencyManagement{
imports{
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
}
通過注釋可以看到各個代碼塊的作用,這里我們是用阿里云的倉庫
接下來我們開始建eureka注冊中心,通過new->Module再建gradle項目來創(chuàng)建

在build中添加eureka-server依賴
//表示自己是一個服務(wù)器 compile 'org.springframework.cloud:spring-cloud-starter-eureka-server'
接下來在application.yml中配置
server: port: 8000 spring: application: name: register-center #起個名字 eureka: client: register-with-eureka: false #啟動時不注冊表明自己是一個注冊中心 fetch-registry: false
啟動類
@SpringBootApplication
@EnableEurekaServer//表明自己是注冊中心
public class RegisterCenterProvider {
public static void main(String[] args) {
SpringApplication.run(RegisterCenterProvider.class);
}
}
啟動:

這就表示注冊中心啟動成功
下面創(chuàng)建服務(wù)注冊到服務(wù)中心
創(chuàng)建一個gradle module 項目

在build.gradle中添加thymeleaf組件,eureka客戶端組件的依賴
//thymeleaf組件 compile 'org.springframework.boot:spring-boot-starter-thymeleaf' //eureka客戶端組件 compile 'org.springframework.cloud:spring-cloud-starter-eureka'
在application.yml中配置:
server: port: 8001 spring: application: name: project-shopping-mall #注冊在注冊中心的名字,它會進行鍵值對映射url thymeleaf: cache: false #關(guān)閉緩存 eureka: client: service-url: defaultZone: http://localhost:8000/eureka/ #注冊到注冊中心 instance: prefer-ip-address: true #用兩種方式進行注冊,一種是使用主機名注冊,一種是使用ip地址進行注冊,這里使用ip地址進行注冊
啟動類:
@SpringBootApplication
@EnableDiscoveryClient //表示eureka客戶端
public class ShoppingMallProvider {
public static void main(String[] args) {
SpringApplication.run(ShoppingMallProvider.class);
}
}
啟動:

成功!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用Spring AntPathMatcher的doMatch方法
這篇文章主要介紹了使用Spring AntPathMatcher的doMatch方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
Java實戰(zhàn)之小蜜蜂擴音器網(wǎng)上商城系統(tǒng)的實現(xiàn)
這篇文章主要介紹了如何利用Java實現(xiàn)簡單的小蜜蜂擴音器網(wǎng)上商城系統(tǒng),文中采用到的技術(shù)有JSP、Servlet?、JDBC、Ajax等,感興趣的可以動手試一試2022-03-03
詳解Java編程中protected修飾符與static修飾符的作用
這篇文章主要介紹了Java編程中protected關(guān)鍵字與static關(guān)鍵字的作用,是Java入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2016-01-01

