码迷,mamicode.com
首页 > 其他好文 > 详细

三、服务消费

时间:2018-11-11 23:34:06      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:pad   实体   zone   ica   代码   访问   alt   一个   url   

1、代码演示

创建工程

我们在上一篇的基础上,再创建一个microservice-spring-cloud工程的子模块,将其命名为microservice-consumer-movie,并且在pom.xml文件中加入相关的依赖。

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>

配置application.yml

spring:
  application:
    name: microservice-consumer-movie
server:
  port: 3001
eureka:
  client:
    healthcheck:
      enabled: true
    serviceUrl:
      defaultZone: http://admin:admin123@localhost:1001/eureka
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}

应用主类

创建应用主类。初始化RestTemplate,用来真正发起REST请求。@EnableEurekaClient注解用来将当前应用加入到服务治理体系中。

@SpringBootApplication
@EnableEurekaClient
public class MainAppConsumer {
    @Bean
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }

    public static void main(String[] args) {
        SpringApplication.run(MainAppConsumer.class, args);
    }
}

创建接口,消费microservice-provider-user提供的服务

@RestController
public class MovieController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/movie/{id}")
    public User getById(@PathVariable("id") Long id){
        return this.restTemplate.getForObject("http://localhost:2001/getUser/"+id,User.class);
    }

}

User实体类

public class User implements Serializable {
    private Long id;

    private String username;

    private String name;

    private short 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 short getAge() {
        return age;
    }

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

    public BigDecimal getBalance() {
        return balance;
    }

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

我们先启动microservice-discovery-eureka,然后启动microservice-provider-user,最后启动microservice-consumer-movie,然后测试访问这个接口http://localhost:3001/movie/1,浏览器响应

{"id":1,"username":"user1","name":"张三","age":18,"balance":100.00}

 

三、服务消费

标签:pad   实体   zone   ica   代码   访问   alt   一个   url   

原文地址:https://www.cnblogs.com/beanbag/p/9943166.html

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