【java、微服务、spring】SpringCloud

vs Dubbo

Spring Cloud是一个微服务框架,提供了微服务领域中的很多功能组件

Dubbo一开始是一个RPC调用框架,核心是解决服务调用间的问题

Spring Cloud是一个大而全的框架

Dubbo则更侧重于服务调用

Dubbo所提供的功能没有Spring Cloud全面

Dubbo的服务调用性能比Spring Cloud高

Spring Cloud和Dubbo可以结合起来一起使用的

服务拆分

1. 不同微服务,不要重复开发相同业务

2.微服务数据独立,不要访问其它微服务的数据库

3.微服务可以将自己的业务暴露为接口,供其它微服务调用

远程调用

提供者与消费者

服务提供者:一次业务中,被其它微服务调用的服务。(提供接口给其它微服务)

服务消费者:一次业务中,调用其它微服务的服务。(调用其它微服务提供的接口)

提供者与消费者角色其实是相对的

一个服务可以同时是服务提供者和服务消费者

RestTemplate

1.在模块中注册RestTemplate

如在order-service中的OrderApplicaiton中编写

package cn.itcast.order;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@MapperScan("cn.itcast.order.mapper")
@SpringBootApplication
public class OrderApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class, args);
    }

    /**
     * Spring Boot中使用RestTemplate,我们通常需要注入它。我们可以通过使用@Bean注解来实现。
     */
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

2.发送请求

进入service模块

package cn.itcast.order.service;

import cn.itcast.order.mapper.OrderMapper;
import cn.itcast.order.pojo.Order;
import cn.itcast.order.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private RestTemplate restTemplate;

    public Order queryOrderById(Long orderId) {
        // 1.查询订单
        Order order = orderMapper.findById(orderId);
        String url = "http://localhost:8081/user/" + order.getUserId();
        User user = restTemplate.getForObject(url, User.class);
        order.setUser(user);
        // 4.返回
        return order;
    }
}

Eureka

作用

(1)注册服务信息

(2)拉取服务user-service的信息(服务不会挂,有心跳续约,每30秒1次,不跳就剔除)

(3)负载均衡(多个服务端口任意挑一个)

(4)远程调用

1.搭建注册中心

创建eureka-server模块

增加依赖

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

创建EurekaApplication

package org.example.eureka;

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

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

编写yml文件

server:
  port: 10086
spring:
  application:
    name: eurekaserver #注册的服务名称
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka
      

搭建结构 

2.注册服务

引入依赖(以user-server模块为例子)

 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

改写yml(插入、整合)

spring:
  application:
    name: userservicer #注册的服务名称
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka

3. 服务拉取(发现)

服务拉取是基于服务名称获取服务列表,然后在对服务列表做负载均衡

1. 修改OrderService的代码,修改访问的url路径,用服务名代替ip、端口:

String url = "http://userservice/user/" + order.getUserId();

2. 在order-service项目的启动类OrderApplication中的RestTemplate添加负载均衡注解:

package cn.itcast.order;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@MapperScan("cn.itcast.order.mapper")
@SpringBootApplication
public class OrderApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class, args);
    }

    /**
     * Spring Boot中使用RestTemplate,我们通常需要注入它。我们可以通过使用@Bean注解来实现。
     */
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
负载均衡原理

Robbon负载均衡

 Nacos(2.3.0)

到bin目录执行命令启动:

.\startup.cmd startup.cmd -m standalone

注册服务

1.增加依赖

父模块

<dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.5.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

服务模块

<dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

在yml文件里增加

spring:
  cloud:
    nacos:
      server-addr: localhost:8848

学习笔记

https://blog.csdn.net/David_Hzy/article/details/136380541

 vs Eureka

共同点

  1. 都支持服务注册和服务拉取
  2. 都支持服务提供者心跳方式做健康检测

区别

  1. Nacos支持服务端主动检测提供者状态:临时实例采用心跳模式,非临时实例采用主动检测模式
  2. 临时实例心跳不正常会被剔除,非临时实例则不会被剔除
  3. Nacos支持服务列表变更的消息推送模式,服务列表更新更及时
  4. Nacos集群默认采用AP方式,当集群中存在非临时实例时,采用CP模式;Eureka采用AP方式

  • 24
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java SpringCloud微服务是一种基于Spring Cloud框架的微服务架构。它通过将一个大型应用程序拆分为多个小型的、自治的服务来实现系统的解耦和扩展性。每个微服务都具有独立的数据库和业务逻辑,并且可以独立部署和扩展。Spring Cloud提供了一系列的组件和工具,如服务注册与发现、负载均衡、熔断器、配置管理等,来支持微服务的开发和管理。其中,Spring Cloud Bus是Spring Cloud中的消息总线方案,它结合Spring Cloud Config可以实现配置信息的自动更新,提供了一种方便的方式来管理微服务的配置。 为了进一步学习和了解Java微服务的实战应用,可以关注小编的博文《Java微服务——SpringCloud实战篇》,它将介绍如何整合Gateway、Config和Bus等组件来构建和管理Java微服务。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Java微服务——SpringCloud概念篇](https://blog.csdn.net/friend_X/article/details/116127269)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [Java微服务篇2——SpringCloud](https://blog.csdn.net/qq_43842093/article/details/120171070)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

岩塘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值