第二章 Spring cloud 服务消费者
程序员文章站
2022-06-25 20:15:49
...
上一篇文章中讲到Spring cloud 服务提供者,现在讲解服务消费者,进而使用调用RestTemplate调用user用户服务,用订单order服务调用用户服务,从而获取用户信息。
OrderController类
package com.example.demo.test.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.example.demo.test.entity.User;
@RestController
public class OrderController {
//使用RestTemplate调用服务
@Autowired
private RestTemplate restTemplate;
//取到application.yml中的值
@Value("${user.userServicePath}")
private String userServicePath;
@GetMapping("/order/{id}")
public User findById(@PathVariable Long id) {
return this.restTemplate.getForObject(this.userServicePath + id, User.class);
}
}
User类
package com.example.demo.test.entity;
import java.math.BigDecimal;
public class User {
private Long id;
private String username;
private String name;
private Short age;
private BigDecimal balance;
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Short getAge() {
return this.age;
}
public void setAge(Short age) {
this.age = age;
}
public BigDecimal getBalance() {
return this.balance;
}
public void setBalance(BigDecimal balance) {
this.balance = balance;
}
}
SpringcloudOrderApplication类:启动类
package com.example.demo.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
public class SpringcloudOrderApplication {
//放入到bean中,new一个RestTemplate对象
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(SpringcloudOrderApplication.class, args);
}
}
application.yml配置
server:
port: 7901 #服务端口号
user:
userServicePath: http://localhost:7900/user/ #用户服务的服务地址
pom.xm配置
<?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.example.demo.test</groupId>
<artifactId>Springcloud-order</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Springcloud-order</name>
<description>Springcloud-order</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
浏览器启动效果如下:
推荐阅读
-
详解Spring Cloud Gateway基于服务发现的默认路由规则
-
SpringCloud之服务注册与发现Spring Cloud Eureka实例代码
-
Spring Cloud Alibaba | Sentinel: 服务限流高级篇
-
Spring Cloud第七篇 | 声明式服务调用Feign
-
[Spring cloud 一步步实现广告系统] 11. 使用Feign实现微服务调用
-
微服务架构下使用Spring Cloud Zuul作为网关将多个微服务整合到一个Swagger服务上
-
Spring Cloud Alibaba | Nacos服务中心初探
-
spring cloud 入门系列八:使用spring cloud sleuth整合zipkin进行服务链路追踪
-
Spring Cloud下使用Feign Form实现微服务之间的文件上传
-
spring cloud服务提供与调用示例