文章目录
基于Nacos实现服务发现
案例说明
本文通过一个简单的示例来演示如何在SpringCloud
生态体系中使用Nacos
来实现服务发现功能。业务流程如下:
服务提供者实现步骤
依赖管理
<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>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
服务配置文件
server.port=7000
spring.application.name=sca-account-service
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
management.endpoints.web.exposure.include=*
创建服务提供者启动类
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class ScaAccountServiceApplication implements CommandLineRunner {
Logger logger = LoggerFactory.getLogger(this.getClass());
public static void main(String[] args) {
SpringApplication.run(ScaAccountServiceApplication.class, args);
}
public void run(String... args) throws Exception {
logger.info("-------------- 账户应用服务提供者-启动成功 --------------");
}
创建服务端控制器
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AccountController {
@GetMapping(value = "/echo/{string}")
public String echo(@PathVariable String string) {
return "您好:" + string;
}
}
以上是服务提供者的主要代码
服务消费者实现步骤
依赖管理
<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>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
服务配置文件
server.port=7001
spring.application.name=sca-account-service
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
management.endpoints.web.exposure.include=*
创建服务消费者启动类
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
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 AccountConsumerApplication implements CommandLineRunner {
Logger logger = LoggerFactory.getLogger(this.getClass());
public static void main(String[] args) {
SpringApplication.run(AccountConsumerApplication.class, args);
}
public void run(String... args) throws Exception {
logger.info("-------------- 账户应用服务消费者-启动成功 --------------");
}
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
创建服务端控制器
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class AccountConsumerController {
@Autowired private RestTemplate restTemplate;
@RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
public String echo(@PathVariable String str) {
return restTemplate.getForObject("http://sca-account-service/echo/" + str, String.class);
}
}
以上是服务消费者的主要代码
测试步骤
分别启动服务提供者和服务消费者,可以在Nacos
控制台看看服务列表,如下图:
浏览器里分别输入以下请求地址并观察页面输出:
服务提供者请求:http://localhost:7000/echo/我是服务提供者
。结果如下图:
服务消费者请求:http://localhost:7001/echo/我是服务消费者
。结果如下图: