Spring boot集成Nacos服务注册中心及发现与消费

上篇【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
application1 run
provider service

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;
	}

}

运行

run consume service

find serveice

4、测试 feign 调用

result

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值