### 构建生产者
创建一个考试,获得参考考试的学生。 通过使用resttemplate来调用另一个student的微服务,实现服务之间的调用。
1.创建model
参考这个链接中的model,都有一样,直接复制过来。
http://hechunbo.com/index.php/archives/366.html
2.更改pom .xml
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
3.配置文件
server:
port: 80
4.创建启动类
@SpringBootApplication
public class Exam {
public static void main(String[] args) {
SpringApplication.run(Exam.class,args);
}
}
5.创建业务类
5.1 拷贝provider中的两个实体类过来。参考:
http://hechunbo.com/index.php/archives/366.html
5.2 使用resttemplate调用provider微服务
创建config.ApplicationContextConfig 添加@Configuration 和@Bean 两个注解。
等价于application.properties中的<bean id="",class=""></bean.
@Configuration
public class ApplicationContextConfig {
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
5.3 创建examController
@RestController
@Slf4j
public class ExamController {
public static final String url="http://localhost:8001";
@Resource
private RestTemplate restTemplate;
@GetMapping("/exam/student/create")
public CommonResult<Student> create(Student student){
return restTemplate.postForObject(url+"/student/create",student,CommonResult.class);
}
@GetMapping("/exam/student/get/{id}")
public CommonResult<Student> getStudent(@PathVariable("id") int id){
return restTemplate.getForObject(url+"/student/get/"+id,CommonResult.class);
}
}
不错不错,我喜欢看 https://www.237fa.com/
哈哈哈,写的太好了https://www.lawjida.com/