1.创建spring boot maven项目
2.修改pom.xml文件
与Eureka注册中心唯一的区别是将下面
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
修改为
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
3.添加配置文件application.properties
#\u5E94\u7528(\u670D\u52A1)\u540D\u79F0
spring.application.name=compute-service
server.port=8762
#\u6CE8\u518C\u4E2D\u5FC3\u5730\u5740
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
4.在Application.class添加注解@EnableEurekaClient
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
public class TomcatApplication {
public static void main(String[] args) {
SpringApplication.run(TomcatApplication.class, args);
}
}
5.创建controller
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
@Value("${server.port}")
String port;
@RequestMapping("/hello")
public String index() {
return "Hello World,I'm from "+port;
}
}
6.启动服务。前提条件是启动Eureka. 可以看到服务已经注册上了。
访问 "http://localhost:8762/hello",返回正常信息