注意, 使用SpringBoot2.2.5以上版本時, 運行Eureka要使用Hoxton.SR1以上版本的SpringCloudjava
<?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>SpringCloud</artifactId> <groupId>com.wang</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>SpringCloud-Eureka-7001</artifactId> <!--導包--> <dependencies> <!--Eureka server--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> <version>1.4.6.RELEASE</version> </dependency> <!--熱部署工具--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project>
注意web
server: port: 7001 #Eureka配置 eureka: instance: hostname: localhost #Eureka服務端的實例名稱 client: register-with-eureka: false #表示是否向Eureka註冊中心註冊本身, 因爲咱們這裏是服務端, 不須要註冊本身 fetch-registry: false # 若是 fetch-registry 爲false, 則表示本身爲註冊中心 service-url: #監控頁面 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #這裏使用${}動態配置, hostname和端口在上面定義了
注意spring
此處在resources下的 application.yaml 中配置apache
url使用${}動態配置架構
url最後必須是/eureka/, 千萬不能寫錯, 不然沒法註冊app
下面的配置很重要, 是代表本身是註冊中心的重要配置maven
client: register-with-eureka: false fetch-registry: false
package com.wang.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication //表示他是一個服務端的啓動類, 能夠接受別人註冊進來 @EnableEurekaServer //啓動以後訪問 http://localhost:7001/ 就行了 public class EurekaServer_7001 { public static void main(String[] args) { SpringApplication.run(EurekaServer_7001.class, args); } }
注意ide
這裏是將provider添加到Eureka中spring-boot
因爲consumer是經過RestTemplate訪問url的, 所以不用Eureka管理微服務
<!--Eureka--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>1.4.6.RELEASE</version> </dependency>
注意
在provider中的配置文件中, 添加如下配置
#Eureka配置, 配置該服務註冊到哪裏(與Server中的url地址一致) eureka: client: service-url: defaultZone: http://localhost:7001/eureka/ instance: instance-id: springcloud-provider-dept8001 #修改Eureka上的默認描述信息
注意
url與註冊中心保持一致, 這樣才能註冊到註冊中心去
經過 eureka.instance.instance-id = XXX 能夠修改Eureka上的默認描述信息
package com.wang.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; //啓動類 @SpringBootApplication //在服務啓動後自動將該服務註冊到Eureka中 @EnableEurekaClient public class DeptProvider_8001 { public static void main(String[] args) { SpringApplication.run(DeptProvider_8001.class, args); } }
注意
停掉8001端口的服務,過一段時間後Eureka會顯示如下內容
這是一種保護機制: 好死不如賴活着
它的架構哲學是寧肯保留錯誤的註冊服務信息, 也不盲目註銷任何可能健康的服務實例
可使用 eureka.server.enable-self-preservation = false 來禁用自我保護機制(不推薦關閉!)
爲了方便與多人協同開發, 提供一些服務的信息是十分必要的, 這裏依舊是對provider進行操做
<!--actuator 完善監控信息--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
在application.yaml中, 添加以下的配置
#監控信息配置 info info: app.name: wang-springcloud company.name: wang.study.com
在controller中, 添加獲取服務的內容, 這裏爲了方便查看, 打印到了控制檯
package com.wang.springcloud.controller; import com.wang.springcloud.pojo.Dept; import com.wang.springcloud.service.DeptService; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.web.bind.annotation.*; import java.util.List; //提供RestFul服務! @RestController @ApiModel("Provider Controller") public class DeptController { //註冊DiscoveryClient, 注意此時要導入的包是SpringCloud的 //獲取一些配置的信息, 獲得具體的微服務 @Autowired private DiscoveryClient client; //註冊進來的微服務, 得到一些信息(獲得配置文件中的info的信息) @ApiOperation("微服務的信息") @GetMapping("/dept/discovery") public Object discovery() { //獲取微服務列表的清單 List<String> services = client.getServices(); System.out.println("discovery => services: " + services); //獲得一個具體的微服務, 經過具體的微服務ID, applicationName(即爲在配置文件中配置的該SpringBoot的名字!) List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT"); for (ServiceInstance instance : instances) { System.out.println( instance.getHost() + "\t" + instance.getPort() + "\t" + instance.getUri() + "\t" + instance.getServiceId() ); } //返回這個client就能夠了 return this.client; } }
注意
要自動裝配DiscoveryClient, 要導入的包是SpringCloud的
返回值爲當前的DiscoveryClient對象
微服務的ID爲在配置文件中配置的 spring.application.name 的名字的大寫
info配置後, 點擊status會跳轉, 頁面顯示在配置文件中配置的內容
package com.wang.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; //啓動類 @SpringBootApplication //在服務啓動後自動將該服務註冊到Eureka中 @EnableEurekaClient //服務發現, 這樣就能夠監控了 @EnableDiscoveryClient public class DeptProvider_8001 { public static void main(String[] args) { SpringApplication.run(DeptProvider_8001.class, args); } }
注意
咱們配置多個Eureka註冊中心來註冊服務
集羣的好處: 若是一個註冊中心崩掉, 不會影響整個服務, 只須要切換端口就能夠了
這裏添加了7002和7003兩個端口的註冊中心
因爲是在單機測試, 要修改hosts文件,理由以下
在單機上測試集羣要注意, hostname不能使用同一個localhost, 要在hosts文件中用多個name映射127.0.0.1端口, 這是因爲Eureka會把hostname相同的url移除掉
127.0.0.1 eureka7001.com 127.0.0.1 eureka7002.com 127.0.0.1 eureka7003.com
這裏須要將三個註冊中心經過url兩兩關聯, 以7001端口爲例, 關聯7002和7003端口的註冊中心
server: port: 7001 #Eureka配置 eureka: instance: hostname: localhost #Eureka服務端的實例名稱 client: register-with-eureka: false #表示是否向Eureka註冊中心註冊本身, 因爲咱們這裏是服務端, 不須要註冊本身 fetch-registry: false # 若是 fetch-registry 爲false, 則表示本身爲註冊中心 service-url: #監控頁面 #集羣(與其餘的Eureka關聯) 這裏綁定7002和7003端口的Eureka defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
注意
只須要修改provider發佈的url便可, 將三個註冊中心的url都添加到配置文件中就行了
#Eureka配置, 配置該服務註冊到哪裏(與Server中的url地址一致) eureka: client: service-url: #向集羣發佈, 只須要向全部的Eureka發佈url就能夠了 defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/ instance: instance-id: springcloud-provider-dept8001 #修改Eureka上的默認描述信息
將三個註冊中心以及provider服務都啓動, 最終能夠看到實現了集羣的Eureka