45分钟

SpringCloud开发实战

实验预计耗时:45分钟

1. 课程背景

1.1 课程目的

Spring Cloud是基于Spring Boot 提供的一套微服务解决方案,Spring Cloud利用Spring Boot的开发便利性,巧妙的简化了分布式系统基础设施的开发,为开发人员提供了快速构建分布式系统的工具。Spring Cloud的核心是服务注册与发现,在使用整套Spring Cloud的功能时,都需要先把开发好的服务注册到服务注册中心。

本实验将带领学员完整地开发一个基于Spring Cloud的微服务应用程序,旨在帮助学员快速掌握Spring Cloud微服务架构的开发流程。

1.2 课前知识准备

学习本课程前,学员需要掌握以下前置知识:

1、能力基础

  • Java编程基础:包括数据结构、逻辑语句、函数、注解、面向对象编程以及web开发基础。
  • Spring开发基础:了解Spring框架的基本架构和开发思想。

2、相关概念

  • 微服务:微服务架构是一项在云中部署应用和服务的新技术。微服务可以在“自己的程序”中运行,并通过“轻量级设备与HTTP型API进行沟通”。
  • 注册中心:注册中心可以说是微服务架构中的“通讯录”,它记录了服务和服务地址的映射关系。在分布式架构中,服务会注册到这里,当服务需要调用其它服务时,就到这里找到服务的地址,进行调用。
image004

3、相关技术

  • Maven:Maven是一个项目管理工具,我们通过创建Maven工程快速获取项目所需要的jar包,并保存在本地仓库,也可以帮助我们对自己的项目进行生成jar包等项目管理操作。
  • Spring Boot:Spring Boot是一款流行的Java开发框架,其设计目的是简化Spring应用的初始搭建以及开发过程,使开发人员不再需要定义样板化的配置。其开发流程大致如下:
image005
  • Spring Cloud: Spring Cloud是基于Spring Boot实现微服务的框架,其核心是服务的注册与发现,Spring Cloud默认使用Eureka作为服务注册中心。

2. 实验环境

2.1 实验操作环境

本课程需要以下实验操作环境:

  • 可以接入互联网的笔记本电脑或者台式机,包括但不限于Windows、MAC系统
  • 安装浏览器,包括但不限于谷歌浏览器、火狐浏览器
  • 实验环境:计算机本地
  • Java软件开发工具包JDK(版本:1.8)
  • Maven(版本:3.5及以上)
  • Eclipse或者IDEA,此实验采用Eclipse作为开发工具

2.2 实验架构图

本实验搭建的Spring Cloud微服务的架构如下,共包含三个部分:注册中心Eureka,服务提供者Provider以及服务消费者Consumer。

image006

2.3 实验的数据规划表

资源名称

下载地址

Eclipse

下载Eclipse

JDK

下载JDK

Maven

下载Maven

3. 实验流程

image007

本实验共分为三个任务,您将使用Java集成开发环境Eclipse依次开发和运行Spring Cloud的服务注册中心,服务提供者和服务消费者。

首先开发服务注册中心项目,使用Eureka作为服务中心,开发流程同Spring Boot的开发流程基本一致,在配置完POM依赖和主程序之后,通过配置文件以及注解的方式指明该项目是一个Eureka Server。

最后依次开发服务提供者Eureka-Provider和服务消费者Eureka-Consumer。其开发流程与Spring Boot的开发流程基本一致,开发完Controller层后,需通过配置文件的方式指定Eureka Server的位置,从而保证服务运行后可以顺利注册到注册中心。

4. 实验步骤

任务1 开发Eureka Server服务注册中心

【任务目标】

完成Spring Cloud 微服务注册中心Eureka Server的开发和运行。

【任务步骤】

1、创建新的Maven工程

1.打开Eclipse,file->new ->maven project,点击Next:

image008

2.项目信息填写如下:

项目信息

填写内容

Group Id

com.test

Artifact Id

eureka-server

Package

com.test.eureka_server

填写完项目基本信息后,点击Finish。

image009

项目创建完成后结构如下:

image010

2、添加POM依赖

