三天速成微服务

微服务技术栈

总结

微服务技术对比

技术栈

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发出的请求,对地址做了修改

相关推荐
安全渗透Hacker17 小时前
参数未校验导致的DOS(服务拒绝)问题典型场景
java·安全·web安全·网络安全·安全性测试
oMcLin18 小时前
如何在 Manjaro Linux 上通过配置systemd服务管理,提升微服务架构的启动速度与资源效率
linux·微服务·架构
霸气十足+拼命+追梦少年18 小时前
服务器挂载U盘或硬盘
运维·服务器
Chan1618 小时前
微服务 - Higress网关
java·spring boot·微服务·云原生·面试·架构·intellij-idea
二哈喇子!18 小时前
JavaSE 与 JavaEE 知识点整合
java·servlet·tomcat
之歆18 小时前
Spring AI入门到实战到原理源码-多模型协作智能客服系统
java·人工智能·spring
慕容雪_18 小时前
运维笔记-笔记本无线网卡连接wifi,有线网卡连接内网
运维·内网·远程
yyy(十一月限定版)18 小时前
c++(3)类和对象(中)
java·开发语言·c++
小杰帅气18 小时前
进程优先级与切换调度
linux·运维·服务器