赞
踩
最近公司的结构要向微服务发展,最终定的是 SpringBoot + Dubbo 的架构组合。使用Nacos 做配置中心和Rpc调用,协议使用 dubbo协议,然后使用某种方式序列化。
主要参考官方案例:
https://github.com/apache/dubbo-spring-boot-project/tree/master/dubbo-spring-boot-samples/service-introspection-samples
现在在这里做一个HelloWorld的案例,用以熟悉流程结构。
本案例的版本使用的是(注意最好不要修改版本,谁改谁知道):
- SpringBoot 版本:2.2.7.RELEASE
- dubbo-spring-boot-starter 版本:2.7.8
- nacos-client 版本:1.0.0
<?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>springboot-dubbo-starter-demo</groupId> <artifactId>dubbo-starter-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>dubbo-starter-demo</name> <description>dubbo-starter-demo</description> <properties> <spring-boot.version>2.2.7.RELEASE</spring-boot.version> <dubbo.version>2.7.8</dubbo.version> </properties> <dependencyManagement> <dependencies> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- Apache Dubbo --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-dependencies-bom</artifactId> <version>${dubbo.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- Dubbo Spring Boot Starter --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.7.8</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> <version>1.0.0</version> </dependency> </dependencies> </project>
#nacos.config.server-addr=genfjs.com:80 ##dubbo config spring.application.name=springboot-dubbo-nacos-provider server.port=10086 # nacos所在地址 nacos.server-address = gnefjs.com nacos.port = 80 nacos.username=nacos nacos.password=nacos dubbo.application.name=${spring.application.name} dubbo.registry.address=nacos://${nacos.server-address}:${nacos.port}/?username=${nacos.username}&password=${nacos.password}®istry-type=service # dubbo接口所在的包 dubbo.scan.base-packages=org.feng.service dubbo.protocol.name=dubbo dubbo.protocol.port=20880 # Provider @Service version demo.service.version=1.0.0 demo.service.name = demoService
package org.feng.service;
public interface DemoService {
String sayName(String name);
}
package org.feng.service.impl; import org.apache.dubbo.config.annotation.DubboService; import org.feng.service.DemoService; import org.springframework.beans.factory.annotation.Value; @DubboService(version = "1.0.0") public class DemoServiceImpl implements DemoService { @Value("${demo.service.name}") private String serviceName; @Override public String sayName(String name) { return String.format("[%s] : Hello, %s", serviceName, name); } }
package org.feng;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DubboStarterDemoApplication {
public static void main(String[] args) {
SpringApplication.run(DubboStarterDemoApplication.class, args);
}
}
<?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>org.feng</groupId> <artifactId>dubbo-starter-demo-consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <name>dubbo-starter-demo-consumer</name> <description>dubbo-starter-demo-consumer</description> <properties> <spring-boot.version>2.2.7.RELEASE</spring-boot.version> <dubbo.version>2.7.8</dubbo.version> </properties> <dependencies> <!-- Dubbo Spring Boot Starter --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.7.8</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> <version>1.0.0</version> </dependency> <!--本项目中使用它来输出日志--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.20</version> </dependency> <!--服务提供者:一般引入接口所在模块--> <dependency> <groupId>springboot-dubbo-starter-demo</groupId> <artifactId>dubbo-starter-demo</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
spring.application.name: dubbo-nacos-service-introspection-consumer-sample
nacos.server-address = gnefjs.com
nacos.port = 80
nacos.username=nacos
nacos.password=nacos
dubbo.registry.address=nacos://${nacos.server-address}:${nacos.port}/?username=${nacos.username}&password=${nacos.password}®istry-type=service
demo.service.version= 1.0.0
package org.feng; import lombok.extern.slf4j.Slf4j; import org.apache.dubbo.config.annotation.DubboReference; import org.feng.service.DemoService; import org.springframework.boot.ApplicationRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; @Slf4j @SpringBootApplication public class DubboStarterDemoConsumerApplication { @DubboReference(version = "1.0.0") DemoService demoService; public static void main(String[] args) { SpringApplication.run(DubboStarterDemoConsumerApplication.class, args); } @Bean public ApplicationRunner runner() { return args -> log.info(demoService.sayName("DubboStarterDemoConsumerApplication。。。")); } }
先保证自己的Nacos已经正常启动。
然后启动服务提供者。
在启动服务消费者。
运行结果是,在服务消费者这端,会输出日志:
[demoService] : Hello, DubboStarterDemoConsumerApplication。。。
表示运行正常!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。