1.1:pom.xml中引入依賴git
(1)引入spring boot依賴github
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.3.RELEASE</version> </parent>
(2)引入spring cloud依賴web
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR4</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
(3)引入eurekaspring
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies>
(4)添加spring-boot的maven插件app
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
(5)application.yml配置maven
server: port: 8051 eureka: client: fetch-registry: false #是否註冊本身,當沒有其餘節點啓動時,因改成false,不註冊本身 register-with-eureka: false #同上 healthcheck: enabled: true serviceUrl: defaultZone: http://localhost:8052/eureka/ #若是是單節點 應當寫的是本身 http://localhost:8051/eureka/ server: enable-self-preservation: false eviction-interval-timer-in-ms: 1000 #刷新時間 instance: hostname: sever1
(5)新建項目重複(1)-(3)並修改application.ymlspring-boot
server: port: 8052 eureka: client: fetch-registry: true register-with-eureka: true healthcheck: enabled: true serviceUrl: defaultZone: http://localhost:8051/eureka/ server: enable-self-preservation: false eviction-interval-timer-in-ms: 1000 instance: hostname: sever2
(6)先啓動server1,在啓動server2,訪問localhost:8051,localhost:8052,出現了相同的結果fetch
能夠看到只有8052端口註冊上了,並無8051這個端口ui
分析一下:server1啓動時this
eureka.client.serviceUrl.defaultZone=http://localhost:8052/eureka/
這個屬性意思是將本身做爲一個服務註冊到8052上,可是如今8052並無啓動,因此server1沒有註冊到server2上,又由於啓動時本身沒有註冊本身,因此無論是在server1仍是server2上都沒有發現8051這個的端口,那麼將server1的application.yml修改
eureka.client.fetch-registry=true eureka.client..register-with-eureka=true
從新啓動server1,那麼這個時候server1和server2就相互註冊造成了一個雙節點的eureka集羣
通過以上了解發現了個問題,先要啓動server1,在啓動server2,而後修改server1的配置,重啓纔會互相註冊造成集羣。由此思考,若是須要添加一個節點server3,你那麼要同時修改各自的配置文件
server1
eureka.client.serviceUrl.defaultZone=http://localhost:8052/eureka/,http://localhost:8053/eureka/
server2
eureka.client.serviceUrl.defaultZone=http://localhost:8051/eureka/,http://localhost:8053/eureka/
而後重啓,那麼有什麼辦法使改變了配置文件,不重啓服務就能生效呢?
首先咱們知道在項目啓動時會加載配置文件的信息,將配置文件的信息存儲到了內存裏。之後進程在內存裏讀取,因此在不重啓的狀況下,改變了配置文件的參數進程並不知道改變了,由於修改的參數沒有放進內存裏。
通過百度有許多解決方法,可是不是過於複雜,就是複雜,又是註解,又是反射。這篇博文簡單耐操的解決了這個問題
https://blog.csdn.net/qq_27385301/article/details/82716218
增長一個controller
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.cloud.context.refresh.ContextRefresher; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RefreshScope public class ConfigClientController { @Value("${eureka.client.serviceUrl.defaultZone}") private String profile; @Autowired private ContextRefresher contextRefresher; @GetMapping("/profile") public String hello() { contextRefresher.refresh(); return this.profile; } }
修改配置文件以後訪問 /profile,執行 contextRefresher.refresh();就將配置文件從新加載到內存裏,不重啓就生效了。
爲了更方便的管理配置文件能夠啓用spring cloud config對配置文件進行統一管理
搭建spring cloud config server
(1)引入依賴,其餘同上
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies>
(2)啓動類加入註解
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
(3)配置文件
server: port: 7001 spring: application: name: framework-config-server profiles: active: native #本地 cloud: config: server: native: search-locations: classpath:/config #git: #uri: http://123.206.58.154:3000/admintelecom/framework.git #username: #password:
(4)將server1和server2的配置文件複製進resources/config下,並啓動
(5)將server1和server2的application.yml清空,並新建bootstrat.yml
spring: application: name: product-eureka-server cloud: config: name: eureka-server1 #eureka-server2 這個是配置中心的配置文件 好比是eureka-server1-dev.yml uri: http://localhost:7001/ profile: dev #文件的後綴
(6)啓動server1,啓動server2,修改config server上的eureka-server1-dev.yml使server1註冊,而後訪問http://server1的地址/profile刷新配置
這樣就實現了不重啓eureka節點在線擴容的功能
源碼:https://github.com/DisMirror/shooter