跳转至

注册中心

创建项目:

image-20200817083531470

新增应用:

image-20200817084450201

编写代码测试

maven 库:

<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-framework</artifactId>
    <version>4.2.0</version>
</dependency>
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-recipes</artifactId>
    <version>4.2.0</version>
</dependency>
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>2.7.3</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.alibaba.boot/nacos-config-spring-boot-starter -->
<dependency>
    <groupId>com.alibaba.boot</groupId>
    <artifactId>nacos-config-spring-boot-starter</artifactId>
    <version>0.2.7</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.alibaba.boot/nacos-discovery-spring-boot-starter -->
<dependency>
    <groupId>com.alibaba.boot</groupId>
    <artifactId>nacos-discovery-spring-boot-starter</artifactId>
    <version>0.2.7</version>
</dependency>

provider:

server.port=10010
dubbo.application.name=zeustest
dubbo.registry.address=nacos://192.168.200.17:20003?namespace=test
dubbo.provider.threads=10
dubbo.provider.threadpool=fixed
dubbo.provider.loadbalance=roundrobin
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880

consumer:

server.port=10011
dubbo.application.id=consumer
dubbo.application.name=consumer
dubbo.registry.address=nacos://192.168.200.17:20003?namespace=test
dubbo.consumer.check=true

启动一遍 provider 后,把 zeus 数据库中对应 app 的 valid 标识设为 1:

image-20200817094236203

之后再启动 provider 和 consumer :

image-20200817094853453

配置中心

在项目索引配置中,新增应用分组:

新增以下索引:

在“集中配置管理”中,新增几个测试用配置:

# conf 索引中的配置
conf_key1=value1
conf_key2=value2

# consumer_demo 索引的配置
hello_key=world_value
test_key1=test_value1
dubbo.consumer.check=true
dubbo.registry.address=nacos://192.168.200.17:20003?namespace=test
dubbo.application.name=consumer_demo

点击“整体发布所有配置”。

编写代码测试

测试 1

maven:

<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.7.3</version>
</dependency>
<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.6.1</version>
</dependency>
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-framework</artifactId>
    <version>4.2.0</version>
</dependency>
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-recipes</artifactId>
    <version>4.2.0</version>
</dependency>
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>2.7.3</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.alibaba.boot/nacos-config-spring-boot-starter -->
<dependency>
    <groupId>com.alibaba.boot</groupId>
    <artifactId>nacos-config-spring-boot-starter</artifactId>
    <version>0.2.7</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.alibaba.boot/nacos-discovery-spring-boot-starter -->
<dependency>
    <groupId>com.alibaba.boot</groupId>
    <artifactId>nacos-discovery-spring-boot-starter</artifactId>
    <version>0.2.7</version>
</dependency>

应用配置(只保留最基础的配置),applocation.properties:

server.port=10011
nacos.config.server-addr=192.168.200.17:20002

main 方法:

@SpringBootApplication
@NacosPropertySource(dataId = "consumer_demo", groupId = "demo")
@EnableDubbo
public class StarterConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(StarterConsumerApplication.class, args);
    }
}

controller:

@RestController
@RequestMapping("test")
public class TestController {
    @NacosValue(value = "${hello_key}")
    public String valueOfHelloKey;

    @GetMapping("/getV")
    public String getV() {
        return "hello=" + valueOfHelloKey;
    }
}

访问 http://localhost:10011/test/getV 测试:

image-20200817111722730

测试 dubbo(dubbo 配置从配置中心取):

image-20200817114302896

测试 2

本测试输出所有配置。

新增一个类用于日志获取和输出:

public class ServiceConf {
    private static final Logger logger = LoggerFactory.getLogger(ServiceConf.class);

    public void initNacos(String dataId, String groupId, String serverAddr) throws NacosException {
        ConfigService configService = getConfigService(serverAddr);
        String content = configService.getConfig(dataId, groupId, 5000);
        logger.error(content);
    }

    private ConfigService getConfigService(String serverAddr) throws NacosException {
        Properties properties = new Properties();
        properties.put("serverAddr", serverAddr);
        return NacosFactory.createConfigService(properties);
    }
}

修改 main 方法:

@SpringBootApplication
@NacosPropertySource(dataId = "consumer_demo", groupId = "demo")
@EnableDubbo
public class StarterConsumerApplication {
    public static void main(String[] args) throws NacosException {
        // 添加以下一行:
        new ServiceConf().initNacos("consumer_demo", "demo", "192.168.200.17:20002");
        SpringApplication.run(StarterConsumerApplication.class, args);
    }
}

输出:

image-20200817120206888