用户服务搭建
- IDEA创建用户服务(File->new->project)
- 项目目录
- pom文件配置
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.8.RELEASE
com.dream
shop-provider-user
0.0.1-SNAPSHOT
shop
Demo project for Spring Boot
1.8
Greenwich.SR3
1.0.5
2.3
1.2.47
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.cloud
spring-cloud-function-context
org.springframework.cloud
spring-cloud-starter
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
com.baomidou
mybatisplus-spring-boot-starter
${mybatisplus-spring-boot-starter.version}
com.baomidou
mybatis-plus
${mybatis-plus.version}
com.baomidou
mybatis-plus-generate
${mybatis-plus.version}
com.alibaba
fastjson
${fastjson.version}
mysql
mysql-connector-java
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
- 相关代码
application.xml
server:
# 指定Tomcat端口
port: 8000
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/shop?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=UTC
username: root
password: root
db-name: shop
filters: wall,mergeStat
mybatis-plus:
mapper-locations: classpath:mapping/*.xml
typeAliasesPackage: com.dream.shop.model
configuration:
map-underscore-to-camel-case: true
#打印sql
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
logging:
level:
root: INFO
# 配置日志级别
MybatisPlusConfig.java
package com.dream.shop.config;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* MybatisPlus配置
*
* @author rain
*/
@Configuration
@MapperScan(basePackages = {"com.dream.shop.dao"})
public class MybatisPlusConfig {
/**
* mybatis-plus分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
UserController.java
package com.dream.shop.controller;
import com.dream.shop.dao.UserMapper;
import com.dream.shop.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.Optional;
/**
* @author rain
*/
@RequestMapping("/users")
@RestController
public class UserController {
@Resource
public UserMapper userMapper;
@GetMapping("/{id}")
public User findById(@PathVariable Long id) {
return this.userMapper.selectById(id);
}
}
UserMapper.xml
id,username, name, age, balance
UserMapper.java
package com.dream.shop.dao;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.dream.shop.model.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
/**
*
* 用户 Mapper 接口
*
*
* @author it-park-team
* @since 2018-11-29
*/
//@Mapper
public interface UserMapper extends BaseMapper {
}
User.java
package com.dream.shop.model;
import com.baomidou.mybatisplus.annotations.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.math.BigDecimal;
/**
* @author rain
*/
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column
private String username;
@Column
private String name;
@Column
private Integer age;
@Column
private BigDecimal balance;
}
启动
http://localhost:8000/users/1
{"id":1,"username":"rain","name":"rain","age":18,"balance":2.0}
电影微服务搭建
- IDEA创建同上(忽略)
- ** 项目目录
- 核心代码
MovieApplication.java
package com.dream.shop;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication//(scanBasePackages = {"com.dream.shop"})
public class MovieApplication {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(MovieApplication.class, args);
}
}
MovieController.java
package com.dream.shop.controller;
import com.dream.shop.dao.UserMapper;
import com.dream.shop.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.util.Optional;
/**
* @author rain
*/
@RequestMapping("/movies")
@RestController
public class MovieController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/users/{id}")
public User findById(@PathVariable Long id) {
// 这里用到了RestTemplate的占位符能力
User user = this.restTemplate.getForObject("http://localhost:8000/users/{id}", User.class, id);
// ...电影微服务的业务...
return user;
}
}
服务启动
http://localhost:8010/movies/users/1
{"id":1,"username":"rain","name":"rain","age":18,"balance":2.0}
注意事项
- @Autowired报警
解决方案:
1.使用@Resource
3.增加@MapperScan(basePackages = {"com.dream.shop.dao"})
存在问题
- 无监控,无画板,无指标,无可视化Dashboard
- 地址硬编码问题