service是spring-cloud中的微服务节点,具体实现了模块的业务(用户中心、工作流中心、某业务模块等等),同样支持负载。
增加依赖
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
com.alibaba
druid
1.1.5
com.baomidou
mybatisplus-spring-boot-starter
1.0.5
com.baomidou
mybatis-plus
2.1.9
增加了web、mybatis-plus、druid的支持,基本的业务模块这几个应该是需要的,后续再补充定时任务、MQ、redis等
yml配置
# 公共配置
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8010/eureka/ #eureka服务注册地址
mybatis-plus:
mapper-locations: classpath:/com/eqlian/*/mapper/*Mapper.xml
#实体扫描,多个package用逗号或者分号分隔
typeAliasesPackage: com.**.entity
global-config:
#主键类型 0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";
id-type: 3
#字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断"
field-strategy: 2
#驼峰下划线转换
db-column-underline: true
#刷新mapper 调试神器
refresh-mapper: true
configuration:
map-underscore-to-camel-case: true
cache-enabled: false
spring:
datasource:
name: test
url: jdbc:mysql://192.168.0.1:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false
username: root
password: root
# 使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
application:
name: service-system
server:
port: 8911
启动类
@ComponentScan("com.**.controller")
@ComponentScan("com.**.service")
@EnableEurekaClient
@SpringBootApplication
public class SmApplication {
public static void main(String[] args) {
SpringApplication.run(SmApplication.class, args);
}
}
config
@Configuration
@MapperScan("com.eqlian.**.mapper")
public class MybatisPlusConfig {
/*
* 分页插件,自动识别数据库类型
* 多租户,请参考官网【插件扩展】
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
@Component
public class SpringContextAware implements ApplicationContextAware {
private static ApplicationContext applicationContext;
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextAware.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static T getBean(String name){
return (T) applicationContext.getBean(name);
}
}
controller
@RestController
@RequestMapping(value = "/user")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public User get(@PathVariable("id") String id) {
return userService.getById(id);
}
}
service
@Service
public class UserService extends ServiceImpl implements IUserService {
@Override
public User getById(String id) {
return selectById(id);
}
}
mapper
public interface UserMapper extends BaseMapper {
// 这里可写其他复杂查询,简单的增删改查用serviceimpl提供的接口即可满足
}
entity
@TableName("t_user")
@Data
public class User extends Model {
@TableId(value = "pk_user", type = IdType.UUID)
private String pkUser;
/**
* 姓名
*/
private String name;
/**
* 密码
*/
private String password;
/**
* 用户名、登录名
*/
private String username;
/**
* 状态:0=启用,1=禁用
*/
private Integer status;
@Override
protected Serializable pkVal() {
return getPkUser();
}
}
基本的查询就写完了,启动服务自动连接到注册中心。
这里接口的地址是 http://localhost:8911/user/{id} 但我们肯定不会把这个地址暴露出去
zuul网关这时会把这个接口代理出去,所以前端无需关心微服务真正的ip地址,只需要请求zuul代理的路径即可。
再讲一下zuul代理的规则
zuul:
routes:
sm:
path: /demo/system/**
serviceId: service-system
可以看到zuul里配置路由规则是将所有/demo/system/**的请求路由到service-system这个微服务上,而service-system则是咱们在yml中设置的微服务id。
至此一个最简单的查询模块就开发和发布完成了。
PS: 如何负载均衡?
很简答,你这个微服务模块启动2个进程注册到注册中心就会自动负载了,serviceid相同但ip:port不同。