自己闲来无事搭建了一个微服务框架,慢慢研究
eureka的配置yaml和pom
启动类上的注解:
@EnableEurekaServer
1.8 UTF-8 UTF-8 2.3.7.RELEASE Hoxton.SR9 org.springframework.cloud spring-cloud-starter-netflix-eureka-server org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-dependencies ${spring-boot.version} pom import org.apache.maven.plugins maven-compiler-plugin 3.8.1 1.8 1.8 UTF-8 org.springframework.boot spring-boot-maven-plugin 2.3.7.RELEASE com.wangchuan.eureka.EurekaApplication repackage repackage
server:
port: 8081 #服务注册中心端口号
eureka:
instance:
hostname: 127.0.0.1 #服务注册中心IP地址
client:
registerWithEureka: false #是否向服务注册中心注册自己
fetchRegistry: false #是否检索服务
serviceUrl: #服务注册中心的配置内容,指定服务注册中心的位置
defaultZone: http://127.0.0.1:8081/eureka/
app1和app2的pom都一样
4.0.0 com.wangchuan app2 0.0.1-SNAPSHOT app2 业务服务1 1.8 UTF-8 UTF-8 2.3.7.RELEASE Hoxton.SR9 org.springframework.cloud spring-cloud-starter-netflix-eureka-server org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-openfeign org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-dependencies ${spring-boot.version} pom import org.apache.maven.plugins maven-compiler-plugin 3.8.1 1.8 1.8 UTF-8 org.springframework.boot spring-boot-maven-plugin 2.3.7.RELEASE com.wfw.clent2.Clent2Application repackage repackage
app1的yaml
eureka:
client:
serviceUrl: #注册中心的注册地址
defaultZone: http://127.0.0.1:8081/eureka/
server:
port: 8082 #服务端口号
spring:
application:
name: app1 #服务名称--调用的时候根据名称来调用该服务的方法
app2的yaml
eureka:
client:
serviceUrl: #注册中心的注册地址
defaultZone: http://127.0.0.1:8081/eureka/
server:
port: 8083 #服务端口号
spring:
application:
name: app2 #服务名称--调用的时候根据名称来调用该服务的方法
app1和2的启动类上都加了
@EnableFeignClients//启用feign进行远程调用
gateway的pom和yaml
4.0.0 com.wangchuan gateway 0.0.1-SNAPSHOT gateway Demo project for Spring Boot 1.8 UTF-8 UTF-8 2.3.7.RELEASE Hoxton.SR9 org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.cloud spring-cloud-starter-gateway 2.2.1.RELEASE org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.springframework.boot spring-boot-starter org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-dependencies ${spring-boot.version} pom import org.apache.maven.plugins maven-compiler-plugin 3.8.1 1.8 1.8 UTF-8 org.springframework.boot spring-boot-maven-plugin 2.3.7.RELEASE com.wfw.clent2.Clent2Application repackage repackage
server:
port: 8080
spring:
application:
name: gateway
cloud:
gateway:
discovery:
locator:
enabled: false
#开启小写验证,默认feign根据服务名查找都是用的全大写
lowerCaseServiceId: true
routes:
- id: app2
uri: lb://app2
predicates:
- Path=/api/**
eureka:
instance:
preferIpAddress: true
instance-id: ${spring.cloud.client.ip-address}:${server.port}
client:
service-url:
defaultZone: http://localhost:8081/eureka/
由上面的yaml可知,app1的端口8002.app2的端口是8083,网关端口是8080
我的测试业务逻辑就是app2调用app1的接口,整长调用127.0.0.1:8083/api/get/api事没有问题的,
我的网关作用是接口转发,请求同意走网关的,
调用127.0.0.1:8080/api/get/api2是可以的。



