5分鐘搭建SpringCloud Eureka服務(wù)注冊(cè)中心的實(shí)現(xiàn)
創(chuàng)建父級(jí)項(xiàng)目 只需保留pom.xml文件
這里只需搭建一個(gè)微服務(wù) 其他操作并無(wú)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tyy.springcloud</groupId>
<artifactId>cloudstudy</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<!-- 這里是父級(jí)下面控制的子級(jí) -->
<modules>
<!-- 服務(wù)客戶端 -->
<module>cloud-provider-8001</module>
<!-- 注冊(cè)中心 -->
<module>cloud-eureka-server9001</module>
</modules>
<!-- 統(tǒng)一管理jar包版本 -->
<!-- 具體這樣 就是為了方便不在選擇jar包版本號(hào) 防止jar包沖突報(bào)錯(cuò) -->
<properties>
<!-- 編碼格式 與JDK1.8 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<!--子模塊繼承之后,提供作用:鎖定版本+子module不用groupId和version-->
<dependencyManagement>
<dependencies>
<!--spring boot 2.2.2-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--spring cloud Hoxton.SR1-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
搭建注冊(cè)中心 cloud-eureka-server9001
首先搭建項(xiàng)目基本就是 寫pom,寫配置…
pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloudstudy</artifactId>
<groupId>com.tyy.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-eureka-server9001</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
aplication.yml
server: port: 9001 eureka: instance: hostname: eureka9001.com #eureka服務(wù)端的實(shí)例名稱 client: # false 表示不向注冊(cè)中心注冊(cè)自己 register-with-eureka: false # false 表示自己就是注冊(cè)中心我的職責(zé)就是維護(hù)服務(wù)實(shí)例,并不需去檢查服務(wù) fetch-registry: false service-url: # 集群就是指向其他eureka 單機(jī)就是指向自己 #設(shè)置與Eureka Server交互的地址查詢服務(wù)和注冊(cè)服務(wù)都需要依賴這個(gè)地址 defaultZone: http://eureka9001.com:9001/eureka/ server: #關(guān)閉自我保護(hù)機(jī)制,保證不可用服務(wù)被及時(shí)踢除 enable-self-preservation: false eviction-interval-timer-in-ms: 2000
3.啟動(dòng)類
@SpringBootApplication
@EnableEurekaServer
public class Eureka9001 {
public static void main(String[] args) {
SpringApplication.run(Eureka9001.class,args);
}
}
去電腦C:\Windows\System32\drivers\etc 里在hosts 文件

如果找不到,把隱藏文件顯示出來(lái)就行了

搭建客戶端 cloud-provider-8001 注冊(cè)到9001
依舊先寫入pom文件
1.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloudstudy</artifactId>
<groupId>com.tyy.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-provider-8001</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
2.application.yml
server:
port: 8001
spring:
application:
name: cloud-dept-service
datasource:
username: root
password: root
url: jdbc:mysql://localhost:3306/db2020?useUnicode=true&characterEncoding-utr-8&useSSL=false
driver-class-name: com.mysql.jdbc.Driver
eureka:
client:
# 表示是否將自己注冊(cè)到EurekaServer 默認(rèn)true
register-with-eureka: true
service-url:
defaultZone: http://eureka9001.com:9001/eureka/
instance:
instance-id: 8001
prefer-ip-address: true #訪問(wèn)路徑顯示ip地址
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
3.啟動(dòng)類
@SpringBootApplication
@EnableEurekaClient
public class DeptMain8001 {
public static void main(String[] args) {
SpringApplication.run(DeptMain8001.class,args);
}
}
測(cè)試
是不是很簡(jiǎn)單呢 啟動(dòng)時(shí) 要先啟動(dòng)注冊(cè)中心 再啟動(dòng)客戶端

這樣就算搭建好啦~!
到此這篇關(guān)于5分鐘搭建SpringCloud Eureka服務(wù)注冊(cè)中心的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringCloud Eureka服務(wù)注冊(cè)中心內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于MockMvc進(jìn)行springboot調(diào)試(SpringbootTest)
這篇文章主要介紹了基于MockMvc進(jìn)行springboot調(diào)試(SpringbootTest),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
springboot集成JWT實(shí)現(xiàn)身份認(rèn)證(權(quán)鑒)的方法步驟
本文主要介紹了springboot集成JWT實(shí)現(xiàn)身份認(rèn)證(權(quán)鑒)的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
淺談java中OO的概念和設(shè)計(jì)原則(必看)
下面小編就為大家?guī)?lái)一篇淺談java中OO的概念和設(shè)計(jì)原則(必看)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05
Mybatis Plus 增刪改查的實(shí)現(xiàn)(小白教程)
本文主要介紹了Mybatis Plus 增刪改查,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
Mybatis基于TypeHandler實(shí)現(xiàn)敏感數(shù)據(jù)加密
業(yè)務(wù)場(chǎng)景中經(jīng)常會(huì)遇到諸如用戶手機(jī)號(hào),身份證號(hào),銀行卡號(hào),郵箱,地址,密碼等等信息,屬于敏感信息,本文就來(lái)介紹一下Mybatis基于TypeHandler實(shí)現(xiàn)敏感數(shù)據(jù)加密,感興趣的可以了解一下2023-10-10
3行代碼快速實(shí)現(xiàn)Spring Boot Oauth2服務(wù)功能
oauthserver是一個(gè)基于Spring Boot Oauth2的完整的獨(dú)立的Oauth服務(wù)器。僅僅需要?jiǎng)?chuàng)建相關(guān)數(shù)據(jù)表,修改數(shù)據(jù)庫(kù)的連接信息,你就可以得到一個(gè)Oauth服務(wù)器。這篇文章給大家介紹3行代碼快速實(shí)現(xiàn)Spring Boot Oauth2服務(wù)功能,需要的朋友參考下吧2018-04-04
@Schedule?如何解決定時(shí)任務(wù)推遲執(zhí)行
這篇文章主要介紹了@Schedule?如何解決定時(shí)任務(wù)推遲執(zhí)行問(wèn)題。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
SpringBoot 自定義+動(dòng)態(tài)切換數(shù)據(jù)源教程
這篇文章主要介紹了SpringBoot 自定義+動(dòng)態(tài)切換數(shù)據(jù)源教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12

