javascript
springcloud基本模块分配搭建以及负载均衡 -凯发k8官方网
springcloud是基于springboot的一套微服务的凯发k8官方网的解决方案,springboot可以快速构建单个应用服务,而springcloud没有重复造轮子而是将现有的技术(服务发现,负载均衡等)整合到一起提供一套分布式服务凯发k8官方网的解决方案。
整体的项目结构
以上是整个项目的结构块
父类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 {listproject-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 {listproject-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 codemappers文件下的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最后一个student-client用于测试负载均衡
application.yml配置就是最基本的配置发现服务中心
spring:application:name: student-client server:port: 8080 eureka:client:service-url:defaultzone: http://localhost:8000/eureka view codestudent-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 codeurl一定要对应业务名称
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
总结
以上是凯发k8官方网为你收集整理的springcloud基本模块分配搭建以及负载均衡的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇:
- 下一篇: 不要機翻謝謝!!