本机IP为 192.168.1.102
1. 新建Maven项目 microservice
2. 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>
<groupId>com.java</groupId>
<artifactId>microservice</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>${project.artifactId}</name>
<!-- 配置版本常量 -->
<properties>
<jdk.version>1.8</jdk.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<!-- 在Eureka服务注册中心,完善显示关于微服务信息 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<delimiters>
<delimit>$</delimit>
</delimiters>
</configuration>
</plugin>
</plugins>
</build>
</project>
3. application.yml
server:
port: 8888
spring:
application:
name: microservice
eureka:
client:
service-url:
defaultZone: http://192.168.1.102:8080/eureka
instance:
instance-id: microservice.java.com
prefer-ip-address: true #访问路径可以显示IP地址
info:
app.name: microservice
app.update: 2018-10-07
build.groupId: $project.groupId$
build.artifactId: $project.artifactId$
build.version: $project.version$
4. MicroServiceStarter.java
package com.java.microservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class MicroServiceStarter extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(MicroServiceStarter.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(MicroServiceStarter.class);
}
}
5. HostController.java
package com.java.microservice.controller;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HostController {
@GetMapping("/getHostMessage/{id}")
public Map<String, Object> getHostMessage(@PathVariable String id) {
Map<String, Object> map = new HashMap<>();
try {
InetAddress serverHost = InetAddress.getLocalHost();
map.put("hostname", serverHost.getHostName());
map.put("hostAddress", serverHost.getHostAddress());
map.put("id", id);
} catch (Exception e) {
e.printStackTrace();
map.put("msg", e.getMessage());
}
return map;
}
}
6. 测试
启动 Eureka 服务注册中心,参考 https://www.cnblogs.com/jonban/p/eureka.html
运行MicroServiceStarter.java
浏览器输入Eureka URL
http://192.168.1.102:8080/

微服务已注册进Eureka
浏览器输入微服务测试 URL
http://192.168.1.102:8888/getHostMessage/hello
返回信息如下
{"hostname":"F6RK2EXYAFARPPS","hostAddress":"192.168.1.102","id":"hello"}
截图如下

运行正常。
