springcould01

1.为什么需要spring cloud

1.1. 什么是单体应用(Monolith)

这种将所有的代码及功能都包含在一个WAR包中的项目组织方式被称为Monolith

1.2缺点

在项目很小的情况下这种单体应用比较简单,但是随着项目越变越大,代码越来越多。就会存在以下缺点。
①编译难,部署难,测试难
②技术选择难
③扩展难

2.1 MicroService(微服务)架构

2.2 为什么需要

使用微服务架构就可以解决单体项目缺点。解决单体应用的不足

2.3 什么是MicroService架构

把一个系统拆分为多个独立技术选型,独立开发,独立部署,独立运维的微服务
微服务架构使用场景 规模大

微服务架构spring解决方案

开发单个服务springboot,协调多个服务用springcloud http协议
单体应用架构:中小型项目(功能相对较少) crm 档案管理 库存管理,公司官网等

    微服务架构:大型项目(功能比较多) 商城 erp,人力资源等

springcloud就是一个基于Spring Boot实现的服务治理工具包

2. Spring cloud概述

1.1 是什么

Spring cloud是一个基于Spring Boot实现的服务治理工具包,用于微服务架构中管理和协调服务的。
五大神兽:
服务注册发现——Netflix Eureka : 注册所有微服务的通信地址
客服端负载均衡——Netflix Ribbon\Feign :服务之间的调用问题
断路器——Netflix Hystrix :解决微服务故障问题,微服务的隔离
服务网关——Netflix Zuul :微服务的统一入口
分布式配置——Spring Cloud Config :统一管理微服务的配置文件

Spring cloud是微服务架构中服务治理工具集,有很多产品组成。核心为五大神兽。相较于dubbo更加靠谱。

1.2 为什么需要Springcloud?

他是用来管理和协调微服务架构中多个服务

3.springcloud入门环境搭建

1)搭建环境
以maven多模块化的方法搭建
2)服务提供者=提供服务
3)服务消费者-调用服务

1.1parent-. 搭建父模块

Parent:
限定springboot版本-开发单个服务
限定springcloud版本-协调多个服务

 <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR1</spring-cloud.version>
        <springboot.version>2.0.5.RELEASE</springboot.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                         <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${springboot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


1.2服务提供者product-service/product-common

    user_interface/facade 公共代码
  domain,query,接口,util

user_provider_8001 :服务提供者
public User getUser

    <!--springboot支持-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>

在这里插入图片描述

1.3 服务消费者

ser_consumer_9001:服务消费者
User user = 调用代码

<!--springboot支持-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
</dependency>

yml:
server:
port: 9001
spring:
application:
name: USER-CONSUMER
在这里插入图片描述
通过springboot实现服务提供者,然后再服务消费者以restTemplate以restful的方式完成服务调用。

4.springcloud Eureka

1.1 是什么

服务注册中心,c/s架构模式,EurekaServer,EurekaClient(服务提供者注册服务,服务消费者获取服务列表完成)
Eureka是netflix的一个子模块,也是核心模块之一

1.2. Eureka注册中心搭建

<!--springboot支持-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
</dependency>

<!--Eureka服务端支持-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

yml配置

server:
port: 7001
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false #是否要注册到eureka
fetchRegistry: false #表示是否从Eureka Server获取注册信息
serviceUrl:
defaultZone: http:// e u r e k a . i n s t a n c e . h o s t n a m e : {eureka.instance.hostname}: eureka.instance.hostname:{server.port}/eureka/ #单机配置
主类

	@SpringBootApplication
@EnableEurekaServer //标识是eureka服务端
public class EnrekaServerApplication_7001 {
    public static void main(String[] args) {
        SpringApplication.run(EnrekaServerApplication_7001.class);
    }
}

1.3. 服务提供者注册到Eureka

 <!--eureka客户端支持 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>

修改配置
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka #告诉服务提供者要把服务注册到哪儿
instance:
prefer-ip-address: true # 当调用getHostname获取实例的hostname时,返回ip而不是host名称
ip-address: 127.0.0.1 # 指定自己的ip信息,不指定的话会自己寻找

1.4. 服务消费者从Eureka调用服务

<!-- Eureka客户端 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

配置
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka #告诉服务提供者要把服务注册到哪儿
instance:
prefer-ip-address: true # 当调用getHostname获取实例的hostname时,返回ip而不是host名称
ip-address: 127.0.0.1 # 指定自己的ip信息,不指定的话会自己寻找

整改服务调用

 @Autowired
private DiscoveryClient discoveryClient;// Eureka客户端,可以获取到服务实例信息

// String baseUrl = "http://localhost:8081/user/";
        // 根据服务名称,获取服务实例
        List<ServiceInstance> instances = discoveryClient.getInstances("user-service");
        // 因为只有一个UserService,因此我们直接get(0)获取
        ServiceInstance instance = instances.get(0);
        // 获取ip和端口信息
        String baseUrl = "http://"+instance.getHost() + ":" + instance.getPort()+"/user/";
       this.restTemplate.getForObject(baseUrl + id, User.class)

5.Eureka注册中心集群

1.1集群和分布式概念的理解

分布式:把不同业务分散到不同的服务器集群。
两个班长 一个收作业 一个点名

1. 2为什么需要集群

如果只有一个注册中心服务器,会存在单点故障所以要集群。

1.3 搭建集群

主类
改名,该端口
映射hosts

127.0.0.1 eureka-7001.com
127.0.0.1 eureka-7002.com
配置
7001:
server:
port: 7001

eureka:
instance:
hostname: eureka-7001.com
client:
registerWithEureka: false #
fetchRegistry: false #
serviceUrl:
#defaultZone: http:// e u r e k a . i n s t a n c e . h o s t n a m e : {eureka.instance.hostname}: eureka.instance.hostname:{server.port}/eureka/ #单机配置
defaultZone: http://eureka-7002.com:7002/eureka/
7002:
server:
port: 7002

eureka:
instance:
hostname: eureka-7002.com
client:
registerWithEureka: false #
fetchRegistry: false #
serviceUrl:
#defaultZone: http:// e u r e k a . i n s t a n c e . h o s t n a m e : {eureka.instance.hostname}: eureka.instance.hostname:{server.port}/eureka/ # 单机配置
defaultZone: http://eureka-7001.com:7001/eureka/

1.4 服务提供者和服务消费者修改

#defaultZone: http://localhost:7001/eureka
defaultZone: http://eureka-7001.com:7001/eureka,http://eureka-7002.com:7002/eureka
如果是只有一个Eureka注册中心,会存在单点故障,要做集群,做了集群后,服务提供者和消费者注册服务或发现服务时要配置集群(把多个Eureka都配置上).

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值