使用 SpringBoot + SpringCloud 构建分布式、负载均衡系统

一、前言

之前参与过 广东海洋大学学生宿舍系统 的开发,该系统是使用 Maven+SSM(Spring+SpringMVC+MyBatis)框架做后台的,也没有使用 Dubbo/SpringCloud 等分布式的架构,作为后期维护者,万一上线后访问量过大(几率较小),系统崩溃,就得对其架构进行改进了。
笔者有点打算后期使用 SpringBoot + SpringCloud 进行改写。
不过其是基于Maven,使用 Dubbo 进行改写的可能性会大点,不过笔者对里边代码存在的问题不是很满意,所以以 SpringBoot 为核心进行重构的可能性会大点。不过精力估计是不允许的,希望该系统风平浪静就好了。
Dubbo 架构具体可以参考之前的文章:在 Maven 工程中使用 Dubbo、ZooKeeper 实现集群负载均衡

二、代码与结构

1、注册中心

这里写图片描述
① RegisterApplication
仅添加注解:@EnableEurekaServer 的操作

package com.cun.register;

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

@EnableEurekaServer
@SpringBootApplication
public class RegisterApplication {

    public static void main(String[] args) {
        SpringApplication.run(RegisterApplication.class, args);
    }
}

② application.yml 配置

spring:
  application:
    name: consumer-message
server:
  port: 9600

eureka:
  instance:
    prefer-ip-address: true # 注册服务的时候使用服务的ip地址
  client:
    service-url:
      defaultZone: http://localhost:9500/eureka/

③ pom 依赖

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

④ 效果:http://localhost:9500
(红色框的内容为 Provider 启动后才有的)
这里写图片描述

2、服务提供者

这里写图片描述
① ProviderApplication (没改过)

package com.cun.provider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
    }
}

② MessageService

package com.cun.provider.service;

import org.springframework.stereotype.Service;

@Service
public class MessageService {

    public String getMessage(){
        return "ITAEM";
    }

}

③ MessageController

package com.cun.provider.controller;

import com.cun.provider.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MessageController {

    @Autowired
    MessageService messageService;

    @GetMapping("/get")
    public String getMessage(){
        return "provider提供信息:"+messageService.getMessage();
    }

}

④ application.yml 配置

server:
  port: 9400
spring:
  application:
    name: provider-message

eureka:
  instance:
    prefer-ip-address: true # 注册服务的时候使用服务的ip地址
  client:
    service-url:
      defaultZone: http://localhost:9500/eureka/

⑤ pom 依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>

⑥ 效果:http://localhost:9400/get
这里写图片描述

3、服务消费者

这里写图片描述
① ConsumerApplication
@EnableDiscoveryClient 开启发现服务功能、RestTemplate 使用负载均衡机制

package com.cun.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@EnableDiscoveryClient //开启发现服务功能
@SpringBootApplication
public class ConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }

    @LoadBalanced //使用负载均衡机制
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

② MessageController

package com.cun.consumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class MessageController {

    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/show")
    public String showMessage(){
        String s = restTemplate.getForObject("http://provider-message/get", String.class);
        return "consumer获取信息:"+s;
    }

}

③ application.yml 配置

spring:
  application:
    name: consumer-message
server:
  port: 9600

eureka:
  instance:
    prefer-ip-address: true # 注册服务的时候使用服务的ip地址
  client:
    service-url:
      defaultZone: http://localhost:9500/eureka/

④ pom 依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>

⑤ 效果:http://localhost:9600/show
这里写图片描述

三、其他

代码放在 github 上了:https://github.com/larger5/BootCloud.git

猜你喜欢

转载自blog.csdn.net/larger5/article/details/80549200