1.3 创建service
spring-cloud-alibaba-demo-dubbo-provider-service
pom.xml
xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.lzj</groupId> <artifactId>spring-cloud-alibaba-demo-dubbo-provider</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>spring-cloud-alibaba-demo-dubbo-provider-service</artifactId> <packaging>jar</packaging> <dependencies> <!-- Spring Boot Begin --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Spring Boot End --> <!-- Projects Begin --> <dependency> <groupId>com.lzj</groupId> <artifactId>spring-cloud-alibaba-demo-dubbo-provider-api</artifactId> <version>${project.parent.version}</version> </dependency> <!-- Projects End --> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.lzj.spring.cloud.alibaba.dubbo.provider.ProviderDubboApplication</mainClass> </configuration> </plugin> </plugins> </build> </project>
ProviderDubboApplication
java
@SpringBootApplication public class ProviderDubboApplication { public static void main(String[] args) { SpringApplication.run(ProviderDubboApplication.class,args); } }
application.yml
yaml
spring: application: name: dubbo-provider main: allow-bean-definition-overriding: true dubbo: scan: base-packages: com.lzj.spring.cloud.alibaba.dubbo.provider.service protocol: name: dubbo port: 20881 #使用kryo实现高速序列化 serialization: kryo provider: #负载策略 loadbalance: roundrobin registry: address: nacos://127.0.0.1:8848
HelloServiceImpl
java
package com.lzj.spring.cloud.alibaba.dubbo.provider.service; import com.lzj.spring.cloud.alibaba.dubbo.provider.HelloService; import org.apache.dubbo.config.annotation.Service; /** * @author Zijian Liao * @date 2020/1/17 18:48 * @description */ @Service(version = "1.0.0") public class HelloServiceImpl implements HelloService { @Override public String hello(String string) { return "Hello dubbo!"; } }
1.4 更新消费者配置
pom.xml
xml
<!-- Projects Begin --> <dependency> <groupId>com.lzj</groupId> <artifactId>spring-cloud-alibaba-demo-dubbo-provider-api</artifactId> <version>${project.parent.version}</version> <exclusions> <exclusion> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-registry-nacos</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-registry-nacos</artifactId> <version>2.7.3</version> </dependency> <!-- Projects End -->
ConsumerController
java
@Reference(version = "1.0.0") private HelloService helloService; @GetMapping("/hello/{str}") public String hello(@PathVariable String str){ return "Consumer send hello and reply is:" + helloService.hello(str); }
更新配置
yaml
dubbo: scan: #扫描包 base-packages: com.lzj.spring.cloud.alibaba.consumer.controller protocol: name: dubbo port: -1 #使用kryo实现序列化 serialization: kryo registry: address: nacos://127.0.0.1:8848
1.使用路由网关
1.1 创建网关GateWay
spring-cloud-alibaba-demo-gateway
pom.xml
xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.lzj</groupId> <artifactId>spring-cloud-alibaba-demo</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>spring-cloud-alibaba-demo-gateway</artifactId> <packaging>jar</packaging> <dependencies> <!-- Spring Boot Begin --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Spring Boot End --> <!-- Spring Cloud Begin --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <!-- Spring Cloud End --> <!-- Commons Begin --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> <!-- Commons Begin --> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.lzj.spring.cloud.alibaba.gateway.GatewayApplication</mainClass> </configuration> </plugin> </plugins> </build> </project>
GatewayApplication
java
@SpringBootApplication @EnableDiscoveryClient public class GatewayApplication { public static void main(String[] args) { SpringApplication.run(GatewayApplication.class,args); } }
application.yml
yaml
spring: application: # 应用名称 name: gateway main: allow-bean-definition-overriding: true cloud: # 使用 Nacos 作为服务注册发现 nacos: discovery: server-addr: 127.0.0.1:8848 # 路由网关配置 gateway: # 设置与服务注册发现组件结合,这样可以采用服务名的路由策略 discovery: locator: enabled: true # 配置路由规则 routes: # 采用自定义路由 ID(有固定用法,不同的 id 有不同的功能,详见:https://cloud.spring.io/spring-cloud-gateway/2.0.x/single/spring-cloud-gateway.html#gateway-route-filters) - id: provider # 采用 LoadBalanceClient 方式请求,以 lb:// 开头,后面的是注册在 Nacos 上的服务名 uri: lb://service-provider predicates: # 路径匹配,以 api 开头,直接配置是不生效的,看 filters 配置 - Path=/api/provider/** filters: # 此处配置去掉 2 个路径前缀,再配置上面的 Path=/api/provider/**,就能按照 http://localhost:8888/api/provider/** 的方式访问了 - StripPrefix=2 - id: consumer uri: lb://service-consumer predicates: - Path=/api/consumer/** filters: - StripPrefix=2 server: port: 8888 logging: level: org.springframework.cloud.gateway: debug
访问: