5分鐘搭建SpringCloud Eureka服務(wù)注冊中心的實(shí)現(xiàn)
創(chuàng)建父級項(xiàng)目 只需保留pom.xml文件
這里只需搭建一個(gè)微服務(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>
<!-- 這里是父級下面控制的子級 -->
<modules>
<!-- 服務(wù)客戶端 -->
<module>cloud-provider-8001</module>
<!-- 注冊中心 -->
<module>cloud-eureka-server9001</module>
</modules>
<!-- 統(tǒng)一管理jar包版本 -->
<!-- 具體這樣 就是為了方便不在選擇jar包版本號 防止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>
搭建注冊中心 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 表示不向注冊中心注冊自己 register-with-eureka: false # false 表示自己就是注冊中心我的職責(zé)就是維護(hù)服務(wù)實(shí)例,并不需去檢查服務(wù) fetch-registry: false service-url: # 集群就是指向其他eureka 單機(jī)就是指向自己 #設(shè)置與Eureka Server交互的地址查詢服務(wù)和注冊服務(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.啟動類
@SpringBootApplication
@EnableEurekaServer
public class Eureka9001 {
public static void main(String[] args) {
SpringApplication.run(Eureka9001.class,args);
}
}
去電腦C:\Windows\System32\drivers\etc 里在hosts 文件

如果找不到,把隱藏文件顯示出來就行了

搭建客戶端 cloud-provider-8001 注冊到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:
# 表示是否將自己注冊到EurekaServer 默認(rèn)true
register-with-eureka: true
service-url:
defaultZone: http://eureka9001.com:9001/eureka/
instance:
instance-id: 8001
prefer-ip-address: true #訪問路徑顯示ip地址
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
3.啟動類
@SpringBootApplication
@EnableEurekaClient
public class DeptMain8001 {
public static void main(String[] args) {
SpringApplication.run(DeptMain8001.class,args);
}
}
測試
是不是很簡單呢 啟動時(shí) 要先啟動注冊中心 再啟動客戶端

這樣就算搭建好啦~!
到此這篇關(guān)于5分鐘搭建SpringCloud Eureka服務(wù)注冊中心的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringCloud Eureka服務(wù)注冊中心內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在IntelliJ IDEA中使用gulp的方法步驟(圖文)
這篇文章主要介紹了在IntelliJ IDEA中使用gulp的方法步驟(圖文),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01
使用GenericObjectPool避免泄漏設(shè)置方法
這篇文章主要為大家介紹了使用GenericObjectPool避免泄漏的設(shè)置方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
客戶端Socket與服務(wù)端ServerSocket串聯(lián)實(shí)現(xiàn)網(wǎng)絡(luò)通信
這篇文章主要為大家介紹了客戶端Socket與服務(wù)端ServerSocket串聯(lián)實(shí)現(xiàn)網(wǎng)絡(luò)通信的內(nèi)容詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-03-03
Java實(shí)現(xiàn)數(shù)據(jù)庫連接池的方法
這篇文章主要介紹了Java實(shí)現(xiàn)數(shù)據(jù)庫連接池的方法,涉及java數(shù)據(jù)庫連接池的創(chuàng)建、連接、刷新、關(guān)閉及狀態(tài)獲取的常用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
Java位運(yùn)算和邏輯運(yùn)算的區(qū)別實(shí)例
Java位運(yùn)算和邏輯運(yùn)算的區(qū)別實(shí)例,請參考下面代碼,希望對你有所幫助2013-02-02
詳解Java如何實(shí)現(xiàn)一個(gè)像String一樣不可變的類
說到?String?大家都知道?String?是一個(gè)不可變的類;雖然用的很多,那不知道小伙伴們有沒有想過怎么樣創(chuàng)建一個(gè)自己的不可變的類呢?這篇文章就帶大家來實(shí)踐一下,創(chuàng)建一個(gè)自己的不可變的類2022-11-11
一個(gè)注解搞定Spring Security基于Oauth2的SSO單點(diǎn)登錄功能
本文主要介紹 同域 和 跨域 兩種不同場景單點(diǎn)登錄的實(shí)現(xiàn)原理,并使用 Spring Security 來實(shí)現(xiàn)一個(gè)最簡單的跨域 SSO客戶端。對Spring Security基于Oauth2的SSO單點(diǎn)登錄功能感興趣的朋友一起看看吧2021-09-09

