zoukankan      html  css  js  c++  java
  • 第五章 SpringCloud之Eureka-Client使用RestTemplate实现服务之间的调用

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

    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>
    View Code

    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;
        }
    }
    View Code

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

    http://localhost:8664/movie/1

    7、查看eureka-server的情况

    http://localhost:8661/

    如下图所示

  • 相关阅读:
    HTML图片映射
    js数组去重问题
    Mooc--五子棋(js)小结
    js跨域问题
    HTML5新特性
    js输出
    CSS display属性学习
    理财课堂日记第1天
    磁盘性能测试方法
    一个清空数据库数据的脚本总结
  • 原文地址:https://www.cnblogs.com/ywjfx/p/10545051.html
Copyright © 2011-2022 走看看