1、创建一个Maven项目,ArtificatId是microservice-sample-consumer-alarm
2、添加依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.3.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> <!-- 引入spring cloud的依赖 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR4</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <!-- 添加spring-boot的maven插件 --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
3、创建实体类
4、创建启动类
@SpringBootApplication
public class ConsumerAlarmApplication {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ConsumerAlarmApplication.class, args);
}
}
@的作用是实例化一个Bean并使用该方法的名称命名,在些等价于:
RestTemplate restTemplate = new RestTemplate()
5、创建Controller,使用RestTemplate请求生产微服务
@RestController
public class AlarmConsumerController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/alarm/findAll")
public User findById() {
return this.restTemplate.getForObject("http://localhost:8000" , AlarmBean.class);
}
}
6、编写application.yml
server: port: 8010