1.spring服务注册和发现
2.编译条件
-
JDK 1.8 or later
-
3.代码格式
3.1 service目录
3.2 client的目录
4.POM.xml的文件
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>service-registration-and-discovery-service</artifactId> <version>0.0.1-SNAPSHOT</version> <name>service-registration-and-discovery-service</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> </repository> </repositories> </project>
build.gradle
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() }
5.原理
启动Eureka服务注册表
您首先需要一个Eureka服务注册中心。
可以使用Spring Cloud的@EnableEurekaServer来建立一个注册表,其他应用程序可以与之通信。
这是一个常规的Spring引导应用程序,添加了一个注释(@enableeruekaserver)以启用服务注册表
5.1 启动EurekaServer的服务
@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);
}
}
5.2 启动Client的客户端
@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 List<ServiceInstance> serviceInstancesByApplicationName( @PathVariable String applicationName) { return this.discoveryClient.getInstances(applicationName); } }
5.3配置为
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
5.4 测试应用程序
5.4.1 要使用Maven运行Eureka服务,请在终端窗口(在/complete目录中)中运行以下命令:
./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)
进入界面为:
5.4.2要使用Maven运行Eureka客户机,请在终端窗口(在/complete目录中)中运行以下命令:
./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