一、先创建一个index项目,此项目受当前父项目管理:


1、选中 com-temp-cloud 项目,右键新建一个 Maven Module   

2、勾选 Create a simple project (skip archetype selection) ,

3、Module Name 填写 com-temp-cloud-index,规范是 父 artifactId 加上 模块名

4、Packaging: 现在选择 jar   

5、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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.temp.cloud</groupId>
		<artifactId>com-temp-cloud</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>com-temp-cloud-index</artifactId>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-mail</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<!-- 打成jar包程序入口路径 -->
					<mainClass>com.temp.index.IndexApplication</mainClass>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

6、创建启动类和Controller类:

1、application.properties 文件内容:


server.port=9090


2、启动类中,添加 RestTemplate Bean;



import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

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

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


3、Controller类中,通过 IP+端口+路径,调用User服务:


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.client.RestTemplate;

@Controller
public class IndexController {

	@Autowired
	private RestTemplate restTemplate;

	@GetMapping({ "", "/" })
	public String index(Model m) {

		// 通过restTemplate调用用户微服务
		Object obj = restTemplate.getForObject("http://localhost:8019/user/2", String.class);

		m.addAttribute("test", obj);

		return "index";
	}
}

4、创建 index.html,显示服务调用后返回内容:



<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<span th:text="${test}"></span>
</body>
</html>

5、浏览器访问:http://localhost:9090/ 结果如下,即成功: