作用

用于消费服务

新建Module

该Module为Maven项目,命名为microservice-student-consumer-80

修改pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?xml version="1.0" encoding="UTF-8"?>

<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>

<!--继承microservice依赖版本-->
<parent>
<groupId>org.example</groupId>
<artifactId>microservice</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>microservice-student-consumer-80</artifactId>
<packaging>war</packaging>

<name>microservice-student-consumer-80 Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>

<dependencies>
<!--引入microservice-common依赖,引入后可以使用microservice-common的实体类,工具类等-->
<dependency>
<groupId>org.example</groupId>
<artifactId>microservice-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</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>

application.yml配置

在resources中新建application.yml

1
2
3
server:
port: 80
context-path: /

SpringCloud相关配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.ledao.config;

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

/**
* SpringCloud相关配置
*
* @author LeDao
* @company
* @create 2021-08-11 14:46
*/
@Configuration
public class SpringCloudConfig {

/**
* 调用服务模版
*
* @return
*/
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}

服务消费者-学生信息控制器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.ledao.controller;

import com.ledao.entity.Student;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
* 服务消费者-学生信息控制器
*
* @author LeDao
* @company
* @create 2021-08-11 14:48
*/
@RestController
@RequestMapping("/student")
public class StudentConsumerController {

@Resource
private RestTemplate restTemplate;

private static final String PRE_HOST = "http://localhost:1001";

/**
* 添加或者修改学生信息
*
* @param student
* @return
*/
@RequestMapping("save")
public boolean save(Student student) {
return restTemplate.postForObject(PRE_HOST + "/student/save", student, Boolean.class);
}

/**
* 查询所有学生信息
*
* @return
*/
@SuppressWarnings("unchecked")
@RequestMapping("/list")
public List<Student> list() {
return restTemplate.getForObject(PRE_HOST + "/student/list", List.class);
}

/**
* 根据id查询学生信息
*
* @param id
* @return
*/
@RequestMapping("/get/{id}")
public Student get(@PathVariable("id") Integer id) {
return restTemplate.getForObject(PRE_HOST + "/student/get/" + id, Student.class);
}

/**
* 根据id删除学生信息
*
* @param id
* @return
*/
@RequestMapping("/delete/{id}")
public boolean delete(@PathVariable("id") Integer id) {
try {
restTemplate.getForObject(PRE_HOST + "/student/delete/" + id, Boolean.class);
return true;
} catch (RestClientException e) {
return false;
}
}
}

服务消费者启动类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.ledao;

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;

/**
* 服务消费者启动类
*
* @author LeDao
* @company
* @create 2021-08-11 15:01
*/
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class StudentConsumerApplication_80 {

public static void main(String[] args) {
SpringApplication.run(StudentConsumerApplication_80.class, args);
}
}

结果

运行服务消费者启动类后,在浏览器地址栏输入对应URL可以进行增删查改操作,端口为80,通过端口80调用端口1001的方法