先啟動nacos服務
服務端程式碼:
application.properties配置:
nacos.config.server-addr=127.0.0.1:8848
endpoint http://localhost:8080/actuator/nacos-config
health http://localhost:8080/actuator/health
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
##dubbo config
spring.application.name=test
dubbo.registry.address=nacos://127.0.0.1:8848
dubbo.application.name=spring-boot-nacos
dubbo.scan.basePackages=com.alibaba.nacos.example.spring.boot.service
dubbo.protocol.name=dubbo
dubbo.protocol.port=-1
<properties>
<spring-boot.version>2.0.3.RELEASE</spring-boot.version>
<nacos-config-spring-boot.version>0.2.1</nacos-config-spring-boot.version>
<netty-all.version>4.0.35.Final</netty-all.version>
</properties>
<dependencies>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>${netty-all.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>${nacos-config-spring-boot.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-actuator</artifactId>
<version>${nacos-config-spring-boot.version}</version>
</dependency>
<!-- Dubbo Nacos registry dependency -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>0.0.1</version>
</dependency>
<!-- Dubbo dependency -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.5</version>
</dependency>
<!-- Alibaba Spring Context extension -->
<dependency>
<groupId>com.alibaba.spring</groupId>
<artifactId>spring-context-support</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.1.RELEASE</version>
<exclusions>
<exclusion>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
@SpringBootApplication
@NacosPropertySource(dataId = "example", autoRefreshed = true)
public class NacosConfigApplication {
public static void main(String[] args) {
SpringApplication.run(NacosConfigApplication.class, args);
}
}
package com.alibaba.nacos.interfaces;
/**
* @author yanming
* @version 1.0.0
* @description
* @date 2019/02/20 18:51
**/
public interface ITestNacos {
String getOrderFormat(Integer integer);
}
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.nacos.interfaces.ITestNacos;
@Service(group = "test-nacos2", retries = 0, timeout = 10000)
public class TestNacosService implements ITestNacos {
@Override
public String getOrderFormat(Integer integer) {
return "test001";
}
}
啟動服務端,登陸nacos控制檯,可以看到服務已經註冊:
客戶端程式碼:
application.properties配置,pom檔案配置與服務端相同,
啟動類:
@SpringBootApplication
public class NacosDubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(NacosDubboConsumerApplication.class, args);
}
}
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.nacos.interfaces.ITestNacos;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @author yanming
* @version 1.0.0
* @description
* @date 2019/02/21 9:46
**/
@Controller
@RequestMapping("consumer")
public class TestNacosConsumer {
@Reference(group = "test-nacos2")
ITestNacos iTestNacos;
@RequestMapping(value = "test",method = RequestMethod.GET)
@ResponseBody
public String getCounsumerTest(){
return iTestNacos.getOrderFormat(1);
}
}