标签:pad 实体 zone ica 代码 访问 alt 一个 url
我们在上一篇的基础上,再创建一个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>
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); } }
@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); } }
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