首页 > 其他分享 >SpringCloud实现简单的远程服务调用

SpringCloud实现简单的远程服务调用

时间:2022-11-08 17:57:09浏览次数:39  
标签:调用 SpringCloud boot public user spring id org 远程

一、服务的提供者。

1、Spring脚手架创建工程。

填写项目信息:

 添加web依赖:

 添加MyBatis依赖:

 填写项目位置:

 生成的项目结构:

 

 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.7.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.db.demo</groupId>
    <artifactId>user-service-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>user-service-demo</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.2</version>
        </dependency>

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </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>
            </plugin>
        </plugins>
    </build>

</project>

因为要使用通用的mapper,所以需要手动加入通用mapper的依赖:

<dependency>
    <groupId>tk.mybatis</groupId>
    <artifactId>mapper-spring-boot-starter</artifactId>
    <version>2.0.2</version>
</dependency>

2、编写代码。

2.1、创建一个Controller类,添加一个对外查询的接口:

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    public UserService userService;

    @GetMapping("/{id}")
    public User queryUserById(@PathVariable("id") Long id){
        return this.userService.queryUserById(id);
    }
}

2.2、创建service

@Service
public class UserService {
    @Autowired
    public UserMapper userMapper;
    public User queryUserById(Long id){
        return this.userMapper.selectByPrimaryKey(id);
    }
}

3.3、创建Mapper接口

@Mapper
public interface UserMapper extends tk.mybatis.mapper.common.Mapper<User> {
}

3.4、实体类。

@Table(name = "tb_user")
public class User implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    // 用户名
    private String userName;
    // 性别
    private String sex;
    // 出生日期
    private Date birthday;
    // 地址
    private String address;
    // 。。。省略getters和setters
}

 

3.5、配置文件:这里用的是yml属性文件。

server:
port: 8081
spring:
application:
name: item-service
datasource:
url: jdbc:mysql://localhost:3306/springboot
username: root
password: 123456
hikari:
maximum-pool-size: 30
minimum-idle: 10

3.6、项目结构:

 

 3.7、启动并测试。启动项目,访问接口  http://localhost:8081/user/1

 

二、服务的调用者。

1、创建方法与上面一样,需要注意的是这里调用的是user-service的功能,所以不需要mybatis相关依赖了。

2、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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.db.demo</groupId>
    <artifactId>user-consumer-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>user-consumer-demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 添加OkHttp支持 -->
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>3.9.0</version>
        </dependency>
    </dependencies>

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

</project>

3、编写代码。

3.1、在启动类中注册RestTemplate模板对象,向该模板对象中注入OkHttp客户端的工厂对象。

@SpringBootApplication
public class ComsumerDemoApplication {

    @Bean
    public RestTemplate getRestTemplate(){
        // 这次我们使用了OkHttp客户端,只需要注入工厂即可
        return new RestTemplate(new OkHttp3ClientHttpRequestFactory());
    }

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

3.2、编写UserDao,注意,这里不是调用mapper查数据库,而是通过RestTemplate远程查询user-service-demo中的接口:

@Component
public class UserDao {
    @Autowired
    public RestTemplate restTemplate;

    public User queryUserById(Long id){
        String url = "http://localhost:8081/user/" +id;
        return this.restTemplate.getForObject(url, User.class);
    }
}

3.3、编写UserService,循坏查询UserDao信息。

@Service
public class UserService {
    @Autowired
    public UserDao userDao;

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

3.4、编写controller。

@Controller
@RequestMapping("consume")
public class UserController {
    @Autowired
    public UserService userService;
    @GetMapping
    public List<User> comsume(@RequestParam("ids") List<Long> ids){
        return this.userService.queryUserByIds(ids);
    }
}

3.5、启动测试 ,因为没有配置端口,所以默认是8080,访问 http://localhost:8080/consume?ids=1,2

 

标签:调用,SpringCloud,boot,public,user,spring,id,org,远程
From: https://www.cnblogs.com/sfwu/p/16868811.html

相关文章