栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

初学springcloud

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

初学springcloud

1、什么是springcloud

     Spring Cloud是一系列框架的有序集合。它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等,都可以用Spring Boot的开发风格做到一键启动和部署。Spring Cloud并没有重复制造轮子,它只是将各家公司开发的比较成熟、经得起实际考验的服务框架组合起来,通过Spring Boot风格进行再封装屏蔽掉了复杂的配置和实现原理,最终给开发者留出了一套简单易懂、易部署和易维护的分布式系统开发工具包。

注: springcloud不能算是一个技术,更像是一种生态

2、为什么要学习是springcloud

   随着时代的发展,需求越来越,传统的开发架构本质就是All in one,即一个项目作为一个整体进行开发和部署,整个项目需要作为一个 整体部署在一台服务器上,当这一台服务器支持不了当前用户量的时候我们的解决办法就是"横向"的扩展服务器   

一台服务器不够就再加一台服务器,但是加上服务器之后就需要考虑负载均衡问题;说白了就是在服务器集群前面加一个程序,程序根据算法实现,用于保证后面的一大堆服务器被雨露均沾,不会出现一台服务器上的资源都要被耗光了,另一台服务器上的资源还没用的情况

所以为了解决这个问题,提出了新的开发架构 —— 微服务架构

但使用微服务架构会迎来许多新的问题。

微服务的四个核心问题?

  1. 服务很多,客户端怎么访问?
    (怎么获取服务?设置注册中心,用户请求都发送到注册中心,再由注册中心分发用户请求到具体服务器)
  2. 这么多服务,服务之间如何通信?(服务之间怎么通信?)
  3. 这么多服务,如何管理?(对于这么多服务怎么统一管理?设置一个统一的服务管理平台,比如zookeeper)
  4. 服务挂了怎么办?(服务器崩溃、断电之后怎么应对?熔断机制)

微服务的四个核心问题的解决方案:
   Spring Cloud 是一套生态!专门用于解决微服务的四个核心问题的生态 

   学习SpringCloud的前提是学习SpringBoot,原因就是因为SpringCloud是基于SpringBoot的

3、springcloud有什么作用

1.SpringCloud NetFilx(第一套SpringCloud生态)

NetFilx公司推出了一套解决SpringCloud生态,或者说一站式解决微服务架构中问题的方案,我们要使用直接拿来使用即可

  • 问题1:服务很多,客户端怎么访问?
    • API网关,实现统一服务治理,使用zuul组件
  • 问题2:这么多服务,服务之间如何通信?
    • 网络通信都是基于HTTP的通信,NetFilx推出了一个自己对HTTP的包装版Feign(Feign基于HttpClient,HttpClient基于HTTP),通信方式:同步并阻塞
  • 问题3:这么多服务,如何管理?
    • 服务注册与发现,使用插件Eureka解决
  • 问题4:服务挂了怎么办?
    • 熔断机制,Hystrix

springcloud提供了一套用来解决微服务面临的问题,上手很容易,可以满足大多数中小企业的需求 

4、springcloud和springboot有什么关系

1、springBoot专注于快速开发的开发单个个体微服务

2、springCloud是关注全局的微服务协调治理的框架,它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等,都可以用Spring Boot的开发风格做到一键启动和部署。

3、springBoot可以离开springCloud独立使用,开发项目

 但springCloud离不开springBoot 属于依赖关系


springCloud主要有5大组件:

1、服务发现Netflix Eureka;

2、客服端负载均衡Netflix Ribbon;

3、断路器Netflix Hystrix;

4、服务网关Netflix Zuul;

5、分布式配置


1、服务注册与发现Netflix Eureka

    springCloud封装了Netflix公司开发的Eureka来实现服务注册和发现,

为什么使用服务注册和发现

    在服务启动时,服务提供者会向注册中心注册服务,暴露自己的地址和端口等,注册中心会更新服务列表。服务消费者启动时会向注册中心请求可用的服务地址,并且在本地缓存一份提供者列表,这样即便注册中心宕机了,仍然可以正常调用服务。如果提供者集群发生变更,注册中心会将变更推送给服务消费者,更新可用的服务地址列表。

使用Netflix的步骤:

1、先导入需要的架包

2、编写配置类

3、在主程序中开启对eureka的支持


Netflix Eureka架包

        
            org.springframework.cloud
            spring-cloud-starter-eureka-server
            1.4.6.RELEASE
        

 2、在application需要的配置

server:
  port: 7001


