`

服务消费者microservice-student-consumer-80

阅读更多
1. 服务消费者项目microservice-student-consumer-80搭建

新建服务消费者项目microservice-student-consumer-80
new -> Maven Module
    -> create a simple project
Module Name:microservice-student-consumer-80
Parent Project:microservice
Working set:SpringCloud
    -> Artifact
Group Id:com.andrew.springcloud
Artifact Id:microservice-student-consumer-80
Version:0.0.1-SNAPSHOT
Packaging:jar


增加注解@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})

将DataSourceAutoConfiguration,DataSourceTransactionManagerAutoConfiguration排队在外,
也就是不会自动配置DataSource与DataSourceTransactionManager。


1.1) 服务消费者项目microservice-student-consumer-80增加pom.xml依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
    http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.andrew.springcloud</groupId>
        <artifactId>microservice</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>microservice-student-consumer-80</artifactId>
    <dependencies>
        <dependency>
            <groupId>com.andrew.springcloud</groupId>
            <artifactId>microservice-common</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 修改后立即生效,热部署 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>
</project>


1.2) 服务消费者项目microservice-student-consumer-80增加application.yml配置

server:
  port: 80
  context-path: /


1.3) 服务消费者项目microservice-student-consumer-80增加com.andrew.StudentConsumerApplication_80.java启动类

package com.andrew;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class })
public class StudentConsumerApplication_80 {
    public static void main(String[] args) {
        SpringApplication.run(StudentConsumerApplication_80.class, args);
    }
}


1.4) 服务消费者项目microservice-student-consumer-80增加com.andrew.config.SpringCloudConfig.java类

package com.andrew.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class SpringCloudConfig {
    @Bean
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}


1.5) 服务消费者项目microservice-student-consumer-80增加com.andrew.controller.StudentConsumerController.java
这里调用服务提供者microservice-student-provider-1001的服务

package com.andrew.controller;

import java.util.List;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.andrew.entity.Student;

@RestController
@RequestMapping("/student")
public class StudentConsumerController {
    @Resource
    private RestTemplate restTemplate;
    @PostMapping(value = "/save")
    private boolean save(Student student) {
        return restTemplate.postForObject("http://localhost:1001/student/save", student, Boolean.class);
    }
    @SuppressWarnings("unchecked")
    @GetMapping(value = "/list")
    public List<Student> list() {
        return restTemplate.getForObject("http://localhost:1001/student/list", List.class);
    }
    @GetMapping(value = "/get/{id}")
    public Student get(@PathVariable("id") Integer id) {
        return restTemplate.getForObject("http://localhost:1001/student/get/" + id, Student.class);
    }
    @GetMapping(value = "/delete/{id}")
    public boolean delete(@PathVariable("id") Integer id) {
        try {
            restTemplate.getForObject("http://localhost:1001/student/delete/" + id, Boolean.class);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}


启动microservice-student-provider-1001
启动microservice-student-consumer-80

http://localhost/student/list
[{"id":1,"name":"zhangsan","grade":"11"},{"id":2,"name":"lisi","grade":"12"},{"id":3,"name":"wangwu","grade":"13"},{"id":4,"name":"student04","grade":"14"}]

http://localhost/student/get/1
{"id":1,"name":"zhangsan","grade":"11"}

post http://localhost/student/save
id    5
grade    15
name    student05
true

http://localhost/student/get/5
{"id":5,"name":null,"grade":null}


2. 服务提供者microservice-student-provider-1001修改

2.1) 服务提供者microservice-student-provider-1001修改com.andrew.controller.StudentProviderController.java

由于是http rest方式交互,增加@RequestBody注解

...
@PostMapping(value = "/save")
public boolean save(@RequestBody Student student) {
    try {
        studentService.save(student);
        return true;
    } catch (Exception e) {
        return false;
    }
}
...


启动microservice-student-provider-1001
启动microservice-student-consumer-80

post http://localhost/student/save
id    6
grade    16
name    student06
true

http://localhost/student/get/6
{"id":6,"name":"student06","grade":"16"}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics