引入依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
- 创建提供者:
控制层:
@RestController
public class MyController {
@GetMapping("user/{id}")
public User getUser(@PathVariable("id") int id)
{
return new User(id,new Date());
}
}
public class User {
private int id;
private Date date;
}
- application.poperties
server.port=8080
#服务别名
spring.application.name=produce-user
- consumer 消费者:
控制层
@RestController
public class ConsumerController {
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
@Autowired//spring提供的一个访问rest 接口的模板
private RestTemplate restTemplate;
private String url ="http://localhost:8080/user/";
@GetMapping("order/{id}")
public User getOrder(@PathVariable("id") int id){
url = "http://119.23.224.88:8080/user/";
//访问提供者获取数据
User user = restTemplate.getForObject(url+id, User.class);
return user;
}
}
- application.properties
server.port=8081
spring.application.name=consumer-user
引入Eureka
在这里插入代码片