前面幾篇博客中我們大概講解了一下spring boot的知識,話說管理這些微服務的架構就構成了spring cloud,我們一樣,先不說原理先搭建出來再說;
我們今天主要搭建三個服務
1.註冊中心(Netflix Eureka):即所有的微服務都得上註冊中心中註冊才能進行管理
2.網關(Netflix Zuul):路由配置(攔截過濾)
3.正常服務:我們這兒就先搭建一個
一、註冊中心
先搭一套spring boot的框架,具體的流程就不說了,之前講的很明白了,然後我們給這套框架加配置configuration;
只需要在spring boot的基礎上添加依賴包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
後面這個依賴是爲了管理spring cloud的所有依賴包方便加的,要是不想加也可以,就在上面依賴包上加一個版本號就可以了。
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
然後我們在啓動文件application.java上添加
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args){
new SpringApplicationBuilder(EurekaApplication.class).web(true).run(args);
}
}
注意添加的註解@EnableEurekaServer
還有一個是@EnableEurekaClient
,注意別添加錯了。
然後在application.properties中添加
server.port=1111
eureka.server.eviction-interval-timer-in-ms=5000
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.server.enable-self-preservation=false
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
其實只有最後一行是必須的,其它的都是配置參數,視具體的環境而加;
這樣,啓動工程,然後訪問:http://localhost:1111/,我們會看到啓動頁面:
好了,我們接下來配置gateway
二、GateWay
一樣,我們先看依賴
這個依賴ZUUL是gateway的核心部分,主要靠這個包來路由
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
但是我們爲了把該服務也註冊到註冊中心上,我們就需要添加註冊的依賴包,看着和註冊中心的挺像吧,其實並非一個包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
然後我們在啓動文件上添加:
@EnableZuulProxy
@SpringCloudApplication
public class GateWayApplication {
public static void main(String[] args){
new SpringApplicationBuilder(GateWayApplication.class).web(true).run(args);
}
}
該註解@EnableZuulProxy
就是將spring boot服務配置爲網關的關鍵。
然後我們在application.properties中添加
我們看到後面三行就是將該服務註冊到註冊中心上,
而前面這個是幹嘛的呢?
這兒就是路由的核心,我們把三的服務test1的路徑配置爲/test1/**,既可以用url,也可以用服務中聲明的spring.application.name=test1
當做serviceId來映射
#routes to url
zuul.routes.test1.path=/test1/**
zuul.routes.test1.url=http://localhost:3333/
#zuul.routes.test1.serviceId=test1
eureka.instance.leaseRenewalIntervalInSeconds=2
eureka.instance.leaseExpirationDurationInSecondes=6
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
這樣我們啓動服務,然後看一下注冊中心的管理頁面,會發現該服務出現在了管理頁面上了;
三、正常的spring boot不說了,我們用這個配置
server.port=3333
spring.application.name=test1
eureka.instance.leaseRenewalIntervalInSeconds=2
eureka.instance.leaseExpirationDurationInSecondes=6
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
注意添加註冊中心的依賴包哦!!!
最後我們會看到註冊中心的管理頁面上會出現兩個服務;
路由的話
我們的test1服務如果用:http://localhost:3333/getMsg可以訪問的話,我們路由之後,url替換爲http://localhost:2222/test1/getMsg訪問;
之後我會具體的把配置還有斷路器的知識講講,未完待續。。。