springcloud-1. 简单入门

目标

创建springcloud-demo父工程, 用户服务工程 userservice, 用户服务消费工程userconsumer

分析

需求: 查询数据库中的数据

  • 父工程springcloud-demo: 添加springboot maven父坐标和管理其他组件依赖
  • 用户服务工程 userservice: 整合mybatis查询数据库中的用户
  • 用户服务消费工程userconsumer: 调用服务提供者的服务,查询数据

开始

创建父工程

  • maven依赖
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
        <mapper.starter.version>2.1.5</mapper.starter.version>
        <mysql.version>8.0.20</mysql.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!-- springCloud -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- 通用Mapper启动器 -->
            <dependency>
                <groupId>tk.mybatis</groupId>
                <artifactId>mapper-spring-boot-starter</artifactId>
                <version>${mapper.starter.version}</version>
            </dependency>
            <!-- mysql驱动 -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-config</artifactId>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

搭建配置user-service工程

  • maven依赖
  <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 通用Mapper启动器 -->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
        </dependency>
        <!-- mysql驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>
  • springboot启动器
@SpringBootApplication
// 配置mapper包扫描
@MapperScan("top.itkaoti.user.mapper")
public class UserApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserApplication.class, args);
    }
}

  • 实体类
@Table(name = "user")
@Data
public class User {

    @Id
    // 开启主键自动回填
    @KeySql(useGeneratedKeys = true)
    private Integer uid;

    private String username;

    private Integer age;

    private Integer gender;

}

  • mapper

继承通用mapper

import tk.mybatis.mapper.common.Mapper;
import top.itkaoti.user.pojo.User;

// 继承通用mapper8
public interface UserMapper extends Mapper<User> {
}

  • service
public interface UserService {

    public User queryById(Integer id);
}


@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User queryById(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }
}
  • Controller
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/{id}")
    public User queryById(@PathVariable Integer id){
        User user = userService.queryById(id);
        return user;
    }

}

  • 配置文件
server:
  port: 8080
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/springcloud
    username: root
    password: root


mybatis:
  type-aliases-package: top.itkaoti.user.pojo

  • 浏览器访问
http://127.0.0.1:8080/user/1

搭建配置user-consumer工程

  • 配置maven依赖
 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

  • 实体类
@Data
public class User {

    private Integer uid;

    private String username;

    private Integer age;

    private Integer gender;

}
  • contoller
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/{id}")
    public User queryById(@PathVariable Integer id){
        User user = restTemplate.getForObject("http://127.0.0.1:8080/user/" + id, User.class);
        return user;
    }

}
  • springboot启动类
@SpringBootApplication
public class UserApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserApplication.class, args);
    }

    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

  • 配置文件
server:
  port: 8081

  • 访问
http://127.0.0.1:8080/user/2

总结

现在我们想想在上面的代码中存在的问题?

  • user-consumer 在获取user-service 的服务是写死的, 不方便日后维护
  • user-service 存在单点问题, 容易down机, 导致服务不可用
  • 如果user-service 在多台机器部署, 那么user-consumer如何链接user-service, 我们还要自己实现负载均衡