Spend small money, peripheral tour, grab immediately, please pay attention to the public number: love to order not to order

Author: Don Juan

Two articles on Nacos have been developed:

A Preliminary study on Nacos (I) — Single machine mode startup

A preliminary study of Nacos (ii) — service registration and discovery using Nacos by SpringCloud

Dubbo can also easily switch Nacos if you want to use Dubbo. Dubbo has merged with Nacos as a registry, so you can directly test the examples provided by Dubbo: Dubbo – Registrie-nacos, which is relatively simple, will not be said more, today we will try to integrate Dubbo and nacos under SpringBoot.

First, we create a new Module named nacos-Demo-API and define a Dubbo interface to test parameter passing and request and return of List and Map parameters.

public interface HelloProvider {

    String hello(String name);

    List<Map<String, String>> testMapList(Map<String, String> map);

}
Copy the code

In the second step, we created a Dubbo service provider Module under the project named nacos-Demo-Dubbo – Provider and added the dependency in POM.xml.

<dependency>
    <groupId>xyz.aiding.demo</groupId>
    <artifactId>nacos-demo-api</artifactId>
    <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </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> The < version > 2.6.5 < / version > < / dependency > <! -- Alibaba Spring Context extension --> <dependency> <groupId>com.alibaba.spring</groupId> < artifactId > spring - the context - support < / artifactId > < version > 1.0.2 < / version > < / dependency > < the dependency > < the groupId > com. Alibaba. The boot < / groupId > < artifactId > dubbo - spring - the boot - starter < / artifactId > < version > 0.2.1. RELEASE < / version > <exclusions> <exclusion> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> </exclusion> </exclusions> </dependency>Copy the code

And in the application.properties configuration, configure dubo.registry. Address to the specified Nacos registry.

server.port=18082
spring.profiles.active=dev
spring.application.name=nacos-demo-dubbo-provider

##dubbo configDubbo. Registry. Address = nacos: / / 127.0.0.1:8848 dubbo. Application. The name = nacos - demo - dubbo - the provider dubbo.scan.basePackages=xyz.aiding.demo.dubbo.provider dubbo.protocol.name=dubbo dubbo.protocol.port=-1Copy the code

Third, write the SpringBoot boot class, which does not require the @enableDiscoveryClient annotation as in the previous chapter.

@SpringBootApplication public class NacosDubboProviderApplication { public static void main(String[] args) { SpringApplication.run(NacosDubboProviderApplication.class, args); }}Copy the code

And implement dubbo interface.

@Service
public class HelloProviderImpl implements HelloProvider {

    @Value("${spring.profiles.active}")
    private String env;

    @Override
    public String hello(String name) {
        return "hello, " + name;
    }

    @Override
    public List<Map<String, String>> testMapList(Map<String, String> map) {
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        list.add(map);
        Map<String, String> map2 = new HashMap<String, String>();
        map2.put("env", env);
        map2.put("testKey"."testValue");
        list.add(map2);
        returnlist; }}Copy the code

Start the Dubbo provider service, log in to the Nacos console, and you can see that the service is registered.

Fourth, we create a Dubbo service consumer Module under the project, named nacos-Demo-dubbo – Consumer, and add the same dependencies as nacos-Demo-Dubbo – Provider to pom.xml. In the application.properties configuration, dubo.registry.address is configured to the specified Nacos registry, similar to provider.

server.port=18083
spring.profiles.active=dev
spring.application.name=nacos-demo-dubbo-consumer

##dubbo configDubbo. Registry. Address = nacos: / / 127.0.0.1:8848 dubbo. Application. The name = nacos - demo - dubbo - the consumer dubbo.scan.basePackages=xyz.aiding.demo.dubbo.provider dubbo.protocol.name=dubbo dubbo.protocol.port=-1Copy the code

Step 5: Write the SpringBoot startup class with the @enabledubbo annotation.

@EnableDubbo @SpringBootApplication public class NacosDubboConsumerApplication { public static void main(String[] args) { SpringApplication.run(NacosDubboConsumerApplication.class, args); }}Copy the code

Write another Web Controller that calls the Duboo interface HelloProvider written above. Note that the provider invocation must use the @Reference annotation, which is the dependency injection annotation of the Dubbo remote service, and requires the service provider and consumer to agree on interface, version, and group information.

@RestController
public class HelloController {

    @Reference
    HelloProvider helloProvider;

    @GetMapping(value = "/api/hello")
    @ResponseBody
    public String index(@RequestParam("name") String name){
        return helloProvider.hello(name);
    }

    @GetMapping(value = "/api/testMapList")
    @ResponseBody
    public List<Map<String, String>> testMapList(){
        Map<String, String> map = new HashMap<String, String>();
        map.put("hello"."nacos-dubbo");
        returnhelloProvider.testMapList(map); }}Copy the code

Start the Dubbo consumer service, log in to the Nacos console, and you can see that the service is registered.

Open a browser, visit http://127.0.0.1:18083/api/hello? name=abc

As simple as that, SpringBoot integration of Dubbo and Nacos ends here, and in the next chapter we start experimenting with Nacos as a configuration center.