码迷,mamicode.com
首页 > 编程语言 > 详细

SpringCloud之Eureka-Client使用RestTemplate实现服务之间的调用

时间:2019-03-17 00:56:10      阅读:638      评论:0      收藏:0      [点我收藏+]

标签:com   实现   address   div   host   web   ide   instance   desc   

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

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/

如下图所示

技术图片

 

SpringCloud之Eureka-Client使用RestTemplate实现服务之间的调用

标签:com   实现   address   div   host   web   ide   instance   desc   

原文地址:https://www.cnblogs.com/ywjfx/p/10545051.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!