回顾

Spring Cloud 基于 Spring Boot,通过前几篇

【Java】Spring boot学习笔记01

【Java】Spring boot学习笔记02

【Java】Spring boot学习笔记03

【Java】Spring boot学习笔记04

【Java】Spring boot学习笔记05

我们系统地学习了 Spring Boot 的基础知识,为 Spring Cloud 打下扎实的基础。

什么是 Spring Cloud?

Spring Cloud 是微服务架构的具体代码实现,提供了快速构建分布式系统的工具。Spring Cloud 基于 Spring Boot 框架,它不重复造轮子,而是将第三方实现的微服务应用当成一个模块集成进去。总得来说,Spring Cloud 是一个比Tomcat更加灵活的一个容器。

如何创建一个Spring Cloud项目?

我们先创建一个最简单的 Spring Cloud 项目。

1、新建一个Maven工程并填写GroupId和ArtifactId:
<span class='yzk_title_1416'>【Java】Spring Cloud学习笔记01</span>-Maxchen个人博客

<span class='yzk_title_1416'>【Java】Spring Cloud学习笔记01</span>-Maxchen个人博客

<span class='yzk_title_1416'>【Java】Spring Cloud学习笔记01</span>-Maxchen个人博客

2、接着在pom.xml中添加以下依赖:

<packaging>pom</packaging>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.3.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Greenwich.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
            <exclusions>
            </exclusions>
        </dependency>
    </dependencies>
</dependencyManagement>

3、接着创建服务注册与发现服务模块eurekaserver,在springcloud工程下新建一个Module:

<span class='yzk_title_1416'>【Java】Spring Cloud学习笔记01</span>-Maxchen个人博客

<span class='yzk_title_1416'>【Java】Spring Cloud学习笔记01</span>-Maxchen个人博客

<span class='yzk_title_1416'>【Java】Spring Cloud学习笔记01</span>-Maxchen个人博客

eurekaserver创建成功以后会在springcloud工程下面生成一个目录,且pom.xml的配置会发生一些改变:

<span class='yzk_title_1416'>【Java】Spring Cloud学习笔记01</span>-Maxchen个人博客

<span class='yzk_title_1416'>【Java】Spring Cloud学习笔记01</span>-Maxchen个人博客

4、接下来在eurekaserver中的pom.xml中添加依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
</dependencies>

5、在 eurekaserver 的resources目录下新建配置文件 application.yml,并添加以下内容:

<span class='yzk_title_1416'>【Java】Spring Cloud学习笔记01</span>-Maxchen个人博客

server:
  port: 8761
spring:
  application:
    name: eurekaserver
  profiles:
    active: dev
  cloud:
    inetutils:
      preferred-networks: 127.0.0.1
    client:
      ip-address: 127.0.0.1
eureka:
  server:
    peer-node-read-timeout-ms: 3000
    enable-self-preservation: true
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
  client:
    registerWithEureka: true
    fetchRegistry: false
    healthcheck:
      enabled: true
    serviceUrl:
      defaultZone: http://127.0.0.1:8761/eureka/

6、接下来新建一个启动类 Application.java,并填入如下代码:

<span class='yzk_title_1416'>【Java】Spring Cloud学习笔记01</span>-Maxchen个人博客

import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringCloudApplication
@EnableEurekaServer
public class Application {

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

7、最后我们启动 Application 的 main 方法,浏览器输入:http://localhost:8761,可以看到如图所示界面:

<span class='yzk_title_1416'>【Java】Spring Cloud学习笔记01</span>-Maxchen个人博客

<span class='yzk_title_1416'>【Java】Spring Cloud学习笔记01</span>-Maxchen个人博客

记录你的想法-丰富你的知识
  • <span class='yzk_title_1668'>【Java】Spring Cloud学习笔记05</span>
    【Java】Spring Cloud学习笔记05
  • <span class='yzk_title_1601'>【Java】Spring Cloud学习笔记04</span>
    【Java】Spring Cloud学习笔记04
  • <span class='yzk_title_1492'>【Java】Spring Cloud学习笔记03</span>
    【Java】Spring Cloud学习笔记03
  • <span class='yzk_title_1425'>【Java】Spring Cloud学习笔记02</span>
    【Java】Spring Cloud学习笔记02
  • <span class='yzk_title_1402'>【Java】Spring boot学习笔记05</span>
    【Java】Spring boot学习笔记05
  • <span class='yzk_title_1394'>【Java】Spring boot学习笔记04</span>
    【Java】Spring boot学习笔记04
发表评论

1 条评论