spring cloud微服务整合dubbo和nacos
版本说明
spring-cloud-dependencies版本:Greenwich.SR5
spring-cloud-alibaba-dependencies版本:2.1.0.RELEASE
dubbo-spring-boot-starter版本:2.7.1
dubbo版本:2.7.3
nacos-client版本:1.1.1
源码流程图
若小伙伴还没有注册过processon,可以使用以下链接注册,支持小编3个文件数 -_-
注册processon
spring cloud微服务
微服务代码较多,读者可以下载源码自行参考
服务端 helle-service-dubbo模块
pom.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">
<parent>
<artifactId>springCloudDemo</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>helle-service-dubbo</artifactId>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>hello-service-api</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml
spring:
application:
name: hello-service-dubbo-provider
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.220.125:3306/mall?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
username: root
password: Root123
mybatis:
mapper-locations: classpath:mybatis/mapper/*.xml
dubbo:
application:
name: hello-service-dubbo-provider
protocol:
name: dubbo
port: -1
registry:
id: nacos-registry
address: nacos://192.168.220.125:8848
config-center:
address: nacos://192.168.220.125:8848
metadata-report:
address: nacos://192.168.220.125:8848
启动类
package org.example;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class ServiceDubboApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceDubboApplication.class, args);
}
}
service类,注意这里@Service注解使用的是org.apache.dubbo.config.annotation.Service
package org.example.service;
import org.apache.dubbo.config.annotation.Service;
import org.apache.dubbo.rpc.RpcContext;
import org.example.service.api.HelloService;
import org.springframework.transaction.annotation.Transactional;
@Service
public class HelloServiceImpl implements HelloService {
@Override
@Transactional
public String sayHello(String name) {
System.out.println("Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
return "Hello " + name;
}
}
客户端 consumer-dubbo模块
pom.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">
<parent>
<artifactId>springCloudDemo</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>consumer-dubbo</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>hello-service-api</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
application.yml
server:
port: 8765
spring:
application:
name: consumer-dubbo
dubbo:
application:
name: consumer-dubbo
protocol:
name: dubbo
port: -1
registry:
id: nacos-registry
address: nacos://192.168.220.125:8848
config-center:
address: nacos://192.168.220.125:8848
metadata-report:
address: nacos://192.168.220.125:8848
启动类
package org.example;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class DubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DubboConsumerApplication.class, args);
}
}
controller
package org.example.controller;
import org.apache.dubbo.config.annotation.Reference;
import org.example.service.api.HelloService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DubboHelloController {
@Reference
HelloService helloService;
@GetMapping(value = "/hi")
public String hi(@RequestParam String name) {
return helloService.sayHello( name );
}
}
测试验证
分别启动服务端和客户端,然后访问nacos,可以看到服务已经注册上来了
现在可以去浏览器验证一下服务是否可用
http://localhost:8765/hi?name=numberone