Nacos服务配置与注册,openFeign服务调用

1.Nacos下载运行

  1.1 Github地址 Tags · alibaba/nacos · GitHub

  目前最新版本是2.2.3,这里下载2.2.2 Release 2.2.2 (Apr 11, 2023) · alibaba/nacos · GitHub

  Windows下载zip包,Linux下载gz包。

  1.2 解压nacos-server-2.2.2.zip包,然后进入到bin目录,终端运行命令 startup.cmd -m standalone,即可启动服务。

  注意:JAVA_HOME需要先配置好,startup.cmd脚本里会用到

  1.3 在浏览器访问http://localhost:8848/nacos,就可以打开Nacos管理界面了,不再需要用户名和密码。默认端口是8848,如果要修改端口或其它配置,可以修改conf目录(bin目录同级)下的application.properties。

2. Springboot(生产者)

  2.1 生成2.7版本Springboot Spring Initializr

  2.2 修改pom文件,主要是增加三个依赖,版本号是2021.0.5

<!-- 微服务 nacos config依赖 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- 微服务 nacos discovery依赖 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- 微服务 bootstrap依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

<?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.7.12</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>11</java.version>
		<spring-cloud-dependencies.version>2021.0.5</spring-cloud-dependencies.version>
		<spring-cloud-alibaba-dependencies.version>2021.0.5.0</spring-cloud-alibaba-dependencies.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-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<!-- 微服务 nacos config依赖 -->
		<dependency>
			<groupId>com.alibaba.cloud</groupId>
			<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
		</dependency>
		<!-- 微服务 nacos discovery依赖 -->
		<dependency>
			<groupId>com.alibaba.cloud</groupId>
			<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
		</dependency>
		<!-- 微服务 bootstrap依赖  -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-bootstrap</artifactId>
		</dependency>
	</dependencies>
<dependencyManagement>
	<dependencies>
		<!-- spring cloud依赖 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-dependencies</artifactId>
			<version>${spring-cloud-dependencies.version}</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
		<!-- spring cloud alibaba依赖 -->
		<dependency>
			<groupId>com.alibaba.cloud</groupId>
			<artifactId>spring-cloud-alibaba-dependencies</artifactId>
			<version>${spring-cloud-alibaba-dependencies.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>
				<configuration>
					<excludes>
						<exclude>
							<groupId>org.projectlombok</groupId>
							<artifactId>lombok</artifactId>
						</exclude>
					</excludes>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

  2.3 在Nacos页面,新建新的命名空间test,自动生成命名空间ID

  2.4 配置bootstrap.properties,bootstrap的优先级大于application

spring.application.name=mytest2
spring.cloud.nacos.config.server-addr=localhost:8848
spring.cloud.nacos.config.namespace=80d30c50-4f49-411c-8157-5bc514d7b1b0
spring.cloud.nacos.config.file-extension=properties

   注意:nacos.config的相关配置需放在这里,否则后面运行时,控制器类因为没有读到Nacos上配置而报错Error creating bean。application.name必须有,namespace对应2.3里test的命名空间ID,file-extension也可以是yaml。

  2.5 配置application.properties

server.port=8088
spring.cloud.nacos.discovery.server-addr=localhost:8848
spring.cloud.nacos.discovery.namespace=80d30c50-4f49-411c-8157-5bc514d7b1b0
spring.cloud.nacos.discovery.register-enabled=true

  nacos.discovery的配置也可以放在bootstrap.properties

  2.6 开启服务注册发现加注解@EnableDiscoveryClient

@SpringBootApplication
@EnableDiscoveryClient
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}

  2.7 增加生产者控制器类

package com.example.demo.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope // Spring Cloud原生注解 开启自动更新
@RequestMapping(value = "/producer")
@Slf4j
public class ProducerController {

    @Value("${config.info}")
    private String name;

    @GetMapping("/getConfig")
    public String getConfig() {
        log.info("getConfig>>>>>>>>>>>");
        return "getConfig>>>>>>>>>>>>" + "发现:" + ">>>服务名称:" + name;
    }

}

  2.8 在Nacos页面,创建新的配置mytest2.properties

  Data ID的格式:${prefix}-${spring.profiles.active}.${file-extension}

  prefix 默认为spring.application.name的值,也可以通过spring.cloud.nacos.config.prefix来配置。

spring.profiles.active为当前环境对应的profile,若为空,则格式变为${prefix}.${file-extension}。

file-extension 通过spring.cloud.nacos.config.file-extension来配置。

   本例我们没有配spring.profiles.active,所以Data ID名称应为mytest2.properties。

 创建后可以在配置列表查看和编辑。

 2.9 启动Springboot服务,访问 http://localhost:8088/producer/getConfig, 返回结果如下

getConfig>>>>>>>>>>>>发现:>>>服务名称:我的服务

在Nacos页面,修改mytest2.properties里的config.info=我的新服务,保存发布后再次访问http://localhost:8088/producer/getConfig,返回结果如下

getConfig>>>>>>>>>>>>发现:>>>服务名称:我的新服务

由此借助Nacos实现不重启Springboot服务,而读取到新的配置信息。

 2.10 在Nacos页面,服务列表里可以找到服务mytest2

3. Springboot(消费者)

  3.1 Copy一份2.1生成的Springboot初始代码,做消费端

  3.2 修改pom文件,主要是服务发现,Openfeign和LoadBalancer三个依赖

<!-- 微服务 nacos discovery依赖 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--在消费者加Openfeign依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--Openfeign依赖的LoadBalancer -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>

<?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.7.12</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>11</java.version>
		<spring-cloud-dependencies.version>2021.0.5</spring-cloud-dependencies.version>
		<spring-cloud-alibaba-dependencies.version>2021.0.5.0</spring-cloud-alibaba-dependencies.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-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<!-- 微服务 nacos discovery依赖 -->
		<dependency>
			<groupId>com.alibaba.cloud</groupId>
			<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
		</dependency>
		<!--
           在消费者加Openfeign依赖
       -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-openfeign</artifactId>
		</dependency>
		<!-- LoadBalancer -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-loadbalancer</artifactId>
		</dependency>
	</dependencies>
<dependencyManagement>
	<dependencies>
		<!-- spring cloud依赖 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-dependencies</artifactId>
			<version>${spring-cloud-dependencies.version}</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
		<!-- spring cloud alibaba依赖 -->
		<dependency>
			<groupId>com.alibaba.cloud</groupId>
			<artifactId>spring-cloud-alibaba-dependencies</artifactId>
			<version>${spring-cloud-alibaba-dependencies.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>
				<configuration>
					<excludes>
						<exclude>
							<groupId>org.projectlombok</groupId>
							<artifactId>lombok</artifactId>
						</exclude>
					</excludes>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

  3.3 配置application.properties

server.port=8089
spring.application.name=consumer
spring.cloud.nacos.discovery.server-addr=localhost:8848
spring.cloud.nacos.discovery.namespace=80d30c50-4f49-411c-8157-5bc514d7b1b0
spring.cloud.nacos.discovery.register-enabled=true
注意:服务端口不要和生产者一样。

  3.4 开启服务注册发现@EnableDiscoveryClient,和openFeign功能@EnableFeignClients

@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}

  3.5 在生产者ProducerController里加个API接口,全路径为/producer/openfeign/{msg}

   @GetMapping("/openfeign/{msg}")
    public String testMsg(@PathVariable("msg") String msg) {
        return "生产者收到消息=" + msg;
    }

  3.6 在消费者里增加OpenFeignService接口,以调用生产者的接口

package com.example.demo;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient(value = "mytest2")//生产者注册的服务名
public interface OpenFeignService {
    @GetMapping("/producer/openfeign/{msg}")//对应生产者API里的全路径
    String get(@PathVariable("msg")String msg);

}

  3.7 增加消费者控制器类

package com.example.demo;

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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/consumer")
public class ConsumerController {
    @Autowired
    OpenFeignService openFeignService;

    @GetMapping("/test/{msg}")
    public String invokeProvider(@PathVariable String msg) {
        return "消费者在调用API," + openFeignService.get(msg);
    }
}

  3.8 启动生产者和消费者Springboot服务,在Nacos服务列表可以看到这两个服务已注册。

  访问消费者API http://localhost:8089/consumer/test/hello,返回结果如下

消费者在调用API,生产者收到消息=hello

  3.9 通过向Nacos服务注册,由Nacos管理所有服务,实现了服务之间解耦。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值