springcloud是基于springboot的一套微服务的解决方案,springboot可以快速构建单个应用服务,而springcloud没有重复造轮子而是将现有的技术(服务发现,负载均衡等)整合到一起提供一套分布式服务解决方案。

整体的项目结构

以上是整个项目的结构块

父类gradle.build引入

buildscript {ext {springBootVersion = '1.5.10.RELEASE'}repositories {mavenCentral()}dependencies {classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")}
}subprojects {apply plugin: 'java'apply plugin: 'idea'apply plugin: 'org.springframework.boot'group = 'com.rk.ytl'version = '0.0.1-SNAPSHOT'sourceCompatibility = 1.8repositories {mavenLocal()mavenCentral()}ext {springCloudVersion = 'Edgware.SR3'}dependencies {compile('org.springframework.boot:spring-boot-starter-web')compile('org.springframework.cloud:spring-cloud-starter')
//        compile('org.springframework.cloud:spring-cloud-starter-eureka')
//        compile('org.springframework.cloud:spring-cloud-starter-eureka-server')runtime('org.springframework.boot:spring-boot-devtools')testCompile('org.springframework.boot:spring-boot-starter-test')}dependencyManagement {imports {mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"}}
}

View Code

首先创建注册中心register-center注册中心,这里注册中心就不多做介绍了。注册中心的启动类

package com.rk.ytl.register;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;/*** @author 杨天乐* @date 2018/3/29 9:59*/
@SpringBootApplication
@EnableEurekaServer
public class RegisterCenterApplication {public static void main(String[] args) {SpringApplication.run(RegisterCenterApplication.class,args);}
}

View Code

建立注册中心的配置(application)

spring:application:name: register-center
server:port: 8000
eureka:client:service-url:defaultZone: http://localhost:8000/eurekafetch-registry: falseregister-with-eureka: false

View Code

这里fetch-registry,register-with-eureka必须指定为false,表名是一个注册中心

注册中心gradle.build 引入

dependencies {compile('org.springframework.cloud:spring-cloud-starter-eureka-server')
}

注册中心就完成了。

创建业务接口和数据接口,这里就不做过多的讲解了。

service-api:

package com.rk.ytl.api;import com.rk.ytl.dto.StudentDTO;
import com.rk.ytl.vo.StudentVO;import java.util.List;/*** @author 杨天乐* @date 2018/3/29 10:18*/
public interface IStudentService {List<StudentDTO> selectAll();Integer LoadBalancedPortTest();
}

View Code

package com.rk.ytl.dto;import java.sql.Date;
import java.util.Objects;/*** @author 杨天乐* @date 2018/3/29 10:04*/
public class StudentDTO {private Integer id;private String stuName;private Integer age;private String time;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getStuName() {return stuName;}public void setStuName(String stuName) {this.stuName = stuName;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getTime() {return time;}public void setTime(String time) {this.time = time;}
}

View Code

package com.rk.ytl.vo;import java.sql.Date;
import java.util.Objects;/*** @author 杨天乐* @date 2018/3/29 10:04*/
public class StudentVO {private Integer id;private String stuName;private Integer age;private String time;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getStuName() {return stuName;}public void setStuName(String stuName) {this.stuName = stuName;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getTime() {return time;}public void setTime(String time) {this.time = time;}
}

View Code

project-dao:

package com.rk.ytl.dao.entity;import org.apache.ibatis.type.Alias;/*** @author 杨天乐* @date 2018/3/29 10:04*/
@Alias("StudentEntity")
public class StudentEntity {private Integer id;private String stuName;private Integer age;private String time;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getStuName() {return stuName;}public void setStuName(String stuName) {this.stuName = stuName;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getTime() {return time;}public void setTime(String time) {this.time = time;}
}

View Code

package com.rk.ytl.dao.mapper;import com.rk.ytl.dao.entity.StudentEntity;import java.util.List;/*** @author 杨天乐* @date 2018/3/29 10:08*/
public interface StudentMapper {List<StudentEntity> selectAll();
}

View Code

project-dao的gradle.build,引入mybatis和springboot的集成jar,已经mysql的jar

dependencies {compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.40'compile group: 'org.mybatis.spring.boot', name: 'mybatis-spring-boot-starter', version: '1.3.2'
}

View Code

接下来创建Student-service业务模块

build里引入project-dao,service-api的依赖

dependencies {compile project(':project-dao')compile project(':service-api')compile('org.springframework.cloud:spring-cloud-starter-eureka-server')
}

View Code

编写mybatis配置文件,application-local也是如下只是server.port端口不同(用于测试负载均衡)

spring:application:name: Student-servicedatasource:url: jdbc:mysql://localhost:3306/myschool?characterEncoding=utf-8&useSSL=falsedriver-class-name: com.mysql.jdbc.Driverusername: rootpassword: root
server:port: 8001
eureka:client:service-url:defaultZone: http://localhost:8000/eureka
mybatis:type-aliases-package: com.rk.ytl.dao.entitymapper-locations: mappers/*.xml

View Code

mappers文件下的studentMapper.xml对应project-dao接口的方法并实现

创建Student-service启动类

package com.ytl.student;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;/*** @author 杨天乐* @date 2018/3/29 10:17*/
@SpringBootApplication
@EnableEurekaServer
@MapperScan("com.rk.ytl.dao.mapper")
public class StudentServiceApplication {public static void main(String[] args) {SpringApplication.run(StudentServiceApplication.class,args);}
}

View Code

@MapperScan("")对应扫描project-dao的mapper包

创建service包,新建实现业务类

package com.ytl.student.service;import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.rk.ytl.api.IStudentService;
import com.rk.ytl.dao.entity.StudentEntity;
import com.rk.ytl.dao.mapper.StudentMapper;
import com.rk.ytl.dto.StudentDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Nullable;
import java.util.List;/*** @author 杨天乐* @date 2018/3/29 10:18*/
@RestController
@Service
public class StudentServiceImpl implements IStudentService {@Autowiredprivate StudentMapper studentMapper;@GetMapping("/selectAll")@Overridepublic List<StudentDTO> selectAll() {return Lists.transform(studentMapper.selectAll(), new Function<StudentEntity, StudentDTO>() {@Nullable@Overridepublic StudentDTO apply(@Nullable StudentEntity input) {StudentDTO studentDTO = new StudentDTO();BeanUtils.copyProperties(input,studentDTO);return studentDTO;}});}@Value("${server.port}")private Integer port;@RequestMapping("/test")@Overridepublic Integer LoadBalancedPortTest() {return port;}
}

View Code

最后一个Student-Client用于测试负载均衡

application.yml配置就是最基本的配置发现服务中心

spring:application:name: Student-Client
server:port: 8080
eureka:client:service-url:defaultZone: http://localhost:8000/eureka

View Code

Student-Client创建启动类

@LoadBalanced是关键,不加,不能实现负载均衡

package com.rk.ytl;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;/*** @author 杨天乐* @date 2018/3/29 10:39*/
@SpringBootApplication
@EnableDiscoveryClient
public class StudentClientApplication {@Bean@LoadBalancedRestTemplate template(){return new RestTemplate();}public static void main(String[] args) {SpringApplication.run(StudentClientApplication.class,args);}
}

View Code

创建controller用于测试

package com.rk.ytl.controller;import com.rk.ytl.dto.StudentDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;/*** @author 杨天乐* @date 2018/3/29 10:40*/
@RestController
public class TestController {@Autowiredprivate RestTemplate restTemplate;@RequestMapping(value = "/test")public Integer test(){return restTemplate.getForObject("http://STUDENT-SERVICE/test",Integer.class);}
}

View Code

url一定要对应业务名称

gradle.build引入eureka的客户端

dependencies {compile project(':service-api')compile('org.springframework.cloud:spring-cloud-starter-eureka')
}

View Code

一直访问localhost:8080/test就能看见端口在切换,实现了负载均衡。

学到希望大家给个赞,多评论,谢谢!

转载于:https://www.cnblogs.com/yangtianle/p/8669015.html

SpringCloud基本模块分配搭建以及负载均衡相关推荐

  1. Nginx系列1: 正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡

    一.什么是正向代理.什么是反向代理 1. 正向代理,意思是一个位于客户端和原始服务器(origin server)之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求并指定目标(原始服务器 ...

  2. 搭建K8S-web-MySQL-keepalived 负载均衡集群项目

    搭建K8S-web-MySQL-keepalived 负载均衡集群项目 部署目录 搭建K8S-web-MySQL-keepalived 负载均衡集群项目 总体架构图 安装MySQL 下载MySQL镜像 ...

  3. nginx+keepalived搭建主从负载均衡并迅速切换

    大家都听说过主从服务器或者负载均衡之类的专业术语,作为衡量一个中高级运维工程师的标准,集群和负载是运维工程师必须掌握的技术,然而在一家小公司是根本不会体会到运维的重要性的.首先从理论上讲一下,当用户量 ...

  4. Pacemaker,Corosync和PCS搭建高可用性负载均衡linux 集群(httpd)

    Pacemaker,Corosync和PCS搭建高可用性负载均衡linux 集群(httpd) 一.高可用性的概念: 1.一种机制来定义哪些系统可以被用作集群节点 2.哪些服务或者应用可以在节点间作失 ...

  5. Springcloud、Nacos 服务注册、负载均衡Ribbon、http客户端Feign

    SpringCloud是基于SpringBoot的一整套实现微服务的框架.它提供了微服务开发所需的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全局锁.决策竞选.分布式会话和集群状态管理等组 ...

  6. SpringCloud微服务-服务注册发现-负载均衡-服务调用-服务降级-服务网关-配置中心-消息总线-消息驱动-链路追踪-alibaba-nacos-sentinel-seata理论原理分析

    SpringCloud理论技术 概述 ​ Spring Cloud是一系列框架的有序集合.它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册.配置中心.消息总 ...

  7. Nginx系列篇二:linux搭建Nginx负载均衡

    建议先搭建好Nginx环境 可阅读--->Linux中搭建Nginx 1.准备好三台服务器[标配] 一.nginx负载均衡服务器:192.168.102.110,配置好Nginx 二.tomca ...

  8. SpringCloud Ribbon中的7种负载均衡策略!

    作者 | 磊哥 来源 | Java中文社群(ID:javacn666) 转载请联系授权(微信ID:GG_Stone) 负载均衡通器常有两种实现手段,一种是服务端负载均衡器,另一种是客户端负载均衡器,而 ...

  9. 手把手搭建简易负载均衡集群

    前言: 最近在公司部署了一个简易的负载均衡集群,这个集群的主要功能是分摊公司业务服务器的流量,防止服务器因为访问量过大而造成负,导致服务宕机或者响应速度慢等一系列问题.在这里做个简要的记录,方便后续自 ...

最新文章

  1. ARM Cortex-M嵌入式C基础编程(下)
  2. 【机器学习入门到精通系列】大规模机器学习图示
  3. C和C++栈stack
  4. java的reflection机制_Java的Reflection机制
  5. ORA-02287: sequence number not allowed here
  6. devserver配置_03-零基础学webpack4.0之html相关配置
  7. 今年考了N3也不知道能不能过
  8. 计算机组成原理中dubs是什么意思,计算机组成原理》课程设计报告.docx
  9. Mercurial使用简单介绍zz
  10. 大多数微型计算机都是基于,基于PCI总线数据采集系统的设计
  11. 十、基于FPGA的PCIE协议介绍(二)
  12. 关于MATLAB中使用latex语法
  13. Javaweb安装教程
  14. 英语语法基础入门怎么学好
  15. QuartusII9.0--项目文件的新建
  16. mysql cluster 分片_MySQL Cluster --01
  17. ShaderMap Pro(贴图制作转换生成工具)v1.3.1官方版
  18. HRNet-v1模型,用于人体形态检测
  19. Qt-利用fmod库显示声音波形
  20. MES系统价格具体跟哪些因素相关?

热门文章

  1. 修改win10 默认网卡 --其实就是改网卡接口跃点
  2. golang字符型及使用细节
  3. JVM调优:开启/关闭TLAB和逃逸分析耗时对比
  4. Python Django根据数据库表生成模型类的命令
  5. Lua 脚本获取 EVAL EVALSHA 命令的参数
  6. IDEA的GsonFormat插件--将json字符串直接实例化成类
  7. SQL 语句之insert语句插入数据:若表中有重复的主键或数据继续插入解决方案
  8. ThreadLocal两个简单示例
  9. python反爬虫策略ppt_了解python爬虫怎样对抗反爬虫策略-
  10. 查看docker运行状态_docker商业版受限?请了解下crio