Spring Cloud簡(jiǎn)介
Spring Cloud是一個(gè)基于Spring Boot實(shí)現(xiàn)的云應(yīng)用開(kāi)發(fā)工具擒贸,它為基于JVM的云應(yīng)用開(kāi)發(fā)中的配置管理干旁、服務(wù)發(fā)現(xiàn)盖喷、斷路器、智能路由雷绢、微代理臂拓、控制總線、全局鎖习寸、決策競(jìng)選胶惰、分布式會(huì)話和集群狀態(tài)管理等操作提供了一種簡(jiǎn)單的開(kāi)發(fā)方式。
Spring Cloud包含了多個(gè)子項(xiàng)目(針對(duì)分布式系統(tǒng)中涉及的多個(gè)不同開(kāi)源產(chǎn)品)霞溪,比如:Spring Cloud Config孵滞、Spring Cloud Netflix、Spring Cloud CloudFoundry鸯匹、Spring Cloud AWS坊饶、Spring Cloud Security、Spring Cloud Commons殴蓬、Spring Cloud Zookeeper匿级、Spring Cloud CLI等項(xiàng)目。
微服務(wù)架構(gòu)
“微服務(wù)架構(gòu)”在這幾年非常的火熱染厅,以至于關(guān)于微服務(wù)架構(gòu)相關(guān)的產(chǎn)品社區(qū)也變得越來(lái)越活躍(比如:netflix痘绎、dubbo),Spring Cloud也因Spring社區(qū)的強(qiáng)大知名度和影響力也被廣大架構(gòu)師與開(kāi)發(fā)者備受關(guān)注肖粮。
那么什么是“微服務(wù)架構(gòu)”呢孤页?簡(jiǎn)單的說(shuō),微服務(wù)架構(gòu)就是將一個(gè)完整的應(yīng)用從數(shù)據(jù)存儲(chǔ)開(kāi)始垂直拆分成多個(gè)不同的服務(wù)涩馆,每個(gè)服務(wù)都能獨(dú)立部署行施、獨(dú)立維護(hù)允坚、獨(dú)立擴(kuò)展,服務(wù)與服務(wù)間通過(guò)諸如RESTful API的方式互相調(diào)用蛾号。
對(duì)于“微服務(wù)架構(gòu)”稠项,大家在互聯(lián)網(wǎng)可以搜索到很多相關(guān)的介紹和研究文章來(lái)進(jìn)行學(xué)習(xí)和了解。也可以閱讀始祖Martin Fowler的《Microservices》鲜结,本文不做更多的介紹和描述皿渗。
服務(wù)注冊(cè)與發(fā)現(xiàn)
在簡(jiǎn)單介紹了Spring Cloud和微服務(wù)架構(gòu)之后,下面回歸本文的主旨內(nèi)容轻腺,如何使用Spring Cloud搭建服務(wù)注冊(cè)與發(fā)現(xiàn)模塊。
這里我們會(huì)用到Spring Cloud Netflix划乖,該項(xiàng)目是Spring Cloud的子項(xiàng)目之一贬养,主要內(nèi)容是對(duì)Netflix公司一系列開(kāi)源產(chǎn)品的包裝,它為Spring Boot應(yīng)用提供了自配置的Netflix OSS整合琴庵。通過(guò)一些簡(jiǎn)單的注解误算,開(kāi)發(fā)者就可以快速的在應(yīng)用中配置一下常用模塊并構(gòu)建龐大的分布式系統(tǒng)。它主要提供的模塊包括:服務(wù)發(fā)現(xiàn)(Eureka)迷殿,斷路器(Hystrix)儿礼,智能路有(Zuul),客戶端負(fù)載均衡(Ribbon)等庆寺。
所以蚊夫,我們這里的核心內(nèi)容就是服務(wù)發(fā)現(xiàn)模塊:Eureka。下面我們動(dòng)手來(lái)做一些嘗試懦尝。
1.創(chuàng)建服務(wù)注冊(cè)中心
1) 創(chuàng)建一個(gè)基礎(chǔ)的Spring Boot工程知纷,pom.xml內(nèi)容如下:
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.shijie</groupId>
<artifactId>springcloudserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springcloudserver</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</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>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2)在application.yml文件中添加以下內(nèi)容
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
3)在默認(rèn)的${projectName}Application.java文件中添加@EnableEurekaServer注解
@EnableEurekaServer
@SpringBootApplication
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
在瀏覽器輸入localhost:8761
2.創(chuàng)建提供服務(wù)的客戶端
1) 創(chuàng)建一個(gè)基礎(chǔ)的Spring Boot工程,pom.xml內(nèi)容如下:
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.shijie</groupId>
<artifactId>springcloudclient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>springCloudClient</name>
<description>client project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.SR2</spring-cloud.version>
</properties>
<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>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2)在application.yml文件中添加以下內(nèi)容(指定服務(wù)為service-hi)
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8763
spring:
application:
name: service-hi
3)在默認(rèn)的${projectName}Application.java文件中添加以下內(nèi)容的注解
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient //這里可以打印log
@RestController
public class SpringCloudClientApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudClientApplication.class, args);
}
}
4)一個(gè)帶日志的demo之controller
新建ComputerContrller類陵霉,內(nèi)容如下:
package com.shijie.controller;
import org.apache.log4j.Logger;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by shijie on 2017/8/24 0024.
*/
@RestController
public class ComputerController {
private final Logger logger = Logger.getLogger(getClass());
@Autowired
private DiscoveryClient client;
@RequestMapping(value = "/add" ,method = RequestMethod.GET)
public Integer add(@RequestParam Integer a, @RequestParam Integer b) {
ServiceInstance instance = client.getLocalServiceInstance();
Integer r = a + b;
logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r);
return r;
}
}
在瀏覽器輸入localhost:8763,然后訪問(wèn)或刷新之前的localhost:8761可以看到有一個(gè)新的服務(wù)service-hi
訪問(wèn)localhost:8763/add
可以在console控制臺(tái)可以看到日志琅轧,在瀏覽器可以看到打印3