【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

学习笔记

相关推荐
当归10245 小时前
SQL Server死锁排查实战指南
java·服务器·网络
echoyu.5 小时前
消息队列-初识kafka
java·分布式·后端·spring cloud·中间件·架构·kafka
little_xianzhong6 小时前
关于对逾期提醒的定时任务~改进完善
java·数据库·spring boot·spring·mybatis
百锦再6 小时前
脚本语言的大浪淘沙或百花争艳
java·开发语言·人工智能·python·django·virtualenv·pygame
程序猿阿伟6 小时前
《云原生微服务治理进阶:隐性风险根除与全链路能力构建》
微服务·云原生·架构
小猪咪piggy6 小时前
【JavaEE】(23) 综合练习--博客系统
java·数据库·java-ee
周航宇JoeZhou6 小时前
JP4-7-MyLesson后台前端(五)
java·前端·vue·elementplus·前端项目·mylesson·管理平台
David爱编程6 小时前
从 JVM 到内核:synchronized 与操作系统互斥量的深度联系
java·后端
渣哥6 小时前
Java Set 不会重复?原来它有“记仇”的本事!
java
一叶飘零_sweeeet6 小时前
从 0 到 1 攻克订单表分表分库:亿级流量下的数据库架构实战指南
java·数据库·mysql·数据库架构·分库分表