微服务学习笔记一:Spring Cloud简介

1.Spring Cloud是一个工具集:Spring   Cloud是在Spring    Boot的基础上构建的,用于简化分布式系统构建的工具集;使架构师在创建和发布微服务时极为便捷和有效.

Spring Cloud解决分布式中的问题:

项目

详细

No.1

配置管理

No.2

控制总线

No.3

集群管理

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

  

原文地址:https://www.cnblogs.com/brant/p/6298301.html