在上一步创建的Eureka-server项目根目录(project节点)下找到pom.xml依赖配置文件,打开后编辑以下内容在project 标签内。

  • spring-cloud-starter-eureka-server表示Eureka服务端(注册中心)依赖jar包;
  • spring-cloud-starter-config为分布式配置中心组件。
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Dalston.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
    </dependency>
</dependencies>

3、创建启动类

1.在包目录com.test.eureka_server 下,右击新建一个Java类作为启动类,命名为EurekaServerApplication.java。

image011

2.EurekaServerApplication.java启动类代码如下:

   package com.test.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);
   	}
   }

这段代码通过注解的方式声明该类为一个EurekaServer启动类。

4、编辑配置文件

1.在项目src/main/resouces资源文件夹中添加application.yml配置文件。

image012

2.在application.yml中添加如下配置内容:

   server.port: 8000
   eureka.client.register-with-eureka: false
   eureka.client.fetch-registry: false
   eureka.client.service-url.defaultZone: http://localhost:${server.port}/eureka/
  • eureka.client.register-with-eureka 属性表示是否将自己注册到Eureka Server, 默认为true。 由于当前应用就是Eureka Server, 因此设为 false;
  • eureka.client.fetch-registry 表示是否从Eureka Server获取注册信息,默认为true。 如果这是一个单点的 Eureka Server,不需要同步其他节点的数据,可以设为false;

至此,EurekaServer的开发就完成了。

5、启动EurekaServerApplication

1.在EurekaServerApplication主类上右键 -> run as -> Java Application,启动项目。

image013

2.在浏览器中访问:http://localhost:8000/,如下界面就是Eureka的web界面,当前没有注册任何服务。

image014

任务2 开发Eureka- Provider服务提供者

【任务目标】

完成Eureka- Provider项目的开发,Provider可以根据Consumer的请求提供返回结果。

【任务步骤】

1、创建新的Maven工程

1.在eclipse中通过maven方式创建eureka-provider项目(具体步骤参照Step1中所示);

项目创建完成后如下图:

image015

2.添加POM依赖

   <dependencyManagement>
       <dependencies>
           <dependency>
               <groupId>org.springframework.cloud</groupId>
               <artifactId>spring-cloud-dependencies</artifactId>
               <version>Dalston.RELEASE</version>
               <type>pom</type>
               <scope>import</scope>
           </dependency>
       </dependencies>
   </dependencyManagement>
   <dependencies>
       <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-config</artifactId>
       </dependency>
       <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-eureka-server</artifactId>
       </dependency>
   </dependencies>

2、创建启动类

1.在包目录下新建启动类EurekaProviderApplication.java。

image016

2.主启动类EurekaProviderApplication代码如下:

   package com.test.eureka.provider;
   
   import org.springframework.boot.SpringApplication;
   import org.springframework.boot.autoconfigure.SpringBootApplication;
   import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
   import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
   
   @EnableEurekaClient
   @EnableDiscoveryClient
   @SpringBootApplication
   public class EurekaProviderApplication {
   	
   	public static void main(String[] args) {
   		SpringApplication.run(EurekaProviderApplication.class, args);
   	}
   }
   

3、新增控制器类

1.在包目录下新建一个HelloController.java。

image017

2.控制器类HelloController.java代码如下:

   package com.test.eureka.provider;
   
   import org.springframework.web.bind.annotation.PathVariable;
   import org.springframework.web.bind.annotation.RequestMapping;
   import org.springframework.web.bind.annotation.RestController;
   
   @RestController
   public class HelloController {
   	@RequestMapping("/hello/{name}")
   	public String hello(@PathVariable("name") String name) {
   		return "provider: hello " + name;
   	}
   }

该代码表示:HelloController会接收/hello/{name}的请求,其中name可以是任意字符串。接收后hello函数会返回一个字符串的拼接:"provider: hello " + name。

4、编辑配置文件

1.在src/main/resources资源文件夹中新增配置文件application.yml。

image018

2.application.yml配置文件内容如下:

其中配置了该项目的服务端口号,服务名称,以及要找寻的eureka服务地址。

server.port: 8001
spring.application.name: eureka-provider 
eureka.client.service-url.defaultZone: http://localhost:8000/eureka/

