全栈项目-乐优商场-java后端-Eureka与Zuul微服务创建与启动
目录
文章目录
内容
上一篇博文全栈项目-乐优商场-后端项目初始化我们完成java后端项目初始化,创建了leyou父工程,接下来我要搭建Eureka与Zuul微服务。
- 图示0-1:
1、创建Eureka微服务模块
- 右键leyou文件夹->new->module,选择Maven工程,下一步
- 配置模块信息,如图所示:
2、配置与启动Eureka模块
-
'ly-registry’模块下pom.xml,添加如下依赖包
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies>
-
新建LyRegistry启动类
-
在ly-registry->src->main->java包下新建com.leyou包
-
leyou包下新建LyRegistry类
-
代码2-1:如下
package com.leyou; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer @SpringBootApplication public class LyRegistry { public static void main(String[] args) { SpringApplication.run(LyRegistry.class); } }
-
-
新建application.yml配置文件
-
在ly-registry->src->main->resources包下新建
-
配置内容如下:
spring: application: name: ly-registry server: port: 10011 eureka: client: service-url: defaultZone: http://127.0.0.1:10011/eureka
-
目录结构图示3-1:
- 测试,点击启动类内启动按钮,查看是否启动成功
-
启动图示4-1:
-
启动成功图示4-2:
-
页面地址栏输入,localhost:10011,查看效果图示4-3:
-
至此Eureka微服务创建启动成功,关于eureka注册中心的相关知识可自行查询性格文档。
3、Zuul微服务创建与启动
3.1、创建Zuul微服务模块’ly-gateway’
创建过程同‘ly-registry’,模块名称改为’ly-gateway‘即可
3.2、pom.xml配置
引入如下依赖:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
3.3、创建启动类与配置文件
启动类与配置文件包结构同上
启动类LyGateway.java代码3.3-1如下:
package com.leyou;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@EnableZuulProxy
@SpringCloudApplication
public class LyGateway {
public static void main(String[] args) {
SpringApplication.run(LyGateway.class);
}
}
配置文件application.yml内容如下:
spring:
application:
name: api-gateway
server:
port: 10021
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10011/eureka
zuul:
prefix: /api
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 5000
ribbon:
ConnectTimeout: 1000
ReadTimeout: 3500
MaxAutoRetries: 0
MaxAutoRetriesNextServer: 0
结构图示3.3-1:
项目结构图示3.3-2:
启动图示3.3-3:
Eureka注册中心页面图示3.3-4:
- 注意事项:
- IDEA2020 Services 面板取代了以前的Run Dashboard面板
至此Zuul网关微服务创建与启动完成
未避免端口冲突,以上配置端口统一最后一位统一改为1,和图片有出入,不用在意。
后记 :
本项目为参考某马视频开发,相关视频及配套资料可自行度娘或者联系本人。上面为自己编写的开发文档,持续更新。欢迎交流,本人QQ:806797785
前端项目源代码地址:https://gitee.com/gaogzhen/vue
后端JAVA源代码地址:https://gitee.com/gaogzhen/JAVA