赞
踩
用户服务:muu-admin
商品服务:muu-commodity
公共服务:muu-common
网关:muu-gateway
muu-admin、muu-commodity 模块勾选Web-String Web、SpringCloudRouting-OpenFeign
muu-gateway 模块勾选Web-String Web、SpringCloudRouting-Gateway
父pom别忘了加入pom,不然打包失败。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
修改每个模块parent全部引用父模块
所有子pom里的dependencyManagement移动到父pom,交给父pom管理。
<!--nacos注册服务-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>${alibaba-nacos-discovery.version}</version>
</dependency>
公共服务引入,然后其他模块引入公共服务就可以了
需要注册nacos的启动类上加上@EnableDiscoveryClient注解
package com.mz.gateway; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient @SpringBootApplication public class MuuGatewayApplication { public static void main(String[] args) { SpringApplication.run(MuuGatewayApplication.class, args); } }
server:
port: 8000
spring:
cloud:
nacos:
discovery:
server-addr: 10.2.26.52:8848
group: mz
namespace: mz-muu
application:
name: muu-admin
nacos客户端下载地址https://github.com/alibaba/nacos/releases
server: port: 8002 spring: cloud: nacos: discovery: server-addr: 10.2.26.52:8848 group: mz namespace: mz-muu # 添加网关 gateway: enabled: true routes: # id唯一表示 路由至admin服务 - id: admin-route # lb://服务名称 (application.name) uri: lb://muu-admin # 访问地址 predicates: - Path=/api/v1/admin/** filters: - StripPrefix=3 application: name: muu-gateway
演示commodity模块调用admin模块
package com.mz.commodity; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; @EnableDiscoveryClient @SpringBootApplication @EnableFeignClients public class MuuCommodityApplication { public static void main(String[] args) { SpringApplication.run(MuuCommodityApplication.class, args); } }
package com.mz.admin.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Date; @RestController @RequestMapping("/user") public class AdminController { /** * 获取当前时间 */ @GetMapping("/getDate") public Date getDate() { return new Date(); } }
package com.mz.commodity.client;
import com.mz.commodity.client.fallback.CommodityClientFallback;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.Date;
@FeignClient(value = "muu-admin",fallback = CommodityClientFallback.class)
public interface AdminClient {
@GetMapping("/user/getDate")
public Date getDate();
}
调用结果
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。