目录
SpringCloud 智慧医疗整体项目结构说明:
smart-medical 项目创建
smart-medical-common 通用模块创建
smart-medical-euraka-server 服务注册中心项目
smart-medical-provider 服务提供者服务
smart-medical-comsumer-ribbon 基于Ribbon 创建服务消费者
smart-medical-consumer-feign 基于Feign 服务消费者
smart-medical-zuul 网关服务
SpringCloud 智慧医疗整体项目结构说明:
smart-medical 项目创建
1、IDEA 创建smart-medical maven项目
2、依次创建子项目:
smart-medical-euraka-server smart-medical-provider smart-medical-comsumer-ribbon smart-medical-common smart-medical-consumer-feign smart-medical-zuul
各个子模块的创建参考截图进行创建
3、pom.xml 文件添加SpringBoot 版本和SpringCloud版本信息
4.0.0 org.springframework.boot spring-boot-starter-parent2.1.0.RELEASE smart-medical-euraka-server smart-medical-provider smart-medical-comsumer-ribbon smart-medical-common smart-medical-consumer-feign smart-medical-zuul org.example smart-medical1.0-SNAPSHOT pom 1.8 8 8 Greenwich.SR5 org.springframework.cloud spring-cloud-dependencies${spring-cloud.version} pom import
smart-medical-common 通用模块创建
项目结构截图:
1、pom.xml 文件
smart-medical org.example 1.0-SNAPSHOT 4.0.0 smart-medical-common1.8 8 8
2、添加包名:com.zzg.model com.zzg.service
package com.zzg.model;
import java.io.Serializable;
import java.util.Date;
public class User implements Serializable {
private String id;
private String account;
private String name;
private String passwd;
private Date createDt;
private String createBy;
private Date updateDt;
private String updateBy;
private Byte state;
private static final long serialVersionUID = 1L;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id == null ? null : id.trim();
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account == null ? null : account.trim();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd == null ? null : passwd.trim();
}
public Date getCreateDt() {
return createDt;
}
public void setCreateDt(Date createDt) {
this.createDt = createDt;
}
public String getCreateBy() {
return createBy;
}
public void setCreateBy(String createBy) {
this.createBy = createBy == null ? null : createBy.trim();
}
public Date getUpdateDt() {
return updateDt;
}
public void setUpdateDt(Date updateDt) {
this.updateDt = updateDt;
}
public String getUpdateBy() {
return updateBy;
}
public void setUpdateBy(String updateBy) {
this.updateBy = updateBy == null ? null : updateBy.trim();
}
public Byte getState() {
return state;
}
public void setState(Byte state) {
this.state = state;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", id=").append(id);
sb.append(", account=").append(account);
sb.append(", name=").append(name);
sb.append(", passwd=").append(passwd);
sb.append(", createDt=").append(createDt);
sb.append(", createBy=").append(createBy);
sb.append(", updateDt=").append(updateDt);
sb.append(", updateBy=").append(updateBy);
sb.append(", state=").append(state);
sb.append(", serialVersionUID=").append(serialVersionUID);
sb.append("]");
return sb.toString();
}
}
package com.zzg.service;
import com.zzg.model.User;
import java.util.List;
public interface UserService {
List selectAll();
}
smart-medical-euraka-server 服务注册中心项目
1、pom.xml 添加eurake 服务jar 包依赖
smart-medical org.example 1.0-SNAPSHOT 4.0.0 smart-medical-euraka-server1.8 8 8 org.springframework.boot spring-boot-starterorg.springframework.boot spring-boot-starter-testorg.springframework.boot spring-boot-starter-weborg.springframework.cloud spring-cloud-starter-netflix-eureka-server
2、添加Eureka 服务注册中心的SpringBoot 程序入口。
package com.zzg;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurakaApplication {
public static void main(String[] args) {
SpringApplication.run(EurakaApplication.class, args);
}
}
3、添加application.properties 配置文件
server.port=8081 # ???? ??????? eureka.instance.hostname=eureka-server # eureka client ???????eureka client ?????? eureka.client.register-with-eureka=false # ?????????eureka server ??eureka???? eureka.client.fetch-registry=false #?????? eureka.server.enable-self-preservation=false #??????????????????60?? eureka.server.eviction-interval-timer-in-ms=1000 eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8081/eureka-server/
smart-medical-provider 服务提供者服务
项目截图:
1、pom.xml 添加eureka 客户端jar 、MyBatis、MySQL8、Alibaba Druid和智慧医疗通用项目模块依赖。
smart-medical org.example 1.0-SNAPSHOT 4.0.0 smart-medical-provider1.8 8 8 org.springframework.boot spring-boot-starterorg.springframework.boot spring-boot-starter-testorg.springframework.boot spring-boot-starter-weborg.springframework.cloud spring-cloud-starter-netflix-eureka-clientorg.mybatis.spring.boot mybatis-spring-boot-starter1.3.2 com.alibaba druid-spring-boot-starter1.1.10 mysql mysql-connector-java8.0.12 org.example smart-medical-common1.0-SNAPSHOT
2、添加Provider SpringBoot程序入口、Mapper 映射接口、Mapper 映射xml和Controller层
package com.zzg;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@MapperScan("com.zzg.mapper")
@EnableEurekaClient
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
package com.zzg.mapper;
import com.zzg.model.User;
import java.util.List;
public interface UserMapper {
int deleteByPrimaryKey(String id);
int insert(User record);
User selectByPrimaryKey(String id);
List selectAll();
int updateByPrimaryKey(User record);
}
delete from house_user where id = #{id,jdbcType=VARCHAR} insert into house_user (id, account, `name`, passwd, create_dt, create_by, update_dt, update_by, `state` ) values (#{id,jdbcType=VARCHAR}, #{account,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{passwd,jdbcType=VARCHAR}, #{createDt,jdbcType=TIMESTAMP}, #{createBy,jdbcType=VARCHAR}, #{updateDt,jdbcType=TIMESTAMP}, #{updateBy,jdbcType=VARCHAR}, #{state,jdbcType=TINYINT} ) update house_user set account = #{account,jdbcType=VARCHAR}, `name` = #{name,jdbcType=VARCHAR}, passwd = #{passwd,jdbcType=VARCHAR}, create_dt = #{createDt,jdbcType=TIMESTAMP}, create_by = #{createBy,jdbcType=VARCHAR}, update_dt = #{updateDt,jdbcType=TIMESTAMP}, update_by = #{updateBy,jdbcType=VARCHAR}, `state` = #{state,jdbcType=TINYINT} where id = #{id,jdbcType=VARCHAR}
package com.zzg.controller;
import com.zzg.mapper.UserMapper;
import com.zzg.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Collection;
import java.util.List;
@RestController
public class UserController {
@Autowired
private UserMapper mapper;
@RequestMapping("/list")
public List get() {
List list = mapper.selectAll();
return list;
}
}
3、添加application.properties 配置文件
server.port=8084 server.servlet.context-path=/provider # ??????????spring.application.name ???? spring.application.name=provider1 eureka.instance.instance-id=provider1 eureka.client.register-with-eureka=true # ?eureka??????URL eureka.client.service-url.defaultZone=http://127.0.0.1:8081/eureka/ # ?????????????30?? eureka.instance.lease-renewal-interval-in-seconds=5 # eureka server ?????????????????????client????90?? eureka.instance.lease-expiration-duration-in-seconds=3 # ??????? spring.datasource.url=jdbc:mysql://localhost:3306/house?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.type=com.alibaba.druid.pool.DruidDataSource # druid ?? # ????????????? spring.datasource.druid.initial-size=5 # ??????? spring.datasource.druid.max-active=30 # ??????? spring.datasource.druid.min-idle=5 # ???????????????? spring.datasource.druid.max-wait=60000 # ??????????????????????????????? spring.datasource.druid.time-between-eviction-runs-millis=60000 # ???????????????? spring.datasource.druid.min-evictable-idle-time-millis=300000 # ???????????sql?????????? spring.datasource.druid.validation-query=SELECT 1 FROM DUAL # ?????true?????????????????????????????????timeBetweenEvictionRunsMillis???validationQuery????????? spring.datasource.druid.test-while-idle=true # ???????validationQuery????????????????????? spring.datasource.druid.test-on-borrow=false # ???????validationQuery????????????????????? spring.datasource.druid.test-on-return=false # ????preparedStatement????PSCache?PSCache???????????????????oracle??mysql?????? spring.datasource.druid.pool-prepared-statements=true # ???PSCache???????0????0??poolPreparedStatements???????true? spring.datasource.druid.max-pool-prepared-statement-per-connection-size=50 # ?????????filters????????sql???? spring.datasource.druid.filters=stat,wall,slf4j # ??connectProperties?????mergeSql????SQL?? spring.datasource.druid.connection-properties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500 # ????DruidDataSource????? spring.datasource.druid.use-global-data-source-stat=true ##### WebStatFilter?? ####### #??StatFilter spring.datasource.druid.web-stat-filter.enabled=true #?????? spring.datasource.druid.web-stat-filter.url-pattern=/* #????????url spring.datasource.druid.web-stat-filter.exclusions=*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/* #??session???? spring.datasource.druid.web-stat-filter.session-stat-enable=true #??sessionStatMaxCount?1000? spring.datasource.druid.web-stat-filter.session-stat-max-count=1000 #spring.datasource.druid.web-stat-filter.principal-session-name= #spring.datasource.druid.web-stat-filter.principal-cookie-name= #spring.datasource.druid.web-stat-filter.profile-enable= ##### StatViewServlet?? ####### #????????? spring.datasource.druid.stat-view-servlet.enabled=true #????????? spring.datasource.druid.stat-view-servlet.url-pattern=/druid/* #?? Reset All ?? spring.datasource.druid.stat-view-servlet.reset-enable=false #??????? spring.datasource.druid.stat-view-servlet.login-username=admin #?????? spring.datasource.druid.stat-view-servlet.login-password=123 #??????allow????????????????? spring.datasource.druid.stat-view-servlet.allow=127.0.0.1 #????deny???allow????deny???????allow?????????? spring.datasource.druid.stat-view-servlet.deny= # mybatis ?? mybatis.mapper-locations=classpath:mapper/*.xml mybatis.type-aliases-package=com.zzg.model # ???????? logging.level.com.zzg.mapper=debug
smart-medical-comsumer-ribbon 基于Ribbon 创建服务消费者
1、项目结构截图:
2、pom.xml 添加eureka 客户端jar 包依赖、hystrix 熔断器jar包依赖和智慧医疗通用模块依赖
smart-medical org.example 1.0-SNAPSHOT 4.0.0 smart-medical-comsumer-ribbon1.8 8 8 org.springframework.boot spring-boot-starterorg.springframework.boot spring-boot-starter-testorg.springframework.boot spring-boot-starter-weborg.springframework.cloud spring-cloud-starter-netflix-eureka-clientorg.springframework.cloud spring-cloud-starter-netflix-hystrixorg.example smart-medical-common1.0-SNAPSHOT
2、添加hystrix 熔断器 配置、Ribbon 程序入口和服务消费者实现Controller
package com.zzg.config;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RibbonConfig {
@Bean
@LoadBalanced //负载均衡标识
RestTemplate restTemplate() {
return new RestTemplate();
}
}
package com.zzg.controller;
import com.zzg.model.User;
import com.zzg.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/list")
public List get() {
List list = userService.selectAll();
return list;
}
}
package com.zzg.service;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.zzg.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
RestTemplate template;
@Override
@HystrixCommand(fallbackMethod = "ServiceError")
public List selectAll() {
String url ="http://provider1/provider/list";
return template.getForObject(url, List.class);
}
public List ServiceError() {
System.out.println("熔断机制启动, Provider 服务提供者异常");
return null;
}
}
package com.zzg;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
public class RibbonApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
}
3、添加application.properties 配置文件
server.port=8085 server.servlet.context-path=/consumer-ribbon spring.application.name=consumer-ribbon eureka.client.service-url.defaultZone=http://127.0.0.1:8081/eureka/
smart-medical-consumer-feign 基于Feign 服务消费者
1、项目结构截图:
2、pom.xml 文件添加 添加eureka 客户端jar 包依赖、hystrix 熔断器jar包依赖和智慧医疗通用模块依赖
smart-medical org.example 1.0-SNAPSHOT 4.0.0 smart-medical-consumer-feign1.8 8 8 org.springframework.boot spring-boot-starterorg.springframework.boot spring-boot-starter-testorg.springframework.boot spring-boot-starter-weborg.springframework.cloud spring-cloud-starter-netflix-eureka-clientorg.springframework.cloud spring-cloud-starter-openfeignorg.example smart-medical-common1.0-SNAPSHOT
2、添加hystrix 熔断器 配置、Feign程序入口和服务消费者实现Controller
package com.zzg.controller;
import com.zzg.model.User;
import com.zzg.service.UserServiceFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserFeignController {
@Autowired
UserServiceFeign userServiceFeign;
@RequestMapping(value = "/list")
public List selectAll() {
return userServiceFeign.selectAll();
}
}
package com.zzg.hystric;
import com.zzg.model.User;
import com.zzg.service.UserServiceFeign;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class UserServiceHystric implements UserServiceFeign {
@Override
public List selectAll() {
System.out.println("熔断机制启动, Provider 服务提供者异常");
return null;
}
}
package com.zzg.service;
import com.zzg.hystric.UserServiceHystric;
import com.zzg.model.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;
@FeignClient(value = "provider1",fallback = UserServiceHystric.class)
@Service
public interface UserServiceFeign {
@GetMapping("/provider/list")
List selectAll();
}
package com.zzg;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
}
3、添加application.properties 配置文件
server.port=8086 spring.application.name=consumer-feign eureka.client.service-url.defaultZone=http://127.0.0.1:8081/eureka/ # ?????? feign.hystrix.enabled=true
smart-medical-zuul 网关服务
1、项目截图:
2、添加zuul 网关服务程序入口
package com.zzg;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
}
}
3、添加application.properties 配置文件
server.port=8087 spring.application.name=zuul eureka.client.service-url.defaultZone=http://127.0.0.1:8081/eureka/
Github地址:git@github.com:zhouzhiwengang/SpringCloud-.git



