第四章 Spring CLoud 使用RestTemplate+Eureka调用用户微服务
2017-12-31 11:07
435 查看
上一篇讲到注册服务和用户服务,本章主要讲解一个服务调用另一个服务的方法:使用RestTemplate+eureka
项目结构如下:SpringCloudOrderApplication类
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableEurekaClient public class SpringCloudOrderApplication { @Bean //必须new 一个RestTemplate并放入spring容器当中,否则启动时报错 public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(SpringCloudOrderApplication.class, args); }
OrderController类
package com.example.demo.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.entity.User; @RestController public class OrderController { @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.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; } }
application.yml配置
spring: application: name: spring-cloud-order #微服务的名称 server: port: 7901 #微服务端口号 user: userServicePath: http://localhost:7900/user/ #用户微服务的URL地址 eureka: client: healthcheck: enabled: true # 开启健康检查 serviceUrl: defaultZone: http://user:123456@localhost:8761/eureka #服务eureka的URL地址 instance: prefer-ip-address: true
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.example.demo</groupId> <artifactId>spring-cloud-order</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>spring-cloud-order</name> <description>spring-cloud-order</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> </parent> <properties> <!-- 文件拷贝时的编码 --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <!-- 编译时的编码 --> <maven.compiler.encoding>UTF-8</maven.compiler.encoding> <!-- jdk版本 --> <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.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!-- 这个库让我们可以访问应用的很多信息,包括:/env、/info、/metrics、/health等 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- 用于热部署 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <!-- 版本依赖管理,故之后添加依赖无需指定version --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR2</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <!-- 用以为integration-test提供支持。 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
启动eureka服务,然后启动用户微服务和订单微服务,如下图所示:
注意,要是创建的项目报错,请点击项目右键:Maven----->Update Project
勾选Force Update of Snapshots/Releases即可
相关文章推荐
- 使用RestTemplate实现rest服务的调用
- RestTemplate使用gzip压缩调用接口
- SpringCloud使用eureka(rest)和ribbon实现服务调用和负载均衡
- SpringCloud 查找调用REST服务使用RestTemplate(ribbon负载)或feign模式 教程源码 火推
- 使用RestTemplate来构建远程调用服务
- ribbon通过RestTemplate调用接口获取List<JavaBean>时,应使用数组而不能直接使用List
- 使用 Spring RestTemplate 调用 rest 服务时自定义请求头(custom HTTP headers)
- spring rest mvc使用RestTemplate调用
- 使用 Spring RestTemplate 调用 rest 服务时自定义请求头(custom HTTP headers)
- SpringCloud Cloud查找调用REST服务使用RestTemplate(ribbon负载) 或feign模式火推13
- spring boot / cloud (八) 使用RestTemplate来构建远程调用服务
- spring boot / cloud (八) 使用RestTemplate来构建远程调用服务
- 前台系统使用resttemplate调用后台系统显示注意事项
- 使用restTemplate调用外部接口
- SpringBoot系列十一:SpringBoot整合Restful架构(使用 RestTemplate 模版实现 Rest 服务调用、Swagger 集成、动态修改日志级别)
- 使用java开发facebook网站应用程序 , facebook,facebookrestclient,页面,fbml,应用程序,调用,用户,密钥,应用,登陆
- Spring中使用RestTemplate调用第三方接口API
- spring rest mvc使用RestTemplate调用
- 使用RestSharp 库调用Restful Service
- 使用C#反射实现用户控件调用父页面方法