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.8 repositories {
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}"
}
}
}

首先创建注册中心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);
}
}

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

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

这里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();
}
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;
}
}
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;
}
}

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;
}
}
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();
}

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'
}

接下来创建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')
}

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

spring:
application:
name: Student-service
datasource:
url: jdbc:mysql://localhost:3306/myschool?characterEncoding=utf-8&useSSL=false
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
server:
port: 8001
eureka:
client:
service-url:
defaultZone: http://localhost:8000/eureka
mybatis:
type-aliases-package: com.rk.ytl.dao.entity
mapper-locations: mappers/*.xml

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);
}
}

@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 { @Autowired
private StudentMapper studentMapper; @GetMapping("/selectAll")
@Override
public List<StudentDTO> selectAll() {
return Lists.transform(studentMapper.selectAll(), new Function<StudentEntity, StudentDTO>() {
@Nullable
@Override
public StudentDTO apply(@Nullable StudentEntity input) {
StudentDTO studentDTO = new StudentDTO();
BeanUtils.copyProperties(input,studentDTO);
return studentDTO;
}
});
} @Value("${server.port}")
private Integer port; @RequestMapping("/test")
@Override
public Integer LoadBalancedPortTest() {
return port;
}
}

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

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

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

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
@LoadBalanced
RestTemplate template(){
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(StudentClientApplication.class,args);
}
}

创建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 { @Autowired
private RestTemplate restTemplate; @RequestMapping(value = "/test")
public Integer test(){
return restTemplate.getForObject("http://STUDENT-SERVICE/test",Integer.class);
}
}

url一定要对应业务名称

gradle.build引入eureka的客户端

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

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

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

SpringCloud基本模块分配搭建以及负载均衡的更多相关文章

  1. springcloud(十二):Ribbon客户端负载均衡介绍

    springcloud(十二):Ribbon客户端负载均衡介绍 Ribbon简介 使用分布式微服务脚骨的应用系统,在部署的时候通常会为部分或者全部微服务搭建集群环境,通过提供多个实例来提高系统的稳定型 ...

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

    转自https://www.cnblogs.com/leeSmall/p/9351343.html 仅供个人学习 一.什么是正向代理.什么是反向代理 1. 正向代理,意思是一个位于客户端和原始服务器( ...

  3. SpringCloud全家桶学习之Feign负载均衡----Feign(四)

    一.Feign概述 (1)Feign是什么? 官网地址:https://projects.spring.io/spring-cloud/spring-cloud.html#spring-cloud-f ...

  4. 企业高并发的成熟解决方案(一)----搭建LVS负载均衡

    企业整个架构分析 1. App服务器上边部署应用,如果是java的话,一般是tomcat: 2. 负载均衡服务器负责转发请求,这种既有主机又有备机的负载均衡成为高可用(HA): 3. 一般web服务器 ...

  5. Net分布式系统之二:CentOS系统搭建Nginx负载均衡

    一.关于CentOS系统介绍 CentOS(Community Enterprise Operating System,中文意思是:社区企业操作系统)是Linux发行版之一,它是来自于Red Hat ...

  6. 《搭建DNS负载均衡服务》RHEL6

    搭建DNS负载均衡环境: 1.至少三台的linux虚拟机,一台主的DNS服务器,1台副的(可以N台),1台测试机. 负载均衡有很多种,apache那样的是为了缓解人们访问网站时给服务器造成太大的压力, ...

  7. 使用IIS Server Farms搭建应用服务负载均衡

    当公司的业务扩大, 伴随着大量的请求,应用服务器的承受能力已经不能满足不断增长的业务需求,使用IIS Server Farms搭建应负载均衡的方式,把请求分发给不同的应用服务器进行处理,这个时候就降低 ...

  8. 用 LVS 搭建一个负载均衡集群(转)

    http://blog.jobbole.com/87503/ 第一篇:<如何生成每秒百万级别的 HTTP 请求?> 第二篇:<为最佳性能调优 Nginx> 第三篇:<用 ...

  9. docker 搭建 nginx负载均衡

    本文描述如何在一台机器上搭建nginx负载均衡,我将会启动3个nginx的docker,分别是1台前置nginx负责分发,后面2台负责处理请求. 首先我切换到/usr/local/docker/文件夹 ...

随机推荐

  1. 封装:简要介绍自定义开发基于WPF的MVC框架

    原文:封装:简要介绍自定义开发基于WPF的MVC框架 一.目的:在使用Asp.net Core时,深感MVC框架作为页面跳转数据处理的方便,但WPF中似乎没有现成的MVC框架,由此自定义开发一套MVC ...

  2. C# 填充客户端提交的值到T对象

    /// <summary>      /// 填充客户端提交的值到 T 对象  如appinfo = AppConvert.To<Appinfo>(context.Reques ...

  3. C# 接口、抽象类、以及事件

    接口.抽象类,用于项目集成,如: Interface icls = appid == "A" ? new ClassA() : new ClassA();icls.func(&qu ...

  4. 2019 拉卡拉java面试笔试题 (含面试题解析)

      本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.拉卡拉等公司offer,岗位是Java后端开发,因为发展原因最终选择去了拉卡拉,入职一年时间了,也成为了面试官 ...

  5. Celery在Django中的使用介绍

    Celery在Django中的使用介绍 Celery简介 celery是一个简单.灵活且可靠的,处理大量消息的分布式系统,并且提供维护这样一个系统的必须工具. 它是一个专注于实时处理的任务队列,同时也 ...

  6. drf--视图家族

    目录 drf 视图家族 前期准备 总路由 urls.py 基表:utils/models.py 模型层 api/models.py 序列化器 api/serializers.py 基本视图(views ...

  7. 配置集成测试环境 phpstudy

    phpStudy是一个PHP调试环境的程序集成包,该程序包集成最新的Apache+PHP+MySQL+phpMyAdmin+ZendOptimizer,一次性安装,无须配置即可使用,是非常方便.好用的 ...

  8. SpringBoot+Security+MyBatis+ES+MQ+Redis+Docker+Vue的电商系统

    今天鹏哥给大家推荐的项目是一套电商系统,包括前台商城系统及后台管理系统,基于SpringBoot+MyBatis实现. 前台商城系统包含首页门户.商品推荐.商品搜索.商品展示.购物车.订单流程.会员中 ...

  9. BBC评出的100本最具影响力经典书籍

    今年,英国广播公司(BBC)邀请全球35个国家共108名文化人士,参与其发起的“影响思维和历史的100部虚构故事”的推荐,要求每人最多提名 5 部作品,这些作品最终将根据提名总量排名. 该活动经过一个 ...

  10. 【Maven】Maven中排除依赖、归类依赖、优化依赖

    参考博文:Maven中排除依赖.归类依赖.优化依赖