SpringCloud微服務(wù)框架Eureka注冊(cè)中心
Eureka注冊(cè)中心
采用了Client+service的設(shè)計(jì)架構(gòu),它作為服務(wù)注冊(cè)功能的服務(wù)器,它是服務(wù)中心 加載pom依賴
<dependencies>
<!--eureka-server服務(wù)端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<!-- 修改后立即生效,熱部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
創(chuàng)建配置application.yml文件
server:
port: 7001
eureka:
instance:
hostname: localhost #eureka服務(wù)端的實(shí)例名稱
client:
register-with-eureka: false #false表示不向注冊(cè)中心注冊(cè)自己。
fetch-registry: false #false表示自己端就是注冊(cè)中心,我的職責(zé)就是維護(hù)服務(wù)實(shí)例,并不需要去檢索服務(wù)
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #設(shè)置與Eureka Server交互的地址查詢服務(wù)和注冊(cè)服務(wù)都需要依賴這個(gè)地址。
創(chuàng)建啟動(dòng)類
package com.yang.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaServer7001_App {
public static void main(String[] args) {
SpringApplication.run(EurekaServer7001_App.class, args);
}
}
將已經(jīng)有的部門微服務(wù)注冊(cè)Eureka
修改microservicecloud-provider-dept-8001的pom依賴倉(cāng)庫(kù)
<!-- 將微服務(wù)provider側(cè)注冊(cè)進(jìn)eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
修改application.yml配置文件
eureka:
client: #客戶端注冊(cè)進(jìn)eureka服務(wù)列表內(nèi)
service-url:
defaultZone: http://localhost:7001/eureka
在啟動(dòng)類上添加注解
@EnableEurekaClient //本服務(wù)啟動(dòng)后會(huì)自動(dòng)注冊(cè)進(jìn)eureka服務(wù)中
查看端口進(jìn)程
netstat -aon|findstr "9000"
殺死進(jìn)程
taskkill /F /pid 2448
修改microservicecloud-provider-dept-8001的pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
修改父工程pom依賴
<build>
<finalName>microservicecloud</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<delimiters>
<delimit>$</delimit>
</delimiters>
</configuration>
</plugin>
</plugins>
</build>
集群配置
找到C:\Windows\System32\drivers\etc路徑下的hosts文件
127.0.0.1 eureka7001.com 127.0.0.1 eureka7002.com 127.0.0.1 eureka7003.com
修改microservicecloud-provider-dept-8001的application.yml配置文件
eureka:
client: #客戶端注冊(cè)進(jìn)eureka服務(wù)列表內(nèi)
service-url:
# defaultZone: http://localhost:7001/eureka
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
修改microservicecloud-eureka-7001的application.yml配置文件
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com #eureka服務(wù)端的實(shí)例名稱
client:
register-with-eureka: false #false表示不向注冊(cè)中心注冊(cè)自己。
fetch-registry: false #false表示自己端就是注冊(cè)中心,我的職責(zé)就是維護(hù)服務(wù)實(shí)例,并不需要去檢索服務(wù)
service-url:
#單機(jī) defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #設(shè)置與Eureka Server交互的地址查詢服務(wù)和注冊(cè)服務(wù)都需要依賴這個(gè)地址(單機(jī))。
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
評(píng)論
圖片
表情
