# wx-cloud **Repository Path**: yiwuxia841971/wx-cloud ## Basic Information - **Project Name**: wx-cloud - **Description**: SpringCloud 个人学习版本 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-06-12 - **Last Updated**: 2022-01-20 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### 微服务结构学习项目 #### 1. 项目目录结构 ```java E:. ├─.idea │ └─inspectionProfiles ├─api-gateway │ ├─src │ │ └─main │ │ ├─java │ │ │ └─com │ │ │ └─wx │ │ │ └─api │ │ │ └─gateway │ │ └─resources │ └─target │ ├─classes │ │ └─com │ │ └─wx │ │ └─api │ │ └─gateway │ └─generated-sources │ └─annotations ├─doc │ └─sql ├─wx-feign-consumer │ ├─src │ │ └─main │ │ ├─java │ │ │ └─com │ │ │ └─wx │ │ │ └─web │ │ │ ├─controller │ │ │ └─feign │ │ └─resources │ └─target │ ├─classes │ │ └─com │ │ └─wx │ │ └─web │ │ ├─controller │ │ └─feign │ └─generated-sources │ └─annotations ├─wx-resources │ ├─src │ │ └─main │ │ └─resources │ └─target │ └─classes ├─wx-test │ ├─src │ │ ├─main │ │ │ └─java │ │ │ └─com │ │ │ └─wx │ │ │ └─test │ │ └─test │ │ └─java │ │ └─com │ │ └─wx │ │ └─test │ └─target │ ├─classes │ │ └─com │ │ └─wx │ │ └─test │ ├─generated-sources │ │ └─annotations │ ├─generated-test-sources │ │ └─test-annotations │ └─test-classes │ └─com │ └─wx │ └─test └─wx-web-provider ├─src │ ├─main │ │ ├─java │ │ │ └─com │ │ │ └─wx │ │ │ └─web │ │ │ └─controller │ │ └─resources │ └─test │ └─java │ └─com │ └─wx │ └─test └─target ├─classes │ └─com │ └─wx │ └─web │ └─controller ├─generated-sources │ └─annotations ├─generated-test-sources │ └─test-annotations ├─maven-status │ └─maven-compiler-plugin │ └─compile │ └─default-compile └─test-classes └─com └─wx └─test ``` 模块功能说明: 1. **api-gateway** :网关模块 2. **wx-resources** : 公共配置文件模块 3. **wx-feign-consumer**: feign消费模块 4. **wx-web-provider** :服务提供模块 5. **wx-test** :测试模块 #### 2. 代码功能实现 1. 主要依赖版本 ```xml 1.8 2.2.5.RELEASE 1.8 1.8 5.1.47 2.0.6.RELEASE Finchley.SR2 29.0-jre 2.0.4.RELEASE ``` 所有的服务模块都需要引入 ```xml com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config ${spring.cloud.alibaba.nacos.config.version} com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery ${spring.cloud.alibaba.nacos.config.version} ``` 其中nacos-config 用于读取nacos注册中心配置,nacos-discovery用户向nacos注册中心注册。 2. 公共模块配置文件如下 ```yaml spring: cloud: nacos: discovery: server-addr: 172.16.71.229:18848 service: ${spring.application.name} group: DEFAULT_GROUP namespace: b07283a9-2f49-4850-89f0-60fb57b8889c enabled: true config: enabled: true refresh-enabled: true server-addr: ${spring.cloud.nacos.discovery.server-addr} file-extension: yaml group: ${spring.cloud.nacos.discovery.group} namespace: ${spring.cloud.nacos.discovery.namespace} shared-configs: - dataId: iot-common-static.yaml refresh: false group: ${spring.cloud.nacos.discovery.group} - dataId: iot-common-refresh.yaml refresh: true group: ${spring.cloud.nacos.discovery.group} username: nacos password: nacos ``` 由于公共配置文件存放在wx-resources模块中,需用用到此配置文件的模块需要在pom.xml中进行如下配置 ```xml src/main/resources **/* false ../wx-resources/src/main/resources **/* false ``` 3. 网关实现,可以自定过滤器,通过实现GlobalFilter, Ordered等接口。基础配置文件如下 ```yaml server: port: 8075 servlet: context-path: /service spring: profiles: active: dev application: name: api-gateway cloud: gateway: discovery: locator: enabled: true #开启动态创建路由的功能,利用微服务名进行路由 lower-case-service-id: true routes: - id: api1 #唯一标识 uri: lb://wuxia-web-01 #转发的地址,写服务名称 predicates: - Path=${server.servlet.context-path}/api1/** # 匹配规则 filters: - StripPrefix=1 # 转发时把/service 去掉 - id: api2 uri: lb://wuxia-web-02 predicates: - Path=${server.servlet.context-path}/api2/** filters: - StripPrefix=1 ``` 4. feign服务间调用实现 * 消费方模块中需要引入 ```xml org.springframework.cloud spring-cloud-starter-openfeign ``` * 启动类添加注解 ```java @EnableFeignClients ``` * 消费方模块定义接口 ,url和参数需要和提供方的接口保持一致 ```java @FeignClient(value = "wuxia-web-01") //服务提供放服务名 public interface ComputeClient { @GetMapping(value = "/api1/ali/now") String test(); } ``` * 使用时直接通过@Autowired注入, 即可使用,像调用本地方法一样调用远程方法。 ### 参考链接 * [详解SpringCloud新一代网关Gateway](https://www.jb51.net/article/216154.htm)