微服务快速入门

认识微服务

贴几个链接:

Sring Cloud中文网

Spring Cloud 英文教程

微服务来源

这里推荐一本书:Spring Cloud微服务实战。版本有点老了,但是里面干货不少,对于入门帮助极大。

具体介绍就不多打字了,直接贴教程吧。

服务提供方

顾名思义,提供服务的。

构建一个SpringBoot项目,就做一个最简单的查找。和普通Spring Boot项目没什么区别。

项目结构:

数据库:

pom.xml:

<?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.5.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.lzx</groupId>
    <artifactId>SpringCloud_021</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringCloud_021</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </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>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

User类:

package com.lzx.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Author 小先生
 * @Date 2021年06月07日13:20
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private int id;
    private String name;
    private String pwd;
}

UserDao:

package com.lzx.dao;

import com.lzx.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

/**
 * @Author 小先生
 * @Date 2021年06月07日13:25
 */
@Repository
@Mapper
public interface UserDao {
    User findUserById(Integer 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.lzx.dao.UserDao">
    <select id="findUserById" resultType="com.lzx.pojo.User">
        select * from user where id = #{id}
    </select>
</mapper>

UserServer:

package com.lzx.server;

import com.lzx.pojo.User;

/**
 * @Author 小先生
 * @Date 2021年06月07日13:28
 */
public interface UserServer {
    User findUserById(Integer id);
}

UserServerImpl:

package com.lzx.server;

import com.lzx.dao.UserDao;
import com.lzx.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author 小先生
 * @Date 2021年06月07日13:29
 */
@Service
public class UserServerImpl implements UserServer{
    @Autowired
    UserDao userDao;
    @Override
    public User findUserById(Integer id) {
        return userDao.findUserById(id);
    }
}

UserController:

package com.lzx.controller;

import com.lzx.pojo.User;
import com.lzx.server.UserServerImpl;
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;

/**
 * @Author 小先生
 * @Date 2021年06月07日13:30
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    UserServerImpl userServer;

    @GetMapping("/{id}")
    public User queryById(@PathVariable("id") int id) {
        return this.userServer.findUserById(id);
    }
}

application.properties:

spring.datasource.url=jdbc:mysql://localhost:3306/mybatis01?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=0913

mybatis.mapper-locations=classpath:mapper/*.xml

朴实无华,启动项目,输入:http://localhost:8080/user/1

服务消费方

服务消费方 就是获得提供方的服务,比如上面的提供方提供了按照id查询user的方法,那么消费方可以不编写dao层面连接数据库的代码,而是在dao层绑定链接。

话不多说,上代码!

项目结构:

pom.xml:

<?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.5.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.lzx</groupId>
    <artifactId>SpringCloud_022</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringCloud_022</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.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-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </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>
        <!-- 添加OkHttp支持 -->
        <!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>4.9.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

修改主启动类:

package com.lzx;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class SpringCloud022Application {
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate(new OkHttp3ClientHttpRequestFactory());
    }

    public static void main(String[] args) {
        SpringApplication.run(SpringCloud022Application.class, args);
    }

}

User:

package com.lzx.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @Author 小先生
 * @Date 2021年06月08日18:08
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private int id;
    private String name;
    private String pwd;
}

UserDao:

package com.lzx.dao;

import com.lzx.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * @Author 小先生
 * @Date 2021年06月08日18:07
 */
@Component
public class UserDao {
    @Autowired
    private RestTemplate restTemplate;
    public User findById(Long id){
        String url="http://localhost:8080/user/"+id;
        return this.restTemplate.getForObject(url,User.class);
    }
}

UserService:

package com.lzx.service;

import com.lzx.dao.UserDao;
import com.lzx.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author 小先生
 * @Date 2021年06月08日18:10
 */
@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    public List<User> findById(List<Long> ids){
        List<User> users = new ArrayList<>();
        for (Long id : ids) {
            User user = this.userDao.findById(id);
            users.add(user);
        }
        return users;
    }
}

ConsumeController:

package com.lzx.controller;

import com.lzx.pojo.User;
import com.lzx.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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author 小先生
 * @Date 2021年06月08日18:12
 */
@RestController
@RequestMapping("consume")
class ConsumerController {

    @Autowired
    private UserService userService;

    @GetMapping
    public List<User> consume(@RequestParam("ids") List<Long> ids) {
        return this.userService.findById(ids);
    }
}

application.properties修改端口:

server.port=8081

 这样再启动这个项目,输入:http://localhost:8081/consume?ids=1

还可以查询多个,输入:http://localhost:8081/consume?ids=1,2,3

 

搞定,下一篇写服务治理Eureka

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值