微服務學習筆記一:Spring Cloud簡介

1.Spring Cloud是一個工具集:Spring   Cloud是在Spring    Boot的基礎上構建的,用於簡化分佈式系統構建的工具集;使架構師在建立和發佈微服務時極爲便捷和有效.java

 

Spring Cloud解決分佈式中的問題:web

項目spring

詳細apache

No.1瀏覽器

配置管理安全

No.2springboot

控制總線服務器

No.3架構

集羣管理app

No.4

安全機制

No.5

Session管理

No.6

Failback

No.7

智能路由

No.8

網關管理

No.9

服務管理(服務發現/服務註冊等)

 

2.Spring Boot簡介

Spring Boot能夠幫助開發者更容易地建立基於Spring的應用程序和服務。

Spring Boot的做用在於建立和啓動新的基於Spring框架的項目。

Spring Boot會選擇最適合的Spring子項目和第三方開源庫進行整合。

大部分Spring Boot應用只須要很是少的配置就能夠快速運行起來。

Spring Boot包含的特性以下

   建立能夠獨立運行的Spring應用。

  直接嵌入Tomcat或Jetty服務器,不須要部署WAR文件。

  提供推薦的基礎POM文件來簡化Apache Maven配置。

  儘量的根據項目依賴來自動配置Spring框架。

  提供能夠直接在生產環境中使用的功能,如性能指標、應用信息和應用健康檢查。

  沒有代碼生成,也沒有XML配置文件。

服務發現和智能路由

 

3. Spring Boot入門: Hello World

建一個空的MAVEN項目myproject

POM.xml

<?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.example</groupId>
    <artifactId>myproject</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <!--使用最新的spring-boot版本 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.RELEASE</version>
    </parent>

    <dependencies><!--web應用基本環境配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <!-- Package as an executable jar -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <!-- Add Spring repositories -->
    <!-- (you don't need this if you are using a .RELEASE version) -->
    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/snapshot</url>
            <snapshots><enabled>true</enabled></snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <url>http://repo.spring.io/milestone</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/snapshot</url>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <url>http://repo.spring.io/milestone</url>
        </pluginRepository>
    </pluginRepositories>

</project>

發佈服務

package com.springboot.test;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@EnableAutoConfiguration
public class Example {

    @RequestMapping("/hello1")
    String home() {
        return "Hello World!";
    }

    @RequestMapping("/hello2/{myName}")
    String index(@PathVariable String myName) {
        return "Hello "+myName+"!!!";
    }
}

 

啓動類

package com.springboot.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class Application {

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

 

測試

在瀏覽器中輸入:

http://localhost:8080/hello1

 

http://localhost:8080/hello2/chenxiaobing

相關文章
相關標籤/搜索