初探Spring Cloud(一)

1.  什麼是Spring Cloud?java

Spring提供了一系列工具,能夠幫助開發人員迅速搭建分佈式系統中的公共組件(好比:配置管理,服務發現,斷路器,智能路由,微代理,控制總線,一次性令牌,全局鎖,主節點選舉, 分佈式session, 集羣狀態)。協調分佈式環境中各個系統,爲各種服務提供模板性配置。使用Spring Cloud, 開發人員能夠搭建實現了這些樣板的應用,而且在任何分佈式環境下都能工做得很是好,小到筆記本電腦, 大到數據中心和雲平臺。web

Spring Cloud官網的定義比較抽象,咱們能夠從簡單的東西開始。Spring Cloud是基於Spring Boot的, 最適合用於管理Spring Boot建立的各個微服務應用。要管理分佈式環境下的各個Spring Boot微服務,必然存在服務的註冊問題。因此咱們先從服務的註冊談起。既然是註冊,必然有個管理註冊中心的服務器,各個在Spring Cloud管理下的Spring Boot應用就是須要註冊的clientspring

Spring Cloud使用erureka server,  而後全部須要訪問配置文件的應用都做爲一個erureka client註冊上去。eureka是一個高可用的組件,它沒有後端緩存,每個實例註冊以後須要向註冊中心發送心跳,在默認狀況下erureka server也是一個eureka client ,必需要指定一個 server。apache

2.  建立Eureka Serverbootstrap

1).建立一個Maven工程helloworld.eureka.server, pom.xml內容以下:後端

<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.cloud</groupId>
	<artifactId>springcloud.helloworld.eureka.server</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springcloud.helloworld.Eureka.server</name>
	<description>Demo Spring Eureka Server</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.3.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>
     </properties>
 
     <dependencies>
         <!--eureka server -->
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-eureka</artifactId>
         </dependency>
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-eureka-server</artifactId>
         </dependency>
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-config</artifactId>
         </dependency>
         <!-- spring boot test-->
         <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>Dalston.RC1</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>
 
     <repositories>
         <repository>
             <id>spring-milestones</id>
             <name>Spring Milestones</name>
             <url>https://repo.spring.io/milestone</url>
             <snapshots>
                 <enabled>false</enabled>
             </snapshots>
         </repository>
     </repositories>
</project>

2). 用Spring Boot建立一個服務類EurekaServerApplication,須要一個註解@EnableEurekaServer加在springboot工程的啓動類上緩存

package springcloud.helloworld.eureka.server;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(EurekaServerApplication.class, args);
	}
}

3).eureka server的配置文件bootstrap.yml,其中registerWithEureka:false和fetchRegistry:false代表本身是一個eureka serverspringboot

server:
   port: 8761服務器

eureka:
   instance:
       hostname: 127.0.0.1
   client:
       registerWithEureka: false
       fetchRegistry: false
       serviceUrl:
           defaultZone: http://127.0.0.1:8761/eureka/session

 

4) eureka server的工程結構以下

server:
   port: 8761

eureka:
   instance:
       hostname: 127.0.0.1
   client:
       registerWithEureka: false
       fetchRegistry: false
       serviceUrl:
           defaultZone: http://127.0.0.1:8761/eureka/

5)啓動eureka server,而後訪問http://localhost:8761, 界面以下, "No instances available" 表示無client註冊

3.  建立Eureka Client

1). 建立一個Maven工程helloworld.eureka.client, pom.xml內容以下:

<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.cloud</groupId>
	<artifactId>springcloud.helloworld.eureka.client</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springcloud.helloworld.eureka.client</name>
	<description>Demo Spring Boot Client</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.3.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>
	</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-web</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>Dalston.RC1</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>

	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>
</project>

2).  建立主類EurekaClientApplication

package springcloud.helloworld.eureka.client;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {

	public static void main(String[] args) {
		SpringApplication.run(EurekaClientApplication.class, args);
	}

	@Value("${server.port}")
	String port;

	@RequestMapping("/")
	public String home() {
		return "hello world from port " + port;
	}
}

3) eureka client的配置文件bootstrap.yml

eureka:
    client:
        serviceUrl:
            defaultZone: http://localhost:8761/eureka/
server:
    port: 8762
spring:
    application:
        name: service-helloworld

4). Client啓動後, 能夠訪問http://localhost:8762

5). 再次訪問服務器端口, 能夠看到Service Helloworld已經自動註冊到以前的server中

相關文章
相關標籤/搜索