Spring Cloud Eureka實現服務治理
阿新 • • 發佈:2018-12-17
一、建立服務註冊中心專案 registrycenter
Next,選擇依賴的類庫Eureka Server,Spring Boot版本選擇最新,Next,Finish
專案建立完成,在主類上新增註解@EnableEurekaServer
在application.properties檔案中增加配置資訊
#註冊中心服務ID spring.application.name=eureka-server #埠號 server.port=1111 eureka.instance.hostname=127.0.0.1 #是否將自己註冊到Eureka服務中心,預設為true eureka.client.register-with-eureka=false #是否從Eureka服務中心獲取註冊資訊 eureka.client.fetch-registry=false #設定與Eureka服務中心互動地址,查詢和註冊服務都需要依賴這個地址 eureka.client.service-url.defaultZone=http://localhost:${server.port}/eureka/
Eureka頁面顯示正常,註冊中心建立成功。
二、建立服務專案MyServiceServer,在註冊中心進行註冊
Next,選擇類庫Eureka Discovery和SpringBoot版本,Next,Finish
專案建立完成,在啟動類上添加註解@EnableDiscoveryClient,宣告這是一個eureka client,否則不會進行服務註冊
在application.properties配置檔案增加配置資訊
#服務名稱 spring.application.name=eureka-my-service #埠號 server.port=2222 #在註冊中心進行註冊 eureka.client.serviceUrl.defaultZone=http://127.0.0.1:1111/eureka/
執行專案失敗,在控制檯發現以下資訊:
Invocation of destroy method failed on bean with name 'scopedTarget.eurekaClient': org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'eurekaInstanceConfigBean': Singleton bean creation not allowed while singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)
因為Eureka client不包含tomcat依賴,spring容器無法建立一些例項,導致專案無法啟動,需要在pom檔案裡面加上依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
啟動專案成功,再次訪問 http://127.0.0.1:1111/
頁面上可以看到eureka-my-service服務,表示服務註冊成功
在MyServiceServer專案中,新增業務類DispatcherController:
package com.tansky.cloud.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DispatcherController {
@RequestMapping("/test")
public String test(){
return "my service";
}
}
三、建立客戶端專案MyServiceClient
Next,選擇依賴的類庫Eureka Discovery和Feign,Next,Finish
專案建立完成,在啟動類上增加註解@EnableDiscoveryClient和@EnableFeignClients,宣告可以註冊和進行遠端服務呼叫
在application.properties配置檔案中增加配置:
spring.application.name=eureka-my-client
server.port=80
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:1111/eureka/
同樣不要忘記在pom檔案裡面加上依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
在MyServiceClient專案中,新增服務訪問類MyClient
package com.tansky.cloud.client;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient("eureka-my-service")
public interface MyClient {
@GetMapping("/test")
String test();
}
@FeignClient(“eureka-my-service”)括號中的字串要與在註冊中心註冊的名稱一致eureka-my-service
在MyServiceClient專案中,新增業務類DispatcherController呼叫服務訪問類MyClient:
package com.tansky.cloud.controller;
import com.tansky.cloud.client.MyClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DispatcherController {
@Autowired
private MyClient myClient;
@RequestMapping("/test")
public String test(){
return myClient.test();
}
}