SpringCloud 和 SpringBoot 版本选择
我选择的版本选择
- SpringBoot 2.3.5.RELEASE
- SpringCloud Hoxton.SR6
1 - 开发父项目(maven聚合 版本管理)
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
</parent>
<properties>
<spring.cloud-version>Hoxton.SR6</spring.cloud-version>
</properties>
<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>
2 - 开发eureka server (eureka注册中心)
- pom.xml
<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>
- 入口类
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication8761 {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication8761.class, args);
}
}
@EnableEurekaServer //开启eureka服务(注册中心)
- 配置编写 application.yml
server:
port: 8761 #默认端口号
spring:
application:
name: EUREKASERVER # eureka服务注册中心的服务名称(微服务的唯一标识)
eureka:
client:
fetch-registry: false # 关闭立即向自己注册自己
register-with-eureka: false # false 表示我是eureka服务注册中心
service-url:
defaultZone: http://localhost:8761/eureka # 向外暴露服务!端口号必须和自己的一致
eureka:
client:
fetch-registry: false # 关闭立即向自己注册自己
这个一定必须是false 不然会出现错误 因为自己还没开启就向自己注册这个时候会有错误,虽然不会影响。推荐关闭
3 - 发开eureka client 客户端
- pom.xml
<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-client</artifactId>
</dependency>
</dependencies>
- 入口类
@SpringBootApplication
@EnableEurekaClient
public class EurekaClient8080 {
public static void main(String[] args) {
SpringApplication.run(EurekaClient8080.class, args);
}
}
- 配置 application.yml
spring:
application:
name: EUREKACLIENTPRODUCT
server:
port: 8080
eureka:
client:
register-with-eureka: true
fetch-registry: false
service-url:
defaultZone: http://localhost:8761/eureka
service-url:
defaultZone: http://localhost:8761/eureka
客户端的defaultZone 要指向服务注册中心暴露的服务
4 - 开发注意事项
- 服务注册中心自己向自己注册的配置项一定要关闭
- 服务注册中心把自己设置为服务注册中心的配置项改为false(false表示自己是注册中心,true表示自己不是注册中心)
- 注册中心一定比客户端先开启