spring-cloud整合dubbo使用nacos做注册中心

nacos安装和入门

nacos简单入个门

nacos 需要准备的地方

spring-cloud整合dubbo使用nacos做注册中心_第1张图片

创建父工程


<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.0modelVersion>

    <groupId>com.zhazhagroupId>
    <artifactId>spring-nacos-parentartifactId>
    <packaging>pompackaging>
    <version>1.0-SNAPSHOTversion>
    <modules>
        <module>spring-cloud-nacos-dubbo-producermodule>
        <module>spring-cloud-nacos-dubbo-apimodule>
        <module>spring-cloud-nacos-dubbo-consumermodule>
    modules>

    <properties>
        <maven.compiler.source>1.8maven.compiler.source>
        <maven.compiler.target>1.8maven.compiler.target>
    properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.bootgroupId>
                <artifactId>nacos-discovery-spring-boot-actuatorartifactId>
                <version>0.2.11version>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-dependenciesartifactId>
                <version>2.7.1version>
                <type>pomtype>
                <scope>importscope>
            dependency>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>2021.0.3version>
                <type>pomtype>
                <scope>importscope>
            dependency>
            <dependency>
                <groupId>com.alibaba.cloudgroupId>
                <artifactId>spring-cloud-alibaba-dependenciesartifactId>
                <version>2021.1version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

project>

创建 api 项目

spring-cloud整合dubbo使用nacos做注册中心_第2张图片

这个项目主要是给服务的提供方和消费方提供一个公共的接口协议(接口参数)

服务的提供方


<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>spring-nacos-parentartifactId>
        <groupId>com.zhazhagroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>spring-cloud-nacos-dubbo-producerartifactId>

    <properties>
        <maven.compiler.source>8maven.compiler.source>
        <maven.compiler.target>8maven.compiler.target>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-bootstrapartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
        dependency>
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
        dependency>
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-dubboartifactId>
        dependency>
        <dependency>
            <groupId>com.zhazhagroupId>
            <artifactId>spring-cloud-nacos-dubbo-apiartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
        dependency>
        <dependency>
            <groupId>org.apache.commonsgroupId>
            <artifactId>commons-lang3artifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

project>

bootstrap.yml 这个文件主要给后期将我们的 application.yml配置上传到 nacos

spring:
  cloud:
    nacos:
      config:
        server-addr: 127.0.0.1:8848
        file-extension: yaml
        namespace: edc7dbc5-31bb-4bb6-bbe7-b76e0828e918

application.yml

server:
  port: 8080
spring:
  main:
    allow-bean-definition-overriding: true
    allow-circular-references: true
  application:
    name: dubbo-producer
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
        # 这个只能修改 dubbo-producer 注册到 nacos的分组,不会修改 dubbo 注册到 nacos 的分组
        namespace: edc7dbc5-31bb-4bb6-bbe7-b76e0828e918
        # 这个只能修改 dubbo-producer 注册到 nacos 的分组,不会修改dubbo注册到 nacos 的分组
        group: spring-cloud
        username: nacos
        password: nacos
dubbo:
  scan:
    base-packages: com.zhazha.producer.service.impl
  protocol:
    # dubbo 协议名称
    name: dubbo
    # dubbo 协议端口( -1 表示自增端口,从 20880 开始)
    port: -1
  registry:
    address: nacos://127.0.0.1:8848
    # 修改 dubbo 在 nacos 上的命名空间
    parameters[namespace]: edc7dbc5-31bb-4bb6-bbe7-b76e0828e918
    # 修改 dubbo 在 nacos 上的分组
    group: spring-cloud
  application:
    # 关闭 dubbo 在线运维功能,该功能主要通过 telnet 方式进行服务的权重调整
    qos-enable: false

注意看注释的地方

package com.zhazha.producer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class NacosDubboProducerApplication {
	
	public static void main(String[] args) throws Exception {
		SpringApplication.run(NacosDubboProducerApplication.class, args);
	}
	
}

package com.zhazha.producer.service.impl;

import com.zhazha.api.HelloService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;

@DubboService
@Slf4j
public class HelloServiceImpl implements HelloService {
	@Override
	public String sayHi() {
		log.info("nacos-dubbo-producer");
		return "nacos-dubbo-producer";
	}
}

服务的提供方步骤结束

服务的消费方


<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>spring-nacos-parentartifactId>
        <groupId>com.zhazhagroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>spring-cloud-nacos-dubbo-consumerartifactId>

    <properties>
        <maven.compiler.source>8maven.compiler.source>
        <maven.compiler.target>8maven.compiler.target>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-bootstrapartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
        dependency>
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
        dependency>
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-dubboartifactId>
        dependency>
        <dependency>
            <groupId>com.zhazhagroupId>
            <artifactId>spring-cloud-nacos-dubbo-apiartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
        dependency>
        <dependency>
            <groupId>org.apache.commonsgroupId>
            <artifactId>commons-lang3artifactId>
        dependency>
    dependencies>

project>

bootstrap.yml

spring:
  cloud:
    nacos:
      config:
        server-addr: 127.0.0.1:8848
        file-extension: yaml
        namespace: edc7dbc5-31bb-4bb6-bbe7-b76e0828e918

application.yml

server:
  port: 8081
spring:
  main:
    allow-bean-definition-overriding: true
    allow-circular-references: true
  application:
    name: dubbo-consumer
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
        # 这个只能修改 dubbo-producer 注册到 nacos的分组,不会修改 dubbo 注册到 nacos 的分组
        namespace: edc7dbc5-31bb-4bb6-bbe7-b76e0828e918
        # 这个只能修改 dubbo-producer 注册到 nacos 的分组,不会修改dubbo注册到 nacos 的分组
        group: spring-cloud
        username: nacos
        password: nacos
dubbo:
#  scan:
#    base-packages: com.zhazha.producer.service.impl
  protocol:
    # dubbo 协议名称
    name: dubbo
    # dubbo 协议端口( -1 表示自增端口,从 20880 开始)
    port: -1
  registry:
    address: nacos://127.0.0.1:8848
    # 修改 dubbo 在 nacos 上的命名空间
    parameters[namespace]: edc7dbc5-31bb-4bb6-bbe7-b76e0828e918
    # 修改 dubbo 在 nacos 上的分组
    group: spring-cloud
  application:
    # 关闭 dubbo 在线运维功能,该功能主要通过 telnet 方式进行服务的权重调整
    qos-enable: false

注意上面注释的地方

package com.zhazha.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class NacosDubboConsumerApplication {
	
	public static void main(String[] args) throws Exception {
		SpringApplication.run(NacosDubboConsumerApplication.class, args);
	}
	
}

package com.zhazha.consumer.controller;

import com.zhazha.api.HelloService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DubboController {
	
	@DubboReference
	private HelloService helloService;
	
	@GetMapping("sayHi")
	public String sayHi() {
		return helloService.sayHi();
	}
	
}

项目需要准备的就这些了

调用成功:

spring-cloud整合dubbo使用nacos做注册中心_第3张图片

spring-cloud整合dubbo使用nacos做注册中心_第4张图片

同时 dubbo 也会在 配置中心记录一些数据

spring-cloud整合dubbo使用nacos做注册中心_第5张图片

配置上传到 nacos

现在将application.yml内的内容上传到 nacos配置中心上

创建专用于配置的命名空间

image.png

提供方配置修改

忘记 dubbo 会占用配置中心了,所以另外新建一个,

bootstrap.yml

spring:
  application:
    name: dubbo-producer
  main:
    allow-bean-definition-overriding: true
    allow-circular-references: true
  cloud:
    nacos:
      config:
        server-addr: 127.0.0.1:8848
        file-extension: yaml
        namespace: de98a7a1-8814-44c5-a635-e19872132371
        username: nacos
        password: nacos

application.yml

server:
  port: 8080

去新建配置:

spring-cloud整合dubbo使用nacos做注册中心_第6张图片

dubbo-producer.yaml填入内容:

spring:
  application:
    name: dubbo-producer
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
        # 这个只能修改 dubbo-producer 注册到 nacos的分组,不会修改 dubbo 注册到 nacos 的分组
        namespace: edc7dbc5-31bb-4bb6-bbe7-b76e0828e918
        # 这个只能修改 dubbo-producer 注册到 nacos 的分组,不会修改dubbo注册到 nacos 的分组
        group: spring-cloud
dubbo:
  scan:
    base-packages: com.zhazha.producer.service.impl
  protocol:
    # dubbo 协议名称
    name: dubbo
    # dubbo 协议端口( -1 表示自增端口,从 20880 开始)
    port: -1
  registry:
    address: nacos://127.0.0.1:8848
    # 修改 dubbo 在 nacos 上的命名空间
    parameters[namespace]: edc7dbc5-31bb-4bb6-bbe7-b76e0828e918
    # 修改 dubbo 在 nacos 上的分组
    group: spring-cloud

消费方配置修改

bootstrap.yml

spring:
  application:
    name: dubbo-consumer
  main:
    allow-bean-definition-overriding: true
    allow-circular-references: true
  cloud:
    nacos:
      config:
        server-addr: 127.0.0.1:8848
        file-extension: yaml
        namespace: de98a7a1-8814-44c5-a635-e19872132371
        username: nacos
        password: nacos

application.yml

server:
  port: 8081

在nacos上创建dubbo-consumer.yaml

spring-cloud整合dubbo使用nacos做注册中心_第7张图片

spring:
  application:
    name: dubbo-consumer
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
        # 这个只能修改 dubbo-producer 注册到 nacos的分组,不会修改 dubbo 注册到 nacos 的分组
        namespace: edc7dbc5-31bb-4bb6-bbe7-b76e0828e918
        # 这个只能修改 dubbo-producer 注册到 nacos 的分组,不会修改dubbo注册到 nacos 的分组
        group: spring-cloud
        username: nacos
        password: nacos
dubbo:
#  scan:
#    base-packages: com.zhazha.producer.service.impl
  protocol:
    # dubbo 协议名称
    name: dubbo
    # dubbo 协议端口( -1 表示自增端口,从 20880 开始)
    port: -1
  registry:
    address: nacos://127.0.0.1:8848
    # 修改 dubbo 在 nacos 上的命名空间
    parameters[namespace]: edc7dbc5-31bb-4bb6-bbe7-b76e0828e918
    # 修改 dubbo 在 nacos 上的分组
    group: spring-cloud
  application:
    # 关闭 dubbo 在线运维功能,该功能主要通过 telnet 方式进行服务的权重调整
    qos-enable: false

最后
spring-cloud整合dubbo使用nacos做注册中心_第8张图片

前面的入门篇已经讲过了,nacos配置中心的名字必须怎样,回到入门篇去看
spring-cloud整合dubbo使用nacos做注册中心_第9张图片

你可能感兴趣的:(springboot,服务发现,java,dubbo,spring,java,服务发现)