简介
上一节,我们讲了创建spring cloud生产者,并利用docker-compose部署到swarm集群中,这节我们讨论一下最restTemlate调用生产者服务
一、创建模块(microservice-consumer-movie)
项目结构如下:
二、pom.xml文件
microservice-spring-cloud com.jacky 1.0-SNAPSHOT 4.0.0 microservice-consumer-movie pom ../microservice-provider-user UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-eureka org.springframework.boot spring-boot-starter-actuator com.spotify docker-maven-plugin build-image install build http://192.168.6.130:5678 true ${docker.repostory}/${docker.image.prefix}/${project.artifactId}:${project.version} java:openjdk-8-jdk-alpine ["java", "-jar", "/${project.build.finalName}.jar"] / ${project.build.directory} ${project.build.finalName}.jar
三、实体类(User.java)
package com.jacky.cloud.entity;import java.math.BigDecimal;public class User { private Long id; private String username; private String name; private Short age; private BigDecimal balance; public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } public String getUsername() { return this.username; } public void setUsername(String username) { this.username = username; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public Short getAge() { return this.age; } public void setAge(Short age) { this.age = age; } public BigDecimal getBalance() { return this.balance; } public void setBalance(BigDecimal balance) { this.balance = balance; }}
四、MovieController.java
package com.jacky.cloud.controller;import com.jacky.cloud.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; @GetMapping("/movie/{id}") public User findById(@PathVariable Long id) { return this.restTemplate.getForObject(this.userServicePath + id, User.class); }}
五、启动类(MicroserviceSimpleConsumerMovieApplication)
package com.jacky.cloud;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 MicroserviceSimpleConsumerMovieApplication { @Bean public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(MicroserviceSimpleConsumerMovieApplication.class, args); }}
六、配置文件application.yml
server: port: 7901spring: application: name: microservice-consumer-movie #zipkin: #base-url: http://127.0.0.1:7788user: userServicePath: http://localhost:7900/simple/ #指定生产者的路径eureka: client: healthcheck: enabled: true serviceUrl: defaultZone: http://jacky:admin@peer1:8761/eureka/,http://jacky:admin@peer2:8762/eureka/,http://jacky:admin@peer3:8763/eureka/ instance: prefer-ip-address: true instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}