【java、微服务、spring】SpringCloud

服务拆分

  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模块

java 复制代码
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模块

增加依赖

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

创建EurekaApplication

java 复制代码
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文件

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

搭建结构

2.注册服务

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

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

改写yml(插入、整合)

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

3. 服务拉取(发现)

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

  1. 修改OrderService的代码,修改访问的url路径,用服务名代替ip、端口:
java 复制代码
String url = "http://userservice/user/" + order.getUserId();
  1. 在order-service项目的启动类OrderApplication中的RestTemplate添加负载均衡注解:
java 复制代码
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

学习笔记

相关推荐
我是小妖怪,潇洒又自在1 小时前
springcloud alibaba(九)Nacos Config服务配置
后端·spring·spring cloud
qq_12498707532 小时前
重庆三峡学院图书资料管理系统设计与实现(源码+论文+部署+安装)
java·spring boot·后端·mysql·spring·毕业设计
大学生资源网2 小时前
java毕业设计之“知语”花卉销售网站的设计与实现源码(源代码+文档)
java·mysql·毕业设计·源码·springboot
小鸡脚来咯2 小时前
Redis三大问题:穿透、击穿、雪崩(实战解析)
java·spring·mybatis
桦说编程2 小时前
并发编程高级技巧:运行时检测死锁,告别死锁焦虑
java·后端·性能优化
jiayong232 小时前
Spring AI Alibaba 深度解析(三):实战示例与最佳实践
java·人工智能·spring
梁同学与Android2 小时前
Android ---【经验篇】ArrayList vs CopyOnWriteArrayList 核心区别,怎么选择?
android·java·开发语言
ss2733 小时前
从零实现线程池:自定义线程池的工作线程设计与实现
java·开发语言·jvm
苗壮.3 小时前
CommandLineRunner 是什么?
java
石工记3 小时前
windows 10直接安装多个JDK
java·开发语言