【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 分钟前
Spring Security入门指南
后端·spring·面试
云泽8089 分钟前
C/C++内存管理详解:从基础原理到自定义内存池原理
java·c语言·c++
Code小翊18 分钟前
堆的基础操作,C语言示例
java·数据结构·算法
高山上有一只小老虎36 分钟前
idea中设置快捷键风格
java·ide·intellij-idea
JH307336 分钟前
IDEA自带的Maven安装位置
java·maven·intellij-idea
梵得儿SHI1 小时前
Java 反射机制核心类详解:Class、Constructor、Method、Field
java·开发语言·反射·class·constructor·java反射·java反射机制
迎風吹頭髮1 小时前
Linux内核架构浅谈44-Linux slab分配器:通用缓存与专用缓存的创建与使用
linux·spring·架构
m0_736927041 小时前
想抓PostgreSQL里的慢SQL?pg_stat_statements基础黑匣子和pg_stat_monitor时间窗,谁能帮你更准揪出性能小偷?
java·数据库·sql·postgresql
Jabes.yang1 小时前
Java面试大作战:从缓存技术到音视频场景的探讨
java·spring boot·redis·缓存·kafka·spring security·oauth2
Query*1 小时前
Java 设计模式——适配器模式进阶:原理深挖、框架应用与实战扩展
java·设计模式·适配器模式