赞
踩
快速搭建一个注册中心(Eureka服务端):
1.创建SpringBoot项目,引入Eureka-server和springCloud依赖
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.2.0.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <properties>
- <java.version>1.8</java.version>
- <spring-cloud.version>Hoxton.M3</spring-cloud.version>
- </properties>
- <dependencies>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
- </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>
2.在启动类上添加@EnableEurekaServer注解
- @SpringBootApplication
- @EnableEurekaServer
- public class EurekaApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(EurekaApplication.class, args);
- }
3.application.yml配置文件
- server:
- port: 8761
- eureka:
- client:
- fetch-registry: false
- register-with-eureka: false
- service-url:
- defaultZone: http://127.0.0.1:8761/eureka
以上搭建了服务端,启动就可以了,下面创建客户端
同样,新建boot项目
1,引入客户端依赖
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.2.0.RELEASE</version>
- <relativePath/>
- </parent>
-
- <properties>
- <java.version>1.8</java.version>
- <spring-cloud.version>Hoxton.M3</spring-cloud.version>
- </properties>
-
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </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>
2,在启动类上添加@EnableEurekaClient 或者@EnableDiscoveryClient注解,区别是后者是SpringClou的,不仅可以用于Eureka还可以是服务作为客户端注册到其他的注册中心中
- @SpringBootApplication
- @EnableEurekaClient
- public class EurekaApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(EurekaApplication.class, args);
- }
-
- }
3,配置文件
- server:
- port:8070
- eureka:
- client:
- service-url:
- defaultZone: http://127.0.0.1:8761/eureka/
以上创建了一个eureka客户端,启动上面的服务端,再启动客户端,就可以成功的注册到服务端
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。