spring cloud 之 dubbo nacos整合

整体思路:

+  搭建本地nacos服务,详见docker安装nacos_xgjj68163的博客-CSDN博客

+   共三个工程,生产者服务、消费者服务、生产者和消费者共同依赖的接口工程(打成jar,供生产者和消费者依赖);

+   生产者注册服务到nacos,消费者调用nacos上的生产者服务;

目录

1. 共同依赖的接口服务搭建

1.1 pom

1.2 公共接口

1.3 maven install , 将可依赖jar包安装到本地仓库 

 2. 生产者服务搭建

2.1 生产者服务pom

2. 配置文件及注册服务

3. 消费者服务搭建

3.1 消费者服务pom

3.2 nacos及dubbo配置

 3.3 调用dubbo服务

4. 测试

4.1 生产者服务注册成功

 4.2 消费者服务注册成功

 4.3 测试controller,消费者调用生成者服务


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=8089

spring.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 {

    @DubboReference
    private 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);
	}
}

4. 测试

4.1 生产者服务注册成功

 4.2 消费者服务注册成功

 4.3 测试controller,消费者调用生成者服务

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot应用中使用DubboNacos可以通过以下步骤实现: 1. 添加DubboNacos的依赖 ```xml <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> <version>${nacos.version}</version> </dependency> ``` 2. 在application.properties文件中配置DubboNacos ```properties #Dubbo配置 dubbo.application.name=demo-provider dubbo.registry.address=nacos://${nacos.host}:${nacos.port} dubbo.scan.basePackages=com.example.demo.service.impl #Nacos配置 nacos.host=127.0.0.1 nacos.port=8848 ``` 3. 创建Dubbo服务接口和实现类 ```java public interface DemoService { String sayHello(String name); } @Service(version = "1.0.0") public class DemoServiceImpl implements DemoService { @Override public String sayHello(String name) { return "Hello " + name; } } ``` 4. 在启动类上添加注解@EnableDubbo和@EnableDiscoveryClient ```java @SpringBootApplication @EnableDubbo @EnableDiscoveryClient public class DemoProviderApplication { public static void main(String[] args) { SpringApplication.run(DemoProviderApplication.class, args); } } ``` 5. 启动应用 6. 在Dubbo Consumer端调用服务 ```java @RestController public class DemoController { @Reference(version = "1.0.0") private DemoService demoService; @GetMapping("/hello") public String sayHello(@RequestParam String name) { return demoService.sayHello(name); } } ``` 以上就是在Spring Boot应用中使用DubboNacos的简单实现方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值