说明 : EnableDiscoveryClient跟 EnableEurekaClient 区别
用@EnableDiscoveryClient注解标明后,该微服务会被Eureka-Server
扫描找到,这里也可以用@EnableEurekaClient,
他们不同在于@EnableEurekaClient只适用于Eureka-Server发现服务,而@EnableDiscoveryClient也适用其他发现组件。
————————————————
版权声明:本文为CSDN博主「nanfeiliulanghan」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/nanfeiliulanghan/article/details/90423824
注意: 创建完pom文件后 父pom需要添加pom格式
spring boot版本跟spring cloud版本要一直 不然报错 No qualifying bean of type ‘javax.servlet.Filter’ available
待添加 spring boot跟spring cloud版本对应
<packaging>pom</packaging>
子pom需要添加为jar 格式
<packaging>jar</packaging>
在这里插入代码片
第一步
创建父项目 pom文件引入spring clou的依赖 父pom进行版本管理
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
# <relativePath/> <!-- lookup parent from repository --> 必须删除 这里做提示
</parent>
<groupId>com.example</groupId>
<artifactId>demo-spring-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo-spring-cloud</name>
<description>Demo project for Spring Boot</description>
<!--统一版本号-->
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- spring cloud依赖-->
<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>
<!-- 国内maven镜像库 公司内一般配置在maven setting文件内-->
<repositories>
<repository>
<id>central</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<layout>default</layout>
<!-- 是否开启发布版构件下载 -->
<releases>
<enabled>true</enabled>
</releases>
<!-- 是否开启快照版构件下载 -->
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
第二步 新建项目 创建 注册中心
pom 依赖
<parent>
<groupId>com.example</groupId>
<artifactId>demo-spring-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>discorvey</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>discorvey</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<!--注册中心依赖 eureka-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!--maven 启动插件-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
启动类配置注解
@EnableEurekaServer
yml配置
server:
port: 8761 # 默认8761
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
fetch-registry: false #
register-with-eureka: false # 默认不注册到注册中心 集群打开
server:
enableSelfPreservation: false
evictionIntervalTimerInMs: 4000 # 清理间隔(单位毫秒,默认是60*1000)
spring:
application:
name: center # 应用名
第三步 创建 gateway
pom文件
<parent>
<groupId>com.example</groupId>
<artifactId>demo-spring-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>gateway</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<!--网关依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build
启动类注解
@EnableZuulProxy
yml配置文件
server:
port: 8180
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka # 注册中心地址
spring:
application:
name: gateway
zuul:
routes:
# 复杂写法
# customer:
# path: /customer/** # 映射路径
# serviceId: customer # 应用名
customer: /customer/** # 简单写法
应用名 映射路径
第四步消费者
pom依赖
<parent>
<groupId>com.example</groupId>
<artifactId>demo-spring-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>customer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>customer</name>
<packaging>jar</packaging>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>common</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
启动类注解
@SpringBootApplication
@EnableFeignClients("com.example.common.service")
public class CustomerApplication {
public static void main(String[] args) {
SpringApplication.run(CustomerApplication.class, args);
}
}
yml配置
server:
port: 8181
servlet:
context-path: /customer
spring:
application:
name: customer
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka # 注册中心地址
register-with-eureka: false # 可不配置 配置false可在不启动注册中心时不报错
fetch-registry: false #可不配置 配置false可在不启动注册中心时不报错