1.新建maven工程
package com.yf.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList; import java.util.List; /** * Created by 79782 on 2018/6/11. */ @RestController public class FirstServer { @RequestMapping("/getMenberAll") public List<String> getMenberAll(){ List<String> listUser = new ArrayList<String>(); listUser.add("zhangsan"); listUser.add("lisi"); listUser.add("wangwu"); return listUser; } }
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; import org.springframework.context.annotation.ComponentScan; /** * Created by 79782 on 2018/6/11. */ @ComponentScan(basePackages = { "com.yf.controller" }) @SpringBootApplication @EnableEurekaClient public class EurekaApplication { public static void main(String[] args) { //new SpringApplicationBuilder(EurekaApplication.class).web(true).run(args); //或者pom引spring-boot-starter-web,然后用下面这个启动 SpringApplication.run(EurekaApplication.class, args); } }
eureka: client: serviceUrl: defaultZone: http://localhost:8899/eureka/ server: port: 8762 spring: application: name: service-member
<?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> <groupId>com.yf</groupId> <artifactId>eruekaService</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR6</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> </project>启动注册中心后,在启动服务,打开http://localhost:8899/ 发现服务被注册上去了