上篇【nacos基本架构和安装】已实现安装和启动Nacos微服务注册中心,本篇主要是服务提供者向Nacos注册服务,服务消费者调用已注册的服务。即:服务注册中心(Nacos)、服务提供者(Provider)、服务消费者(Consumer)。如果仍在使用Eureka,可以参考之前的文章。
1、服务注册中心(Nacos)
进入nacos的bin目录,启动服务
startup.cmd -m standalone
2、服务提供者(Provider)
创建Spring Boot工程,以此代码为基础,主要代码调整如下:
pom.xml
<?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 https://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.4.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.test</groupId>
<artifactId>nacos_provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>nacos_provider</name>
<description>nacos provider project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-discovery-spring-boot-starter</artifactId>
<version>0.2.4</version>
</dependency>
</dependencies>
.....
</project>
NacosProviderApplication
@SpringBootApplication
public class NacosProviderApplication {
public static void main(String[] args) {
SpringApplication.run(NacosProviderApplication.class, args);
}
}
HelloController
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.naming.NamingService;
@RestController
public class HelloController implements CommandLineRunner {
@NacosInjected
private NamingService namingService;
@Value("${spring.application.name}")
private String applicationName;
@Value("${server.port}")
private Integer serverPort;
@RequestMapping("/hello")
public String hello(){
return String.format("Hello, the messge come from %s, port is %s!", applicationName, serverPort);
}
@Override
public void run(String... args) throws Exception {
// 通过Naming服务注册实例到注册中心
namingService.registerInstance(applicationName, "127.0.0.1", serverPort);
}
}
application.properties
spring.application.name=provide-nacos-service
server.port=9001
nacos.discovery.server-addr=127.0.0.1:8848
运行
注册服务到Nacos
3、服务消费者(Consumer)
同上,创建Spring Boot工程,然后主要调整代码如下:
pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-discovery-spring-boot-starter</artifactId>
<version>0.2.4</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
NacosCustomerApplication
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class NacosCustomerApplication{
public static void main(String[] args) {
SpringApplication.run(NacosCustomerApplication.class, args);
}
}
application.properties
spring.application.name=consume-nacos-service
server.port=9002
nacos.discovery.server-addr=127.0.0.1:8848
Feign接口
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
@FeignClient("provide-nacos-service")
public interface HelloFeignClient {
@RequestMapping("/hello")
String helloWorld();
}
ConsumerController
@RestController
public class ConsumerController {
@Autowired
HelloFeignClient hfc;
@RequestMapping("/hello-customer")
public String hello() {
String result = hfc.helloWorld();
return "this is FeignResult:" + result;
}
}
运行