javascript
SpringCloud基本模块分配搭建以及负载均衡
springcloud是基于springboot的一套微服務(wù)的解決方案,springboot可以快速構(gòu)建單個(gè)應(yīng)用服務(wù),而springcloud沒(méi)有重復(fù)造輪子而是將現(xiàn)有的技術(shù)(服務(wù)發(fā)現(xiàn),負(fù)載均衡等)整合到一起提供一套分布式服務(wù)解決方案。
?
整體的項(xiàng)目結(jié)構(gòu)
以上是整個(gè)項(xiàng)目的結(jié)構(gòu)塊
父類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?
?
首先創(chuàng)建注冊(cè)中心register-center注冊(cè)中心,這里注冊(cè)中心就不多做介紹了。注冊(cè)中心的啟動(dòng)類
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 楊天樂(lè)* @date 2018/3/29 9:59*/ @SpringBootApplication @EnableEurekaServer public class RegisterCenterApplication {public static void main(String[] args) {SpringApplication.run(RegisterCenterApplication.class,args);} } View Code建立注冊(cè)中心的配置(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,表名是一個(gè)注冊(cè)中心
注冊(cè)中心gradle.build 引入
dependencies {compile('org.springframework.cloud:spring-cloud-starter-eureka-server') }注冊(cè)中心就完成了。
?
創(chuàng)建業(yè)務(wù)接口和數(shù)據(jù)接口,這里就不做過(guò)多的講解了。
service-api:
package com.rk.ytl.api;import com.rk.ytl.dto.StudentDTO; import com.rk.ytl.vo.StudentVO;import java.util.List;/*** @author 楊天樂(lè)* @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 楊天樂(lè)* @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 楊天樂(lè)* @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 Codeproject-dao:
package com.rk.ytl.dao.entity;import org.apache.ibatis.type.Alias;/*** @author 楊天樂(lè)* @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 楊天樂(lè)* @date 2018/3/29 10:08*/ public interface StudentMapper {List<StudentEntity> selectAll(); } View Codeproject-dao的gradle.build,引入mybatis和springboot的集成jar,已經(jīng)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?
接下來(lái)創(chuàng)建Student-service業(yè)務(wù)模塊
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編寫(xiě)mybatis配置文件,application-local也是如下只是server.port端口不同(用于測(cè)試負(fù)載均衡)
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 Codemappers文件下的studentMapper.xml對(duì)應(yīng)project-dao接口的方法并實(shí)現(xiàn)
創(chuàng)建Student-service啟動(dòng)類
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 楊天樂(lè)* @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("")對(duì)應(yīng)掃描project-dao的mapper包
創(chuàng)建service包,新建實(shí)現(xiàn)業(yè)務(wù)類
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 楊天樂(lè)* @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最后一個(gè)Student-Client用于測(cè)試負(fù)載均衡
application.yml配置就是最基本的配置發(fā)現(xiàn)服務(wù)中心
spring:application:name: Student-Client server:port: 8080 eureka:client:service-url:defaultZone: http://localhost:8000/eureka View CodeStudent-Client創(chuàng)建啟動(dòng)類
@LoadBalanced是關(guān)鍵,不加,不能實(shí)現(xiàn)負(fù)載均衡
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 楊天樂(lè)* @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創(chuàng)建controller用于測(cè)試
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 楊天樂(lè)* @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 Codeurl一定要對(duì)應(yīng)業(yè)務(wù)名稱
gradle.build引入eureka的客戶端
dependencies {compile project(':service-api')compile('org.springframework.cloud:spring-cloud-starter-eureka') } View Code?
一直訪問(wèn)localhost:8080/test就能看見(jiàn)端口在切換,實(shí)現(xiàn)了負(fù)載均衡。
?
學(xué)到希望大家給個(gè)贊,多評(píng)論,謝謝!
?
轉(zhuǎn)載于:https://www.cnblogs.com/yangtianle/p/8669015.html
總結(jié)
以上是生活随笔為你收集整理的SpringCloud基本模块分配搭建以及负载均衡的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。