Eureka注冊與發現
1,eureka基礎知識
1,什麼是服務治理:
SpringCloud分裝了Netflix公司開發的Eureka子產品來實作服務治理
在傳統的rpc遠端調用架構中,管理每個服務與服務之間依賴關系比較複雜
,管理比較複雜,是以需要使用服務治理,管理服務于服務之間依賴關系,
可以實作服務調用,負載均衡,容錯,實作服務發現于注冊
1,什麼是服務注冊與發現:
Eureka采用了CS的設計架構,EurekaServer作為服務注冊功能的伺服器,
他是服務注冊中,而系統中的其他微服務,
使用Eureka的用戶端連接配接到Eureka Server并維持心跳連接配接,
這樣系統的維護人員就可以通過Eureka Server來監控系統中各各微服務是否正常運作,
在服務注冊與發現中,有一個注冊中心,當伺服器啟動時,
會把目前自己伺服器的資訊,比如服務位址通訊位址等以别名方式注冊到注冊中心上。
另一方(消費者|服務提供者)以别名的方式去注冊中心上擷取實際的服務通訊位址,
然後在實作本地RPC調用RPC遠端調用架構核心思想:在注冊中心,
因為使用注冊中心管理每個服務與服務之間的一個依賴關系(服務治理概念)。
在任何rpc遠端架構中,都會有一個注冊中心(存放服務位址相關資訊(接口位址))
SpringCloud-eureka(單機版)Eureka注冊與發現1,單機Eureka建構步驟2,把提供者(cloud-provider-payment8001)注冊進Eureka3,把消費者(cloud-consumer-order80)注冊進Eureka
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiAzNfRHLGZkRGZkRfJ3bs92YsYTMfVmepNHLycGRPlXSU1UMNpmTzM2MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZwpmL5UTN5MTO1MTM1ADOwAjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
1,單機Eureka建構步驟
1>建構Module cloud-eureka-server7001
1>該pom檔案
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>com.atgugu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
1>該YML檔案
server:
port: 7001
eureka:
instance:
hostname: localhost #eureka服務端的執行個體名字
client:
register-with-eureka: false #表識不向注冊中心注冊自己
fetch-registry: false #表示自己就是注冊中心,職責是維護服務執行個體,并不需要去檢索服務
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #設定與eureka server互動的位址查詢服務和注冊服務都需要依賴這個位址
1>在主啟動類上加兩個注解
@EnableEurekaServer
@SpringBootApplication
1>測試,運作主啟動類通路:http://localhost:7001/
2,把提供者(cloud-provider-payment8001)注冊進Eureka
2>先建構一個module(cloud-provider-payment8001)
2>該pom檔案
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2>寫yml
server:
port: 8001
spring:
application:
name: cloud-payment-service
eureka:
client:
register-with-eureka: true
fetchRegistry: true
service-url:
defaultZone: http://localhost:7001/eureka
2>主啟動類上加注解
@EnableEurekaClient
@SpringBootApplication
2>測試
1先啟動注冊中心7001,在啟動8001
2通路http://localhost:7001/
3,把消費者(cloud-consumer-order80)注冊進Eureka
3>建立一個module(cloud-consumer-order80)
3>改pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
3>寫yml
server:
port: 80
spring:
application:
name: cloud-order-service
eureka:
client:
register-with-eureka: true
fetchRegistry: true
service-url:
defaultZone: http://localhost:7001/eureka
3>主啟動類
@EnableEurekaClient
@SpringBootApplication
3>測試
1,按順序啟動7001,8001,80
2,通路Eureka7001
3,在裡面Server,Controller層寫一個接口用于測試(就不粘貼代碼了)POSTMAN通路:http://localhost/consumer/payment/get/31
出現資料者說明消費者,調用服務提供者成功