作用
用于消费服务
新建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>
<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> <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> <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;
@Configuration public class SpringCloudConfig {
@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;
@RestController @RequestMapping("/student") public class StudentConsumerController {
@Resource private RestTemplate restTemplate;
private static final String PRE_HOST = "http://localhost:1001";
@RequestMapping("save") public boolean save(Student student) { return restTemplate.postForObject(PRE_HOST + "/student/save", student, Boolean.class); }
@SuppressWarnings("unchecked") @RequestMapping("/list") public List<Student> list() { return restTemplate.getForObject(PRE_HOST + "/student/list", List.class); }
@RequestMapping("/get/{id}") public Student get(@PathVariable("id") Integer id) { return restTemplate.getForObject(PRE_HOST + "/student/get/" + id, Student.class); }
@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;
@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的方法