博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第五章 SpringCloud之Eureka-Client使用RestTemplate实现服务之间的调用
阅读量:5168 次
发布时间:2019-06-13

本文共 5209 字,大约阅读时间需要 17 分钟。

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

1、pom.xml

4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.3.RELEASE
com.test
eureka-client-movie
0.0.1-SNAPSHOT
eureka-client-movie
Demo project for Spring Boot
1.8
Greenwich.SR1
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
View Code

2、添加application.yml配置

server:  port: 8664user:  userServicePath: http://localhost:8663/user/spring:  application:    name: eureka-client-movieeureka:  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@EnableEurekaClientpublic 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;@RestControllerpublic 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;    }}
View Code

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

http://localhost:8664/movie/1

7、查看eureka-server的情况

http://localhost:8661/

如下图所示

 

转载于:https://www.cnblogs.com/ywjfx/p/10545051.html

你可能感兴趣的文章
css3 2d转换3d转换以及动画的知识点汇总
查看>>
【Java】使用Eclipse进行远程调试,Linux下开启远程调试
查看>>
对Vue为什么不支持IE8的解释之一
查看>>
计算机改名导致数据库链接的诡异问题
查看>>
Java8内存模型—永久代(PermGen)和元空间(Metaspace)(转)
查看>>
ObjectiveC基础教程(第2版)
查看>>
centos 引导盘
查看>>
Notes of Daily Scrum Meeting(12.8)
查看>>
Apriori算法
查看>>
onlevelwasloaded的调用时机
查看>>
lr_start_transaction/lr_end_transaction事物组合
查看>>
CodeIgniter学习笔记(四)——CI超级对象中的load装载器
查看>>
.NET CLR基本术语
查看>>
ubuntu的home目录下,Desktop等目录消失不见
查看>>
建立,查询二叉树 hdu 5444
查看>>
[Spring框架]Spring 事务管理基础入门总结.
查看>>
2017.3.24上午
查看>>
Python-常用模块及简单的案列
查看>>
LeetCode 159. Longest Substring with At Most Two Distinct Characters
查看>>
基本算法概论
查看>>