第一步:创建一个名字为springcloud-demo的springboot项目
第二步:在该工程里面新建4个模块,名字分别为:
discovery,gateway,service,service2
第三步:完成各模块配置
1. discovery模块中,application.properties中写入:
server.port=8081
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
2. gateway模块中,application.properties中写入:
server.port=8084
spring.application.name=gateway
eureka.client.service-url.defaultZone=http://localhost:8081/eureka/
zuul.routes.service=/service/**
zuul.routes.service2=/service2/**
3. service模块中,application.properties中写入:
spring.application.name=service.service
eureka.client.service-url.defaultZone=http://localhost:8081/eureka/
server.port=8082
4. service2模块中,application.properties中写入:
spring.application.name=service2
eureka.client.service-url.defaultZone=http://localhost:8081/eureka/
server.port=8083
第四步:启动项目
按照顺序依次启动项目:discovery,gateway,service,service2
第五步: 测试结果
在各模块中写一个GetMapping("/get")的controller文件
在浏览器输入:
http://localhost:8081/service/get
http://localhost:8081/service2/get
根据返回值可以看出发出的请求被转发到了8082和8083端口
网友评论