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}\" } }}
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-centerserver:
port: 8000eureka: client:
service-url:
defaultZone: http://localhost:8000/eureka fetch-registry: false
register-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 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 Codeproject-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 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-service datasource: url: jdbc:mysql://localhost:3306/myschool?characterEncoding=utf-8&useSSL=false driver-class-name: com.mysql.jdbc.Driver username: root password: rootserver: port: 8001eureka: client: service-url: defaultZone: http://localhost:8000/eurekamybatis: type-aliases-package: com.rk.ytl.dao.entity mapper-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 { @Autowired private StudentMapper studentMapper; @GetMapping(\"/selectAll\") @Override public List return Lists.transform(studentMapper.selectAll(), new Function 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; }} View Code 最后⼀个Student-Client⽤于测试负载均衡 application.yml配置就是最基本的配置发现服务中⼼ spring: application: name: Student-Clientserver: port: 8080eureka: 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 @LoadBalanced RestTemplate 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 { @Autowired private RestTemplate restTemplate; @RequestMapping(value = \"/test\") public Integer test(){ return restTemplate.getForObject(\"http://STUDENT-SERVICE/test\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就能看见端⼝在切换,实现了负载均衡。 学到希望⼤家给个赞,多评论,谢谢! 因篇幅问题不能全部显示,请点此查看更多更全内容