用户服务搭建
- IDEA创建用户服务(File->new->project)
- 项目目录
- pom文件配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dream</groupId>
<artifactId>shop-provider-user</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>shop</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR3</spring-cloud.version>
<mybatisplus-spring-boot-starter.version>1.0.5</mybatisplus-spring-boot-starter.version>
<mybatis-plus.version>2.3</mybatis-plus.version>
<fastjson.version>1.2.47</fastjson.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatisplus-spring-boot-starter</artifactId>
<version>${mybatisplus-spring-boot-starter.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generate</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 相关代码
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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dream.shop.dao.UserMapper">
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.dream.shop.model.User">
<id column="id" property="id" />
<result column="username" property="username" />
<result column="name" property="name" />
<result column="age" property="age" />
<result column="balance" property="balance" />
</resultMap>
<!-- 通用查询结果列 -->
<sql id="Base_Column_List">
id,username, name, age, balance
</sql>
</mapper>
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;
/**
* <p>
* 用户 Mapper 接口
* </p>
*
* @author it-park-team
* @since 2018-11-29
*/
//@Mapper
public interface UserMapper extends BaseMapper<User> {
}
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
2.增加@Mapper注解
3.增加@MapperScan(basePackages = {"com.dream.shop.dao"})
存在问题
- 无监控,无画板,无指标,无可视化Dashboard
- 地址硬编码问题
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。