40

GitHub - coderqianlq/spring-cloud-cli: 自己搭建的简单的springcloud项目,主要集成...

 5 years ago
source link: https://github.com/coderqianlq/spring-cloud-cli
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

README.md

68747470733a2f2f696d6167652e6962622e636f2f6659394c74652f537072696e675f4c6f676f735f434c4f55445f484f522e706e67

Build Status License

1. Spring Cloud简介

Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中涉及的配置管理、服务发现、断路器、智能路由、微代理、控制总线、全局锁、决策竞选、分布式会话和集群状态管理等操作提供了一种简单的开发方式。

Spring Cloud 全家桶:

服务注册中心Spring Cloud Netflix Eureka服务调用方式REST API服务网关Spring Cloud Netflix Zuul断路器Spring Cloud Netflix Hystrix分布式配置Spring Cloud Config服务跟踪Spring Cloud Sleuth消息总线Spring Cloud Bus数据流Spring Cloud Stream批量任务Spring Cloud Task

2. 微服务架构

微服务架构就是将一个完整的应用从数据存储开始垂直拆分成多个不同的服务,每个服务都能独立部署、独立维护、独立扩展,服务与服务间通过诸如RESTful API的方式互相调用。各个微服务之间是松耦合的,每个微服务仅关注于完成一件任务,每个任务代表着一个小的业务能力。

3. 服务治理

服务治理是实现微服务的关键。那么有没有好的服务治理方案呢?我想大家都听过或者使用过dubbo,dubbo就是一个带有服务治理功能的RPC框架。dubbo提供了一套较为完整的服务治理方案,所以企业如果要实现服务化的话,dubbo是很好的一个选择。

服务发现与注册作为服务治理最最重要的问题,dubbo中引入了一个注册中心的概念,而zookeeper作为dubbo推荐的注册中心,承担了及其重要的作用。

那么我们如何使用Spring Cloud来实现服务治理呢?答案就是Spring Cloud Eureka,也就是本篇博客要介绍的重点。Spring Cloud Eureka是Spring Cloud Netflix项目下的服务治理模块。而Spring Cloud Netflix项目是Spring Cloud的子项目之一,主要内容是对Netflix公司一系列开源产品的包装,它为Spring Boot应用提供了自配置的Netflix OSS整合。通过一些简单的注解,开发者就可以快速的在应用中配置一下常用模块并构建庞大的分布式系统。它主要提供的模块包括:服务发现(Eureka),断路器(Hystrix),智能路由(Zuul),客户端负载均衡(Ribbon)等。

4. 动手搭服务发现和注册

第一步:创建服务注册中心

创建Spring boot项目,命名为eureka-server,并在pom.xml中引入需要的依赖内容:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<!-- 引入Eureka服务包-->
<dependencies>
	<dependency>
	    <groupId>org.springframework.cloud</groupId>
	    <artifactId>spring-cloud-starter-eureka-server</artifactId>
	</dependency>
</dependencies>

<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>

通过@EnableEurekaServer注解启动一个服务注册中心提供给其他应用进行对话。在Spring Boot启动类加上这个注解即可,如下:

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

在默认设置下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为,只需要在application.properties配置文件中增加如下信息:

spring.application.name=eureka-server
server.port=8761
eureka.client.service-url.defaultZone=http://127.0.0.1:8761/eureka
#表示是否将自己注册到Eureka Server上,默认为true
eureka.client.registerWithEureka=false
#表示是否从Eureka Server上获取注册信息,默认为true
eureka.client.fetchRegistry=false

启动工程,访问 http://localhost:8761/ ,可以看到下面的页面,其中还没有发现任何服务。

第二步:创建服务提供方

下面我们创建提供服务的客户端,并向服务注册中心注册自己。本文我们主要介绍服务的注册与发现,所以我们不妨在服务提供方中尝试着提供一个接口来获取当前所有的服务信息。

首先,创建一个基本的Spring Boot应用。命名为eureka-order,在pom.xml中,加入如下配置:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<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>
</dependencies>

<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>

在应用启动类中通过加上@EnableEurekaClient(该注解上有@EnableDiscoveryClient)注解,该注解能激活Eureka中的DiscoveryClient实现,这样才能实现Controller中对服务信息的输出。

@SpringBootApplication
@EnableEurekaClient
public class EurekaOrderApplication {
	public static void main(String[] args) {
		SpringApplication.run(EurekaOrderApplication.class, args);
	}
}

我们在完成以上工作后,再继续对eureka-order的application.properties做一些配置工作,具体如下:

spring.application.name=eureka-order
server.port=8100
eureka.client.service-url.defaultZone=http://127.0.0.1:8761/eureka

通过spring.application.name属性,我们可以指定微服务的名称后续在调用的时候只需要使用该名称就可以进行服务的访问。eureka.client.serviceUrl.defaultZone属性对应服务注册中心的配置内容,指定服务注册中心的位置。为了在本机上测试区分服务提供方和服务注册中心,使用server.port属性设置不同的端口。

同时启动两个服务,再访问 http://localhost:8761/ 出现下图内容则表示服务注册成功:

这样就基本完成了基础的Spring Cloud搭建。Spring Cloud其它组件的搭建教程可参考我的博客


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK