关于Nacos之前写了两篇文章关于SpringBoot对它的使用,感兴趣可以查看一下。
《SpringBoot使用Nacos配置中心》
《SpringBoot使用Nacos服务发现》
在SpringBoot使用的时候,需要自行去向Nacos服务注册自己的服务,Nacos也提供了SpringCloud服务发现的依赖,本文结合spring-cloud-starter-alibaba-nacos-discovery进行使用介绍。
本文使用版本为:
两个服务,如下:
原理很简单,浏览器访问消费者,消费者调用服务提供者。
创建一个项目,项目中加入SpringCloud-Nacos依赖,完整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 http://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.0.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.dalaoyang</groupId> <artifactId>springcloud_nacos_discovery_provider</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springcloud_nacos_discory_provider</name> <description>springcloud_nacos_discovery_provider</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Finchley.SR2</spring-cloud.version> </properties> <dependencies> <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>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>0.2.1.RELEASE</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> 复制代码
在启动类加入@SpringBootApplication注解,完整启动类代码如下所示。
package com.dalaoyang; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class SpringcloudNacosDiscoveryProviderApplication { public static void main(String[] args) { SpringApplication.run(SpringcloudNacosDiscoveryProviderApplication.class, args); } } 复制代码
配置文件中配置Nacos服务地址,当前服务名,这里需要注意一点,使用Ribbon负载均衡的时候服务名中不能使用下划线,不然会找不到服务。
配置文件如下所示。
server.port=10000 spring.application.name=springcloud-nacos-discovery-provider spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 复制代码
创建一个Controller,提供一个方法进行测试,如下所示。
package com.dalaoyang.controller; import org.springframework.web.bind.annotation.*; /** * @author yangyang * @date 2019/2/4 */ @RestController public class TestController { @GetMapping("/test/{string}") public String test(@PathVariable String string) { return "Hello Nacos :" + string; } } 复制代码
与服务提供者类似,在服务消费者加入SpringCloud-Nacos依赖和OpenFeign依赖,完整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 http://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.0.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.dalaoyang</groupId> <artifactId>springcloud_nacos_discovery_consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springcloud_nacos_discovery_consumer</name> <description>springcloud_nacos_discovery_consumer</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Finchley.SR2</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>2.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>0.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> 复制代码
在启动类加入注解@EnableDiscoveryClient并且开启负载均衡,如下所示。
package com.dalaoyang; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableDiscoveryClient public class SpringcloudNacosDiscoveryConsumerApplication { public static void main(String[] args) { SpringApplication.run(SpringcloudNacosDiscoveryConsumerApplication.class, args); } @LoadBalanced @Bean public RestTemplate restTemplate() { return new RestTemplate(); } } 复制代码
配置文件与提供者一致,只是服务名不同,如下所示。
server.port=10001 spring.application.name=springcloud-nacos-discovery-consumer spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 复制代码
创建一个Controller进行调用服务,如下所示。
package com.dalaoyang.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; /** * @author yangyang * @date 2019/2/4 */ @RestController public class TestConrtroller { @Autowired private RestTemplate restTemplate; @GetMapping("test/{string}") public String test(@PathVariable String string) { return restTemplate.getForObject("http://springcloud-nacos-discovery-provider/test/" + string, String.class); } } 复制代码