搜索
您的当前位置:首页正文

分布式微服务

来源:易榕旅网


认识微服务

单体服务架构的特点

分布式架构的特点

  1. 耦合度降低
  2. 可重用性高
  3. 开发效率高

微服务

微服务的架构特征:

SpringCloud

集成了各种微服务功能组件,并基于SpringBoot实现了这些组件的自动装配,从而提供了良好的开箱即用体验

总结

  • 例如:学生管理系统
    • 优点:拆分粒度更小、服务更独立、耦合度更低
    • 缺点:架构非常复杂,运维、监控、部署难度提高

服务拆分及远程调用

服务拆分原则

、单一职责:不同微服务,不要重复开发相同业务。

、数据独立:不要访问其他微服务的数据库

、面向服务:将自己的业务暴露为接口,供其他微服务调用

假如在订单服务中,需要一起返回用户的信息

 

我们的做法:

第一步:注册RestTemplate

package com.tledu.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);
    }

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

第二步:远程调用

 

Eureka注册中心

 

Eureka的结构和作用

 

 

搭建eureka-server

1.创建eureka-server服务

父工程下创建子工程 

2.引入eureka依赖

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

3.编写启动类

package com.tledu.eureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }
}

4.编写配置文件

server:
  port: 10086
spring:
  application:
    name: eureka-server
eureka:
  client:
    service-url: 
      defaultZone: http://127.0.0.1:10086/eureka

5.启动服务

 服务注册

1.引入依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

2.配置文件

spring:
  application:
    name: userservice
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka

3.启动user服务

 

 

 

服务发现

1.引入依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

2.配置文件

spring:
  application:
    name: orderservice
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka

3.服务拉取,负载均衡

 

 

因篇幅问题不能全部显示,请点此查看更多更全内容

Top