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?Boot如何限制在一分鐘內(nèi)某個IP只能訪問10次
有些時候,為了防止我們上線的網(wǎng)站被攻擊,或者被刷取流量,我們會對某一個ip進行限制處理,這篇文章,我們將通過Spring?Boot編寫一個小案例,來實現(xiàn)在一分鐘內(nèi)同一個IP只能訪問10次,感興趣的朋友一起看看吧2023-10-10
用Java代碼實現(xiàn)棧數(shù)據(jù)結(jié)構(gòu)的基本方法歸納
這篇文章主要介紹了用Java代碼實現(xiàn)棧數(shù)據(jù)結(jié)構(gòu)的基本方法歸納,各種算法的實現(xiàn)也是ACM上經(jīng)常出現(xiàn)的題目,是計算機學(xué)習(xí)的基本功,需要的朋友可以參考下2015-08-08
解決使用json-lib包實現(xiàn)xml轉(zhuǎn)json時空值被轉(zhuǎn)為空中括號的問題
網(wǎng)上能查到的xml轉(zhuǎn)json的jar包大部分是net.sf.json-lib,但是JSON json =xmlSerializer.read(xml); 方法會出現(xiàn)將空值轉(zhuǎn)化為[]的問題,下面為大家提供兩種解決方法2018-03-03
JDBC中Statement和Preparement的使用講解
今天小編就為大家分享一篇關(guān)于JDBC中Statement和Preparement的使用講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01

