Spring-Cloud 从0开始(一) Eureka-Server

背景

2019年第一篇blog打算从微服务开始,正所谓本身立下的flag趴着也要写完^^.因此从今天开始打算会持续写Spring-Cloud相关文章.java

什么是微服务

  1. 微服务是一种架构风格
  2. 一些列微小的服务组成
  3. 每一个服务独立开发,独立部署

为甚么采用微服务

由于单体用用存在一些问题,总结概括以下:spring

  1. 开发效率低(代码体系大)
  2. 代码维护难(代码体系大)
  3. 稳定性不高(改动一个模块可能影响其余代码功能)
  4. 扩展困难(单个模块扩展须要考虑是否影响其余模块功能)
  5. 部署不够灵活(部署时间超级长,部署繁琐)

什么是Eureka

  1. Eureka:在英文词典中意为"找到了,发现了", 顾名思义,他在Spring-Cloud中承担的角色是服务的注册与发现
  2. Spring Cloud Eureka 是基于Netflix Eureka作的二次封装
  3. Spring Cloud Eureka 组件由两部分组成 Eureka-Server, Eureka-Client
  4. Eureka-Server:服务注册中心
  5. Eureka-Client:服务注册和服务调用

说明

  1. 在系列博客中,Spring-Cloud版本是:Greenwich.RC1
  2. IDE 采用idea
  3. Spring-Boot 版本:2.1.1.RELEASE,关于Spring-CLoud版本和Boot的版本对应,咱们能够去官网查看,咱们这里选择以下

搭建Eureka-Server

  1. 打开idea,选择建立Spring项目 bootstrap

  2. 选择Maven坐标:gav 架构

  3. 选择项目类型为Eureka-Server app

  4. 建立完项目以后,查看Pom中Spring-Cloud与Boot版本:ide

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

<!-- spring-cloud版本 -->
<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Greenwich.RC1</spring-cloud.version>
</properties>
复制代码
  1. 做为Eureka-Server咱们须要在启动类添加注解@EnableEurekaServer
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}
复制代码
  1. 配置文件修改
  1. 因为Spring-Cloud中bootstrap.yml/properties(这里采用yml)是项目的启动加载配置文件,因此咱们先将配置文件重命名为bootstrap.yml
  1. 做为服务注册中心,咱们要求高可靠性和稳定性,因此咱们这里搭建三套Eureka-Server,端口分别为8761, 8762, 8763;其项目搭建方式同样(同上), Eureka-Server1的配置文件以下:
eureka:
 client:
 service-url:
 defaultZone: http://localhost:8762/eureka/,http://localhost:8763/eureka/
 register-with-eureka: false

spring:
 application:
 name: eureka-server

server:
 port: 8761
复制代码

Eureka-Server2的配置文件以下:spring-boot

spring:
 application:
 name: eureka-server2

server:
 port: 8762

eureka:
 client:
 service-url:
 defaultZone: http://localhost:8761/eureka/,http://localhost:8763/eureka/
 register-with-eureka: false
复制代码

Eureka-Server3的配置文件以下:微服务

spring:
 application:
 name: eureka-server3

server:
 port: 8763

eureka:
 client:
 register-with-eureka: false
 service-url:
 defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
复制代码

配置说明:url

  1. spring.application.name: 是服务的名称
  2. server.port: 表明服务的端口
  3. eureka.client.register-with-eureka=false 表示让Eureka-Server本身不须要注册到本身
  4. 由上面三个Eureka-Server的配置咱们能够看到,在注册Eureka-Server集群中,咱们只须要将不一样的Eureka-Server相互注册,就能够实现Eureka-Server的高可用
  1. 启动服务 启动服务以后咱们能够访问:http://localhost:8761/; http://localhost:8762/; http://localhost:8763/ 发现注册中心已经启动:

结尾

到这里,咱们集群式的Eureka-Server已经搭建好了, 咱们下一节来搭建Eureka-Client来发现服务.好了,预知后事如何, 请听下回分解!idea

相关文章
相关标签/搜索