# Eureka配置
eureka:
  instance:
    a-s-g-name: localhost # Eureka服务端的实例名称
  client:
    register-with-eureka: false #表示是否向注册中心注册自己
    fetch-registry: false #fetch-registry如果为false,表示自己为注册中心
    service-url: #监控页面
      defaultZone: http://localhost:7002/eureka/,http://localhost:7003/eureka/ 
#在这里是注册服务器的地址,因为使用了集群所以有多个地址(如不使用集群地址为:http://localhost:7001/eureka)

3、在主启动类上加上注解(@EnableEurekaService)

package com.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer //表示EnableEurekaServer 服务端的启动类 可以让别人注册进来
public class EurekaService_7001 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaService_7001.class,args);
    }
}

到这里Eureka就好了,如果使用集群就重复写几个,只要改变端口号就行(如7002,7003......)

注:别忘记在注启动类上加注解 。

下面需要写服务的提供者:

提供者需要注册到服务中

步骤:

1、先导入需要的架包

2、编写配置类

3、启动程序


        
            org.springframework.cloud
            spring-cloud-starter-eureka
            1.4.6.RELEASE
        

        
            com.jie
            springcloud-api
            1.0-SNAPSHOT
        
        
            junit
            junit
        
        
            mysql
            mysql-connector-java
        
        
            ch.qos.logback
            logback-core
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-test
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            com.alibaba
            druid
        

        
            org.springframework.boot
            spring-boot-starter-jetty
        

        
            org.springframework.boot
            spring-boot-devtools
        

2、application.yaml

server:
  port: 8001
#  mybatis配置
mybatis:
  type-aliases-package: com.springcloud.pojo
  mapper-locations: classpath:mybatis/mapper/*.xml
# spring配置
spring:
  application:
    name: springcloud-provider-dept
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/db01?useSSL=true&serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8
    driver-class-name: org.gjt.mm.mysql.Driver

#eureka配置
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/,http://localhost:7002/eureka/,http://localhost:7003/eureka/
  instance:
    instance-id: springcloud-provider-dept8001  

提供者微服务使用mvc的三层架构访问数据库

 3、在主启动注解

//主启动类
@SpringBootApplication
@EnableEurekaClient
public class Dept_8001 {
    public static void main(String[] args) {
        SpringApplication.run(Dept_8001.class,args);
    }
}

写完后先启动 eureka 服务注册7001   在启动提供者8001

访问localhost:7001

 发现有Status 下UP(1)

说明提供者服务已经被注册到服务了


下面还需要写一个消费者微服务

步骤:

1、导入架包

2、配置application

3、主启动类注解

4、启动

        
            com.jie
            springcloud-api
            1.0-SNAPSHOT
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.cloud
            spring-cloud-starter-eureka
            1.4.6.RELEASE
        
    

2、application.yaml

server:
  port: 80

# eureka配置
eureka:
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://localhost:7001/eureka/,http://localhost:7002/eureka/,http://localhost:7003/eureka/

3、主启动类

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

因为是消费者服务所以不能有service层 只存在controller层用来调用数据

@RestController
public class ConsumerController {

    @Autowired
    private RestTemplate restTemplate;
//    ribbon在这里的地址 应该是一个变量 通过服务名访问
//    private static final String REST_URL_PREFIX="http://localhost:8001";
      private static final String REST_URL_PREFIX="http://SPRINGCLOUD-PROVIDER-DEPT";

    @RequestMapping("/consumer/add")
    public boolean addddd(Dbpt dbpt){
        return restTemplate.postForObject(REST_URL_PREFIX+"/add",dbpt,Boolean.class);
    }
    @RequestMapping("/consumer/queryId/{deptId}")
    public Dbpt queryId(@PathVariable("deptId") int deptId){
        return restTemplate.getForObject(REST_URL_PREFIX+"/queryId/"+deptId,Dbpt.class);
    }
    @RequestMapping("/consumer/query")
    public List query(){
        return restTemplate.getForObject(REST_URL_PREFIX+"/query",List.class);
    }
}

这里使用了RestTemplate模板 通过查看源码得知RestTemplate不在spring容器中,所以我们需要自己创建一个config类把RestTemplate注册到spring容器中。

@Configuration
public class BeanConfig { //@Configuration 相当于applicationContext.xml中的配置类
    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

写完之后 启动三个服务分别是:eureka:7001  提供者:8001 消费者:80

通过访问消费者在服务中查找相应数据返回在浏览器上

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/358122.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号