SpringCloud微服务入门

SpringCloud入门:微服务搭建与Eureka集群

SpringCloud入门

1. 概述

SpringCloud是基于SpringBoot的一个微服务架构

2. 单体项目

单体应用:一个独立的应用,所有的模块和资源都在一个项目中,最终把项目打包成一个文件,部署在一台服务器上!

单体项目的优点:

①:开发部署简单

②:成本相对较低

③:小型规模项目,性能更优

单体项目的缺点:

①:扩展能力受限

②:模块之间的耦合度较高

③:不便于维护升级

④:技术选型单一(只能使用同一种语言或者数据库)

⑤:单一模块故障会牵连其他模块

3. 微服务

微服务:将一个大型应用拆分成多个小型应用,这些小型项目部署在不同的容器中,有自己独立的进程,互相之间使用http协议restful来达到数据交互!这些小型项目同时运行来完成大型项目的所有业务!

微服务与分布式:

微服务一定是分布式,它是对分布式更细粒度的划分,分布式更是一种广泛的应用。

例如:将一个应用部署在不同的计算机上,即可称之为分布式,但不一定是微服务架构!

微服务的优点:

①:便于局部扩展

②:技术选型多样化(可以使用不同的语言,不同的数据库)

③:单个微服务便于开发维护

④:单个微服务复杂性降低

⑤:服务与服务之间耦合度降低

⑥:当项目规模较大的时候,性能更优

⑦:单个微服务故障,不会牵连到其他服务

微服务的缺点:

①:成本相对较高

②:微服务数据交互受通讯影响

③:不便于部署

4. SpringCloud的组件

服务注册发现——Netflix Eureka : 帮我们服务的通信地址的

客服端负载均衡——Netflix Ribbon\Feign : 解决网络通信的

断路器——Netflix Hystrix :解决微服务故障的

服务网关——Netflix Zuul :微服务的大门(安保部门)

分布式配置——Spring Cloud Config :统一管理微服务的配置

5. SpringCloud与Dubbo

SpringCloud属于Spring系列,Dubbo属于阿里巴巴

SpringCloud:是一个全方位的解决方案,基于http协议,具备搭建微服务的相关组件!

Dubbo:是一个rpc框架,使用原生的tcp请求,需要整合其他的组件开发!

从通信上来说:Dubbo更优,从开发的复杂性来说,SpringCloud更优,从功能完整度来说,SpringCloud更优!

6. Eureka注册中心

注册中心:用于管理微服务的通讯地址,所有的微服务在启动时,都会向注册中心提交自己的通讯地址(IP,端口号),注册中心会形成一个服务地址清单,所有的微服务会从注册中心获取地址清单,当某个服务需要向另一个服务发起请求时,该服务会从地址清单中获取目标服务的通讯地址,使用http发起调用!

注册中心可以实现服务的自动上下线,每个微服务都会使用心跳检测(周期性地向注册中心发送请求)向注册中心续租,当某个微服务故障的时候,不会再发送心跳检测,那么注册中心就会把该服务标记为下线,同时从地址清单中移除下线的服务的地址,其他的微服务会同步新的通信清单!

7. Maven多模块的方式搭建微服务

简单分为三个模块:Eureka注册中心服务(端口号:3000),用户服务(端口号:1000)、支付服务(端口号:2000)

用户服务需要拉取支付服务 !

1.创建父项目

pom.xml中的依赖

 <!--springboot的父工程-->
    <parent>
        <groupId>org.springframework.boot </groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
    </parent>
    <!--springcloud的所需依赖-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
    </dependencies>
    </dependencyManagement>

在父项目中引用SpringBoot的父工程,同时管理SpringCloud所有的jar包,SpringCloud的版本必须与SpringBoot的版本对应

2.在父项目中创建一个模块作为Eureka服务

①:pom.xml中需要导入的依赖

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

spring-cloud-starter-netflix-eureka-server包中既包括eureka的服务端和客户端,也包括web,所以不需要再导入web依赖

②:在application.yml中配置

server:
  port: 3000
eureka:
  instance:
    hostname: 127.0.0.1
    appname: eureka-center
  client:
    registerWithEureka: false 
    fetchRegistry: false #禁止获取服务的通讯地址清单
    serviceUrl:
      #defaultZone: http://localhost:3000/eureka
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

在此yml中需要说明:

registerWithEureka: false 禁止在注册中心注册自身,因为Eureka既可以作为服务端也可作为客户端,作为服务端的时候,不需要去注册!

fetchRegistry: false 禁止获取服务的通讯地址清单!

defaultZone: http://{eureka.instance.hostname}:{server.port}/eureka/ 注册中心的地址,使用$取值可以动态修改地址!

③:创建主配置类

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

@EnableEurekaServer 注解开启Eureka服务!

访问http://localhost:3000/即可查看Eureka的信息!

3.在父项目创建一个模块作为用户模块服务

①:pom.xml中的依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--客户端依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

spring-cloud-starter-netflix-eureka-client包中没有web相关,需要导入web依赖

②:在application.yml中配置

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:3000/eureka/ 
  instance:
    prefer-ip-address: true
    instance-id: pay-server:2000
server:
  port: 2000
spring:
  application:
    name: pay-server

defaultZone: http://localhost:3000/eureka/ 注册中心服务端的注册地址

prefer-ip-address: true 使用ip进行注册
instance-id: pay-server:2000 服务注册到注册中心的id名

③:创建主配置类

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

@EnableDiscoveryClient 开启服务发现,开启注册中心客户端功能

@EnableEurekaClient 开启EurekaClient客户端功能,只在Eureka下可用

这两个注解可以不加,也是可以的

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

4.在父项目创建一个模块作为支付服务

配置同用户服务一样!

8. Eureka的集群

为了达到高可用,需要对Eureka做集群,这里在一台电脑上模拟两个Eureka注册中心!

Eureka-A会在Eureka-B中注册,同时Eureka-B也会在Eureka-A中注册

两个Eureka注册中心的地址都是127.0.0.1

  1. 修改host文件

C:\Windows\System32\drivers\etc\hosts 右击属性--安全--编辑--添加读写权限!

127.0.0.1       peer1
127.0.0.1       peer2
  1. 修改eureka注册中心的配置application.yml,这里使用多环境的方式配置,在启动完第一个环境,切换到第二个环境,再启动!
#使用SpringBoot多环境配置的方式来配置 2个 注册中心

#主配置
spring:
  profiles:
    active: peer1
---
#第一个EurekaServer的配置
spring:
  profiles: peer1
  application:
    name: eureka-server
eureka:
  instance:
    hostname: peer1
    prefer-ip-address: true
    instance-id: eureka-server:3000
  client:
    serviceUrl:
      defaultZone: http://peer2:3001/eureka/
server:
  port: 3000
---
#第二个EurekaServer的配置
spring:
  profiles: peer2
  application:
    name: eureka-server
eureka:
  instance:
    hostname: peer2
    prefer-ip-address: true
    instance-id: eureka-server:3001

  client:
    serviceUrl:
      defaultZone: http://peer1:3000/eureka/
server:
  port: 3001

两个eureka互相注册,peer1的注册中心地址应该是peer2,peer2的注册中心地址是peer1!

设置运行方式为并行,启动主配置类,然后切换环境为peer2,再运行,启动用户服务和支付服务,访问 http://localhost:3000/,在application中可以看到已经注册的服务!

最后编辑于:2024-12-10 21:05:30


喜欢的朋友记得点赞、收藏、关注哦!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值