三.SpringCloud消费者模型

SpringCloud消费者模型

#Pom文件

<?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.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.hp.cloud</groupId>
    <artifactId>two</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>two</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!--添加支持Spring MVC的web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

#Yml文件

server:
  port: 8010

#Bean

package com.hp.cloud.two.beans;

import java.math.BigDecimal;

public class User {
    private Long id;
    private String username;
    private String name;
    private Integer age;
    private BigDecimal balance;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public BigDecimal getBalance() {
        return balance;
    }

    public void setBalance(BigDecimal balance) {
        this.balance = balance;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", balance=" + balance +
                '}';
    }
}

#Controller

package com.hp.cloud.two.controller;

import com.hp.cloud.two.beans.User;
import org.springframework.beans.factory.annotation.Autowired;
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 resetTemplate;
    @GetMapping("/user/{id}")
    public User findById(@PathVariable Long id){
        return this.resetTemplate.getForObject("http://localhost:8000/"+id,User.class);
    }
}

#启动器类

package com.hp.cloud.two;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
 *
 * 原理:使用RestTemplate调用用户微服务的API,从而查询制定ID的用户信息。
 * 1.创建maven项目
 * 2.和用户微服务一样,电影微服务也需引入Spring Boot及Spring Cloud的依赖管理,在此基础上,添加spring-boot-starter-web依赖
 * 3.创建用户实体类,该类是一个POJO
 * 4.创建启动类
 * 5.创建Controller,在其中使用RestTemplate请求用户微服务的API
 * 6.编写配置文件
 *
 */
@SpringBootApplication
public class TwoApplication {

    @Bean
    public RestTemplate restTemplate(){
      return  new RestTemplate();
}

    /**
     *
     * @Bean是一个方法注解,作用是实例化一个Bean并使用该方法的名称命名。在本例中,添加@Bean注解的restTemplate()
     * 方法,等价于RestTemplate restTemplate=new RestTemplate();
     */
    public static void main(String[] args) {
        SpringApplication.run(TwoApplication.class, args);
    }

}

#测试

http://localhost:8010/user/1

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值