0
点赞
收藏
分享

微信扫一扫

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

1.建cloud-consumer-order80

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

2.改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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud2020</artifactId>
<groupId>com.itxiongmao.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>cloud-consumer-order80</artifactId>

<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>


<dependency>
<groupId>com.itxiongmao.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</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》-微服务架构编码构建-服务消费者_spring_02
​​​ http://localhost/consumer/payment/create?serial=1234567​​《SpringCloud专题04》-微服务架构编码构建-服务消费者_maven_03

不要忘记@RequestBody注解


举报

相关推荐

0 条评论