JDK 1.8 or later
Gradle 4+ or Maven 3.2+
3.1 service目录
3.2 client的目录
4.0.0 org.springframework.boot spring-boot-starter-parent 2.2.2.RELEASE com.example service-registration-and-discovery-service 0.0.1-SNAPSHOT service-registration-and-discovery-service Demo project for Spring Boot 1.8 Hoxton.SR1 org.springframework.cloud spring-cloud-starter-netflix-eureka-server org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin spring-milestones Spring Milestones https://repo.spring.io/milestone
plugins { id 'org.springframework.boot' version '2.2.2.RELEASE' id 'io.spring.dependency-management' version '1.0.8.RELEASE' id 'java' } group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility = '1.8' repositories { mavenCentral() maven { url 'https://repo.spring.io/milestone' } } ext { set('springCloudVersion', "Hoxton.SR1") } dependencies { implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server' testImplementation('org.springframework.boot:spring-boot-starter-test') { exclude group: 'org.junit.vintage', module: 'junit-vintage-engine' } } dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" } } test { useJUnitPlatform() }
启动Eureka服务注册表
您首先需要一个Eureka服务注册中心。
可以使用Spring Cloud的@EnableEurekaServer来建立一个注册表,其他应用程序可以与之通信。
这是一个常规的Spring引导应用程序,添加了一个注释(@enableeruekaserver)以启用服务注册表
@EnableEurekaServer
@SpringBootApplication
package com.example.serviceregistrationanddiscoveryservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class ServiceRegistrationAndDiscoveryServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceRegistrationAndDiscoveryServiceApplication.class, args);
}
}
@EnableDiscoveryClient @SpringBootApplication
定义一个Client的客服端
package com.example.serviceregistrationanddiscoveryclient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @EnableDiscoveryClient @SpringBootApplication public class ServiceRegistrationAndDiscoveryClientApplication { public static void main(String[] args) { SpringApplication.run(ServiceRegistrationAndDiscoveryClientApplication.class, args); } } @RestController class ServiceInstanceRestController { @Autowired private DiscoveryClient discoveryClient; @RequestMapping("/service-instances/{applicationName}") public ListserviceInstancesByApplicationName( @PathVariable String applicationName) { return this.discoveryClient.getInstances(applicationName); } }
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
logging.level.com.netflix.eureka=OFF
logging.level.com.netflix.discovery=OFF
./mvnw spring-boot:run -pl eureka-service
下载老半天,不好用,修改为, 本机也配置了mvn环境,
mvn spring-boot:run -pl eureka-service
编译成功后,启动界面为:启动服务端口为8761端口,
2020-04-06 19:42:12.075 INFO 4075 --- [ Thread-11] o.s.c.n.e.server.EurekaServerBootstrap : Initialized server context
2020-04-06 19:42:12.100 INFO 4075 --- [ Thread-11] e.s.EurekaServerInitializerConfiguration : Started Eureka Server
2020-04-06 19:42:12.139 INFO 4075 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8761 (http) with context path ''
2020-04-06 19:42:12.140 INFO 4075 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8761
2020-04-06 19:42:12.144 INFO 4075 --- [ main] gistrationAndDiscoveryServiceApplication : Started ServiceRegistrationAndDiscoveryServiceApplication in 19.956 seconds (JVM running for 20.472)
./mvnw spring-boot:run -pl eureka-client
打开终端,点击下面加号,新的session,运行eureka-client
输入下面命令mvn spring-boot:run -pl eureka-client
要使用Gradle运行Eureka服务,请在终端窗口(在/complete目录中)中运行以下命令:
./gradlew :eureka-service:bootRun
要使用Gradle运行Eureka客户机,请在终端窗口(在/complete目录中)中运行以下命令:
./gradlew :eureka-client:bootRun