5、运行EurekaProvider

1.EurekaProviderApplication.java主类上右键 -> run as -> Java Application。

image013

2.eureka-provider项目会自动注册到任务1中开发的eureka-server注册中心;在浏览器中再次访问:http://localhost:8000/

image019

任务3 开发Eureka- Consumer服务消费者

【任务目标】

完成Eureka-Consumer项目的开发,Consumer为访问请求的发出者。

【任务步骤】

1、创建新的Maven工程

1.在eclipse中通过maven方式创建eureka-consumer项目(具体步骤参照Step1中所示);项目创建完成后的结构如下图:

image020

2.添加POM依赖

在项目根目录下的pom.xml配置文件中的project节点下加入以下依赖:

   <dependencyManagement>
       <dependencies>
           <dependency>
               <groupId>org.springframework.cloud</groupId>
               <artifactId>spring-cloud-dependencies</artifactId>
               <version>Dalston.RELEASE</version>
               <type>pom</type>
               <scope>import</scope>
           </dependency>
       </dependencies>
   </dependencyManagement>
   <dependencies>
       <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-config</artifactId>
       </dependency>
       <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-eureka-server</artifactId>
       </dependency>
   </dependencies>

2、创建启动类

1.在包目录下新建启动类EurekaConsumerApplication.java。

image021

2.主启动类EurekaConsumerApplication代码如下:

   package com.test.eureka.consumer;
   
   import org.springframework.boot.SpringApplication;
   import org.springframework.boot.autoconfigure.SpringBootApplication;
   import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
   import org.springframework.cloud.client.loadbalancer.LoadBalanced;
   import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
   import org.springframework.context.annotation.Bean;
   import org.springframework.web.client.RestTemplate;
   
   @SpringBootApplication
   @EnableDiscoveryClient
   @EnableEurekaClient
   public class EurekaConsumerApplication {
   	public static void main(String[] args) {
   		SpringApplication.run(EurekaConsumerApplication.class, args);
   	}
   
   	@Bean
   	@LoadBalanced
   	RestTemplate getRrestTemplate() {
   		return new RestTemplate();
   	}
   }

3、新增控制器类

1.在包目录下新建HelloConsumerController.java。

image022

2.控制器类HelloConsumerController.java代码如下:

   package com.test.eureka.consumer;
   
   import org.springframework.beans.factory.annotation.Autowired;
   import org.springframework.web.bind.annotation.PathVariable;
   import org.springframework.web.bind.annotation.RequestMapping;
   import org.springframework.web.bind.annotation.RestController;
   import org.springframework.web.client.RestTemplate;
   
   @RestController
   public class HelloConsumerController {
   	@Autowired
   	RestTemplate restTemplate;
   
   	@RequestMapping("/consumer/hello/{name}")
   	public String helloConsumer(@PathVariable("name") String name) {
   		return restTemplate.getForObject("http://eureka-provider/hello/" + name, String.class);
   	}
   }

4、编辑配置文件

1.在src/main/resources资源文件夹中新增配置文件application.yml

image023

2.application.yml配置文件内容如下

server.port: 8002
spring.application.name: eureka-consumer
eureka.client.service-url.defaultZone: http://localhost:8000/eureka/

5、启动EurekaConsumer

1.运行EurekaConsumerApplication.java启动类,eureka-consumer项目会自动注册到step1中开发的eureka-server注册中心;

2.在浏览器中再次访问:http://localhost:8000/

可以查看到服务的注册情况:

image024

能看到注册到eureka-server的2个服务(eureka-provider、eureka-consumer)

5. 实验验证

通过浏览器访问:http://localhost:8002/consumer/hello/要传递的参数;

image025

浏览器能根据输入的参数返回对应的内容,说明consumer消费到了provider返回的信息,并将其传回到浏览器。至此,就说明我们的Spring Cloud程序运行成功。

至此,相信您已经掌握了Spring Cloud开发的基本流程。

6. FAQ

【问题】1、因端口占用导致的项目启动失败

当项目启动时,出现如下错误时,说明当前的端口已经被占用。

image026

【解决】修改application.yml中的server.port为其他端口号,如当8000端口被占用时,修改端口为8080。

image027