從本章節(jié)開始,我們會(huì)接觸Spring-Cloud的相關(guān)內(nèi)容,SpringCloud分布式開發(fā)有五大利器,
服務(wù)發(fā)現(xiàn)——Netflix Eureka
客服端負(fù)載均衡——Netflix Ribbon
斷路器——Netflix Hystrix
服務(wù)網(wǎng)關(guān)——Netflix Zuul
分布式配置——spring Cloud Config
這五大利器都是由Netflix公司開發(fā)的,并捐贈(zèng)給了Apache開源組織。
先給大家看一下大概的微服務(wù)架構(gòu)圖,有一個(gè)整體的框架概念。

我們會(huì)在接下去的章節(jié)中接觸這些內(nèi)容,本章節(jié)就開始講注冊(cè)中心與服務(wù)發(fā)現(xiàn)——Netflix Eureka
<strong>注冊(cè)中心建立</strong>
1、pom.xml中引入 Eureka相關(guān)jar包
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2、啟動(dòng)類中引入注解,具體如下:
package com.cqf.chapter3;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
*
* @author qifu.chen
* @version 1.0.0
* @date May 16, 2017 3:11:16 PM
*/
@EnableEurekaServer
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
3、application.properties配置注冊(cè)中心相關(guān)屬性,這個(gè)文件的存放目錄是src.main.resources
server.port=1111
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
一個(gè)簡(jiǎn)單的注冊(cè)中心就創(chuàng)建好了,需要注意,服務(wù)啟動(dòng)后,端口已經(jīng)修改為1111,所以服務(wù)注冊(cè)中心的訪問地址是http://localhost:1111/

具體詳見demo <a >chapter3-eureka-server</a>
注冊(cè)中心建好了,但是此時(shí)還沒有任何的服務(wù)注冊(cè)上來,下面就來講解一下,怎么把服務(wù)注冊(cè)到注冊(cè)中心——服務(wù)發(fā)現(xiàn)。
<strong>服務(wù)發(fā)現(xiàn)</strong>
1、pom.xml文件引入相關(guān)jar包
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2、編寫啟動(dòng)類,這里和會(huì)中類的前面加入@EnableDiscoveryClient注解,服務(wù)啟動(dòng)時(shí)通過這個(gè)注解就能在注冊(cè)中心發(fā)現(xiàn)此服務(wù)。
package com.cqf.chapter3;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
*
* @author qifu.chen
* @version 1.0.0
* @date May 18, 2017 3:33:55 PM
*/
@EnableDiscoveryClient
@SpringBootApplication
class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
3、配置文件如下
spring.application.name=cqf-service
server.port=2222
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
這樣,一個(gè)服務(wù)就提供出去了。需要注意,這里的服務(wù)啟動(dòng)端口是2222。
接下來,我們來啟動(dòng)一下這個(gè)服務(wù),在注冊(cè)中心就能看到這個(gè)服務(wù),效果如下:

具體詳見demo <a >chapter3-cqf-service</a>
你喜歡就是我最大的動(dòng)力——cqf