本文将介绍如何注册一个服务到eureka注册中心。
1、环境约束
win10 64为操作系统
idea2018.1.5
maven-3.0.5
jdk-8u162-windows-x64
2、前提约束
3、操作步骤
3.1 按下图步骤创建一个eureka客户端
创建一个eureka客户端
3.2 重命名application.properties为application.yml
修改为以下内容:
server:
port: 8083
eureka:
client:
service-url:
defaultZone : http://localhost:7561/eureka
spring:
application:
name: user-service
3.3 修改主启动类
package net.wanho.springboot.service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableEurekaClient
public class ServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceApplication.class, args);
}
}
3.4 创建一个net.wanho.springboot.service.InfoService.java类
package net.wanho.springboot.service;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class InfoService {
@RequestMapping("/info/query")
public String queryInfo()
{
return "this is from service, the port is 8083";
}
}
3.5 启动测试
在eureka注册中心【上一篇】启动的基础上,启动本章服务。
在浏览器中继续查看Eureka注册中心信息:
新服务已经注册到eureka
在浏览器中输入http://localhost:8083/info/query,我们也能看到响应信息。
至此,我们完成了注册一个服务到eureka中。