《SpringCloud专题04》-微服务架构编码构建-服务消费者

1.建cloud-consumer-order80

《SpringCloud专题04》-微服务架构编码构建-服务消费者_第1张图片

2.改POM


<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">
    <parent>
        <artifactId>springcloud2020artifactId>
        <groupId>com.itxiongmao.springcloudgroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>cloud-consumer-order80artifactId>

    <dependencies>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-actuatorartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>


        <dependency>
            <groupId>com.itxiongmao.springcloudgroupId>
            <artifactId>cloud-api-commonsartifactId>
            <version>1.0-SNAPSHOTversion>
            <scope>compilescope>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <scope>runtimescope>
            <optional>trueoptional>
        dependency>

    dependencies>


project>

3.写YML

server:
  port: 80

spring:
  application:
    name: cloud-comsumer-order

4.主启动

package com.itxiongmao;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @BelongsProject: springcloud2020
 * @BelongsPackage: com.itxiongmao
 * @CreateTime: 2020-07-09 11:52
 * @Description: TODO
 */
@SpringBootApplication
public class OrderMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderMain80.class,args);
    }
}

5.Resttemplate

Resttemplate是Spring用于同步client端的核心类,简化了与http服务的通信,并满足RestFul原则,程序代码可以给它提供URL,并提取结果。默认情况下,RestTemplate默认依赖jdk的HTTP连接工具。当然你也可以 通过setRequestFactory属性切换到不同的HTTP源,比如Apache HttpComponents、Netty和OkHttp。

官网地址:https://docs.spring.io/spring-framework/docs/5.2.2.RELEASE/javadoc-api/org/springframework/web/client/RestTemplate.html

config配置类

package com.itxiongmao.config;

/**
 * @BelongsProject: springcloud2020
 * @BelongsPackage: com.itxiongmao.config
 * @CreateTime: 2020-07-09 12:27
 * @Description: TODO
 */
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class ApplicationContextConfig {

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

6.controller

package com.itxiongmao.controller;

/**
 * @BelongsProject: springcloud2020
 * @BelongsPackage: com.itxiongmao.controller
 * @CreateTime: 2020-07-09 12:31
 * @Description: TODO
 */
import com.itxiongmao.springcloud.entities.CommonResult;
import com.itxiongmao.springcloud.entities.Payment;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URI;
import java.util.List;

@RestController
@RequestMapping("consumer")
public class OrderController {

    public static final String PAYENT_URL = "http://localhost:8001";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("payment/create")
    public CommonResult<Payment> create(Payment payment) {
        return restTemplate.postForObject(PAYENT_URL+"/payment/create",payment, CommonResult.class);
    }

    @GetMapping("payment/selectOne/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id") long id) {
        return restTemplate.getForObject(PAYENT_URL+"/payment/get/"+id,CommonResult.class);
    }
}

7.测试

http://localhost/consumer/payment/selectOne/31
《SpringCloud专题04》-微服务架构编码构建-服务消费者_第2张图片
http://localhost/consumer/payment/create?serial=1234567
《SpringCloud专题04》-微服务架构编码构建-服务消费者_第3张图片

不要忘记@RequestBody注解

你可能感兴趣的:(分布式微服务专题)