整体思路:
+ 搭建本地nacos服务,详见docker安装nacos_xgjj68163的博客-CSDN博客
+ 共三个工程,生产者服务、消费者服务、生产者和消费者共同依赖的接口工程(打成jar,供生产者和消费者依赖);
+ 生产者注册服务到nacos,消费者调用nacos上的生产者服务;
1. 共同依赖的接口服务搭建
1.1 pom
注意:
其中build plugins spring-boot-maven-plugin插件,classifier为exec,表示构建可依赖的jar包及可启动的jar包
<?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>hj.example</groupId><artifactId>springboot-provider</artifactId><version>0.0.1-SNAPSHOT</version><relativePath/> <!-- lookup parent from repository --></parent><artifactId>sample-api</artifactId><version>0.0.1-SNAPSHOT</version><name>sample-api</name><description>sample-api</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><classifier>exec</classifier></configuration></plugin></plugins></build></project>
1.2 公共接口
package hj.example.sample;public interface IHelloService {String sayHello(String name);
}
1.3 maven install , 将可依赖jar包安装到本地仓库
2. 生产者服务搭建
2.1 生产者服务pom
包括4个依赖:接口依赖sample-api、nacos配置中心依赖spring-cloud-starter-alibaba-nacos-config、nacos注册中心依赖spring-cloud-starter-alibaba-nacos-discovery,spring-cloud dubbo依赖spring-cloud-starter-dubbo
<?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>hj.example</groupId><artifactId>springboot-provider</artifactId><version>0.0.1-SNAPSHOT</version><relativePath/> <!-- lookup parent from repository --></parent><artifactId>sample-provider</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>sample-provider</name><description>sample-provider</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>hj.example</groupId><artifactId>sample-api</artifactId><version>0.0.1-SNAPSHOT</version></dependency><!-- nacos --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId><version>2.2.5.RELEASE</version></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId><version>2.2.5.RELEASE</version></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-dubbo</artifactId><version>2.2.5.RELEASE</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><classifier>exec</classifier></configuration></plugin></plugins></build>
</project>
2. 配置文件及注册服务
通过@DubboService注解,将dubbo服务注册到nacos上;
dubbo配置,如果不在bootstrap.properties上配置spring.cloud.nacos.config.prefix,默认连接nacos配置中心的dubbo.properties配置文件;
程序优先读取bootstrap.properties配置文件,内容为:
spring.cloud.nacos.config.server-addr=127.0.0.1:8948
spring.cloud.nacos.config.username=nacos
spring.cloud.nacos.config.password=spring.cloud.nacos.config.enabled=false
application.propertes文件内容为:
spring.application.name=sample-provider
server.port=8089spring.cloud.nacos.discovery.server-addr=127.0.0.1:8948
spring.cloud.nacos.discovery.username=nacos
spring.cloud.nacos.discovery.password=
spring.cloud.nacos.discovery.service=sample-provider
nacos上dubbo.properties文件内容:
启动类:
package hj.example.sampleprovider;import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.ConfigurableApplicationContext;@DubboComponentScan
@EnableDiscoveryClient
@SpringBootApplication
@EnableDubbo(scanBasePackages="hj.example.sampleprovider.sample")
public class SampleProviderApplication {public static void main(String[] args) {
// Main.main(args);ConfigurableApplicationContext context = SpringApplication.run(SampleProviderApplication.class, args);String info = context.getEnvironment().getProperty("info");System.out.println("==========" + info);}
}
注册服务类:
package hj.example.sampleprovider.sample;import hj.example.sample.IHelloService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Value;@DubboService
public class HelloServiceImpl implements IHelloService {@Value("${dubbo.application.name}")private String serviceName;public String sayHello(String name) {return String.format("[%s]: Hello, %s", serviceName, name);}
}
3. 消费者服务搭建
3.1 消费者服务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><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.10.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>hj.example</groupId><artifactId>sample-consumer</artifactId><version>0.0.1-SNAPSHOT</version><name>sample-consumer</name><description>sample-consumer</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>hj.example</groupId><artifactId>sample-api</artifactId><version>0.0.1-SNAPSHOT</version></dependency><!-- nacos --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId><version>2.2.5.RELEASE</version></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId><version>2.2.5.RELEASE</version></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-dubbo</artifactId><version>2.2.5.RELEASE</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><classifier>exec</classifier></configuration></plugin></plugins></build></project>
3.2 nacos及dubbo配置
配置中心配置bootstrap.properties及nacos配置中心文件dubboConsumer.properties
bootstrap.properties
spring.cloud.nacos.config.server-addr=127.0.0.1:8948
spring.cloud.nacos.config.username=nacos
spring.cloud.nacos.config.password=spring.cloud.nacos.config.prefix=dubboConsumer.properties
dubboConsumer.properties
3.3 调用dubbo服务
使用注解@DubboReference调用dubbo服务,测试controller
package hj.example.sampleconsumer.controller;import hj.example.sample.IHelloService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class TestController {@DubboReferenceprivate IHelloService iHelloService;@RequestMapping("/test")public ResponseEntity<Object> test() {System.out.println("=========consumer test");String sayHelloRs = iHelloService.sayHello("hj");return new ResponseEntity<>(sayHelloRs, HttpStatus.OK);}
}
启动类:
package hj.example.sampleconsumer;import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication
@EnableDubbo
@EnableDiscoveryClient
public class SampleConsumerApplication {public static void main(String[] args) {SpringApplication.run(SampleConsumerApplication.class, args);}
}