從本章節開始,我們會接觸Spring-Cloud的相關內容,SpringCloud分布式開發有五大利器,
服務發現——Netflix Eureka
客服端負載均衡——Netflix Ribbon
斷路器——Netflix Hystrix
服務網關——Netflix Zuul
分布式配置——spring Cloud Config
這五大利器都是由Netflix公司開發的,并捐贈給了Apache開源組織。
先給大家看一下大概的微服務架構圖,有一個整體的框架概念。
我們會在接下去的章節中接觸這些內容,本章節就開始講注冊中心與服務發現——Netflix Eureka
<strong>注冊中心建立</strong>
1、pom.xml中引入 Eureka相關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、啟動類中引入注解,具體如下:
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配置注冊中心相關屬性,這個文件的存放目錄是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/
一個簡單的注冊中心就創建好了,需要注意,服務啟動后,端口已經修改為1111,所以服務注冊中心的訪問地址是http://localhost:1111/
具體詳見demo <a >chapter3-eureka-server</a>
注冊中心建好了,但是此時還沒有任何的服務注冊上來,下面就來講解一下,怎么把服務注冊到注冊中心——服務發現。
<strong>服務發現</strong>
1、pom.xml文件引入相關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、編寫啟動類,這里和會中類的前面加入@EnableDiscoveryClient注解,服務啟動時通過這個注解就能在注冊中心發現此服務。
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/
這樣,一個服務就提供出去了。需要注意,這里的服務啟動端口是2222。
接下來,我們來啟動一下這個服務,在注冊中心就能看到這個服務,效果如下:
具體詳見demo <a >chapter3-cqf-service</a>
你喜歡就是我最大的動力——cqf