注意:这个章节,请结合前几章节一起使用,因为其要调用上一章节的服务

1、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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.test</groupId>
<artifactId>eureka-client-movie</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-client-movie</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.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-netflix-eureka-server</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

2、添加application.yml配置

server:
port: 8664
user:
userServicePath: http://localhost:8663/user/ spring:
application:
name: eureka-client-movie
eureka:
instance:
hostname: localhost
prefer-ip-address: true
instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
client:
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:8661/eureka

3、在启动类添加注解

package com.test.eurekaclientmovie;

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 EurekaClientMovieApplication { @Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(EurekaClientMovieApplication.class, args);
} }

4、MovieController.java

package com.test.eurekaclientmovie.controller;

import com.test.eurekaclientmovie.entity.User;
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; @RestController
public class MovieController {
@Autowired
private RestTemplate restTemplate; @Value("${user.userServicePath}")
private String userServicePath; /* @Autowired
private LoadBalancerClient loadBalancerClient;*/ @GetMapping("/movie/{id}")
public User findById(@PathVariable Long id) {
//请用虚拟ip
return restTemplate.getForObject(this.userServicePath+id, User.class);
} /**
* 配置单一的方法进行轮询
* @return
*//*
@GetMapping("/test")
public String test() {
//请用虚拟ip
ServiceInstance serviceInstance =loadBalancerClient.choose("eureka-client-user");
System.out.println(serviceInstance.getHost()+":"+serviceInstance.getPort());
return "1";
}*/
}

5、User.java

package com.test.eurekaclientmovie.entity;

import java.io.Serializable;
import java.math.BigDecimal; public class User implements Serializable {
private Long id;
private String name;
private String username;
private BigDecimal balance;
private short age; public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public BigDecimal getBalance() {
return balance;
} public void setBalance(BigDecimal balance) {
this.balance = balance;
} public short getAge() {
return age;
} public void setAge(short age) {
this.age = age;
}
}

6、实现服务之间的调用,如果访问下面URL有返回值,说明实现了服务之间的调用了,没有实现,请仔细检查配置,也可以留言

http://localhost:8664/movie/1

7、查看eureka-server的情况

http://localhost:8661/

如下图所示

第五章 SpringCloud之Eureka-Client使用RestTemplate实现服务之间的调用的更多相关文章

  1. SpringCloud实战 | 第五篇:SpringCloud整合OpenFeign实现微服务之间的调用

    一. 前言 微服务实战系列是基于开源微服务项目 有来商城youlai-mall 版本升级为背景来开展的,本篇则是讲述SpringCloud整合OpenFeign实现微服务之间的相互调用,有兴趣的朋友可 ...

  2. SpringCloud创建Eureka Client服务注册

    1.说明 本文详细介绍微服务注册到Eureka的方法, 即Eureka Client注册到Eureka Server, 这里用任意一个Spring Cloud服务为例, 比如下面已经创建好的Confi ...

  3. 微服务~Eureka实现的服务注册与发现及服务之间的调用

    微服务里一个重要的概念就是服务注册与发现技术,当你有一个新的服务运行后,我们的服务中心可以感知你,然后把加添加到服务列表里,然后当你死掉后,会从服务中心把你移除,而你作为一个服务,对其它服务公开的只是 ...

  4. springcloud 入门 3 (服务之间的调用)

    服务调用: 指的是注册到服务端上的客户端之间数据的相互调用问题:服务与服务的通讯是基于http restful的 服务直接调用主要有两种实现:ribbon  和 feign ribbon是实现负载均衡 ...

  5. SpringBoot+SpringCloud实现登录用户信息在微服务之间的传递

    实现思路: 1:准备一个ThreadLocal变量,供线程之间共享. 2:每个微服务对所有过来的Feign调用进行过滤,然后从请求头中获取User用户信息,并存在ThreadLocal变量中. 3:每 ...

  6. 【转】SpringBoot+SpringCloud实现登录用户信息在微服务之间的传递

    实现思路: 1:准备一个ThreadLocal变量,供线程之间共享. 2:每个微服务对所有过来的Feign调用进行过滤,然后从请求头中获取User用户信息,并存在ThreadLocal变量中. 3:每 ...

  7. SpringCloud创建Config Client通过Eureka访问Config

    1.说明 本文详细介绍配置中心客户端使用方法, 即Config Client到Config Server读取配置. 读取配置的方式有两种, 第一种是直接配置Configer Server的URL, 第 ...

  8. 一起来学Spring Cloud | 第五章:熔断器 ( Hystrix)

    在微服务项目中,一个系统可以分割成很多个不同的服务模块,不同模块之间我们通常需要进行相互调用.springcloud中可以使用RestTemplate+Ribbon和Feign来调用(工作中基本都是使 ...

  9. 一文读懂SpringCloud与Eureka,Feign,Ribbon,Hystrix,Zuul核心组件间的关系

    概述 毫无疑问,Spring Cloud是目前微服务架构领域的翘楚,无数的书籍博客都在讲解这个技术.不过大多数讲解还停留在对Spring Cloud功能使用的层面,其底层的很多原理,很多人可能并不知晓 ...

随机推荐

  1. thinkphp5.1 中间件是什么,有什么用

    中间件是什么?有什么作用? 中间件主要用于拦截或过滤应用的HTTP请求,并进行必要的业务处理.---tp5.1手册 也就是说,降低了系统的耦合:[在http请求阶段,执行中间件的入口执行方法(hand ...

  2. 【异常】org.apache.hadoop.hdfs.server.common.InconsistentFSStateException

    1 异常信息 - ::, INFO org.apache.hadoop.hdfs.server.namenode.FSNamesystem: Maximum size of an xattr: -- ...

  3. Foo, Bar的含义

    有些朋友问:foo, bar是什么意思, 为什么C++书籍中老见到这个词.我google了一下, 发现没有很好的中文答案.这个问题,在维基百科上有很好的回答.在这里翻译给大家. 译文: 术语fooba ...

  4. CentOS 7 中英文桌面安装步骤详细图解

    https://www.cnblogs.com/haoliyou/p/7694868.html

  5. inode、软硬链接

    关于inode是什么,可以看这篇文章:http://www.cnblogs.com/adforce/p/3522433.html 如何查看inode ll -di /boot / /app查看文件和文 ...

  6. VCL界面控件DevExpress VCL Controls v19.1.3全新来袭

    DevExpress VCL Controls是 Devexpress公司旗下最老牌的用户界面套包.所包含的控件有:数据录入,图表,数据分析,导航,布局,网格,日程管理,样式,打印和工作流等,让您快速 ...

  7. MFC界面库BCGControlBar v30.1——Grid/Report控件

    亲爱的BCGSoft用户,我们非常高兴地宣布BCGControlBar Professional for MFC和BCGSuite for MFC v30.1正式发布!此版本包含themed find ...

  8. wx小程序知识点(三)

    三.封装小程序的数据请求 (1)在根目录创建utils目录,创建config.js.base.js (2)在config.js中创建config类,并将请求路径配置给config的属性restUrl, ...

  9. FFT用于高效大数乘法(当模板用)

    转载来源:https://blog.csdn.net/zj_whu/article/details/72954766 #include <cstdio> #include <cmat ...

  10. Springboot 使用JdbcTemplate

    Springboot 使用JdbcTemplate book package com.draymonder.book.jdbc; public class Book { private Integer ...