【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

学习笔记

相关推荐
噼里啪啦啦.9 分钟前
Spring事务和事务传播机制
数据库·sql·spring
盖世英雄酱5813610 分钟前
Java 内存管理技巧(新手必看集合篇)
java
码农小灰12 分钟前
Java 8 Stream API 入门到实践详解
java·java案例
步、步、为营18 分钟前
.NET 事件模式举例介绍
java·开发语言·.net
cui_hao_nan20 分钟前
设计模式——模板方法
java·设计模式
小吕学编程21 分钟前
HttpServletRequest常用方法
java·http
在未来等你21 分钟前
Java并发编程实战 Day 11:并发设计模式
java·设计模式·多线程·并发编程·threadlocal·生产者消费者·读写锁
李少兄33 分钟前
解决 idea提示`SQL dialect is not configured` 问题
java·sql·intellij-idea
BreezeDove39 分钟前
IDEA安装&迁移IDEA配置数据位置
java·ide·intellij-idea
太阳之神aboluo44 分钟前
压测软件-Jmeter
java·运维·jmeter