三天速成微服务

微服务技术栈

总结

微服务技术对比

技术栈

SpringCloud

SpringCloud是目前国内使用最广泛的微服务框架。官网地址:https://spring.io/projects/spring-cloud

Springboot和SpringCould兼容性

代码目录结构如下

用于远程调用Bean

代码

java 复制代码
package cn.itcast.order.config;

//import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestTemplateConfig {

    @Bean
//    @LoadBalanced // 添加负载均衡注解
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

方法执行过程过程

OrderController
OrderService
OrderMapper

UserMapper

Order
User
远程调用成功截图

访问

总结

微服务调用方式基于RestTemplate发起的http请求实现远程调用http请求做远程调用是与语言无关的调用,只要知道对方编辑端口、接口路径、请求参数即可。

Eureka消费者与提供者

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

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

提供者和消费者是相对的,一个服务可能既是提供者又是消费者。

pom.xml引入依赖服务端依赖

java 复制代码
    <dependencies>
        <!--eureka服务端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

注册userservice

use pom.xml引入服务管理依赖

java 复制代码
      <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

成功服务截图

复制多个实例

操作如下

输入 -Dserver.port=8082

点击确定

成功注册两个实例

Eureka服务注册总结

服务注册是微服务架构中的关键环节,保证各服务之间的发现和调用。

1引入依赖

在pom文件中eureka-client依赖:

复制代码
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2配置文件

在application.yml文件,添加服务名称、eureka地址:

复制代码
spring:
  application:
    name: userservice
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:1086/eureka

服务注册是微服务架构中的关键环节,保证各服务之间的发现和调用。以下是服务注册的关键概念和步骤的总结:

Eureka服务发现总结

Ribbon-负载均衡原理

SpringCloudRibbon的底层采用了一个拦截器,拦截了RestTemplate发出的请求,对地址做了修改

相关推荐
骄马之死13 小时前
SpringMVC + SpringBoot 核心知识点总结
java·spring boot·后端
逸模13 小时前
告别熬夜手工整理台账,逸模智能归集实现项目数据自动化存档
大数据·运维·人工智能·笔记·其他·信息可视化·自动化
fanly1113 小时前
AgentForge 智能体组件:与云驿插件平台构建全生态化的微服务一体化智能开发引擎
微服务·ai·agent
sbjdhjd14 小时前
Redis 主从复制、哨兵高可用与 Cluster 集群部署实验手册
运维·前端·redis·云原生·开源·bootstrap·html
AOwhisky14 小时前
MySQL 学习笔记(第四期):SQL 语言之多表查询
linux·运维·网络·数据库·笔记·学习·mysql
Phantom Void14 小时前
服务器处理客户端请求的设计方法
linux·运维·网络
倔强的石头10614 小时前
Fooocus开源神器+cpolarAI让绘画告别服务器依赖
运维·服务器·cpolar
wei_shuo15 小时前
服务器挂了等用户投诉才发现?我用Beszel搭了轻量监控系统,宕机第一时间通知我
运维·服务器
王码码203515 小时前
多台服务器怎么统一看状态?Beszel 轻量监控,搭起来不费事
运维·服务器·后端·安全·阿里云·接口·web
郑洁文15 小时前
基于Spring Boot的流浪动物救助网站
java·spring boot·后端·毕设·流浪动物救助