前言
上篇咱们讲到如何应用 k8s 搭建 nacos,这篇就来讲讲微服务如何通过搭建好的 nacos 服务注册和配置。
操作
A 服务、B 服务,A 应用 Feign 调用 B 服务外面办法。
A、B 服务引入相干依赖包,
- springcloud 版本:2020.0.0
- springboot 版本:2.4.2
-
alibaba 版本:2021.1
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.2</version> </parent> <properties> <java.version>1.8</java.version> <spring-boot.version>2.4.2</spring-boot.version> <spring-cloud.version>2020.0.0</spring-cloud.version> <spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- spring cloud 依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.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.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> </dependency> </dependencies>
A、B 服务注册和配置到 Nacos 服务的 bootstrap.yml 文件:
spring: application: name: a-service # b-service profiles: active: test cloud: nacos: #注册 A、B 服务到 Nacos discovery: server-addr: http://nacos-headless.default.svc.cluster.local:8848 namespace: xxx-xxx-xxx-xxx service: a-service # b-service #配置 A、B 服务到 Nacos config: server-addr: http://nacos-headless.default.svc.cluster.local:8848 file-extension: yaml prefix: a-service # b-service namespace: xxx-xxx-xxx-xxx server: port: a-port # b-port
能够间接在 Nacos 下面创立 A、B 服务的配置文件:
B 服务办法:
@SpringBootApplication
@EnableDiscoveryClient
@RestController
@RefreshScope
public class BServiceApplication {public static void main(String[] args) {SpringApplication.run(BServiceApplication.class, args);
}
@Value("${server.port}")
String port;
@GetMapping("/hi")
public String hi(@RequestParam(value = "name", defaultValue = "forezp",required = false) String name) {return "hello" + name + ", i'm provider ,my port:" + port;}
}
A 服务想要调用 B 服务办法,首先创立 BFeignClient 类:
@FeignClient("b-service")
public interface BFeignClient {@GetMapping("/hi")
String hi(@RequestParam(value = "name", defaultValue = "forezp", required = false) String name);
}
接着,创立调用的办法:
@SpringBootApplication
@RestController
@EnableDiscoveryClient
@EnableFeignClients
@RefreshScope
public class AServiceApplication {public static void main(String[] args) {SpringApplication.run(AServiceApplication.class, args);
}
@Autowired
BFeignClient bFeignClient;
@GetMapping("/hi-feign")
public String hiFeign(){return bFeignClient.hi("feign");
}
}
运行起来,看看两个服务是否在 Nacos 注册胜利,如图所示:
注册胜利之后,咱们就能够测试 A 服务调用 B 服务是否胜利:
总结
1、因为咱们应用的是 service headless,所以咱们要应用 dns 查找 nacos,默认是你的 service name 加上固定写法:default.svc.cluster.local,我的 nacos service name 是 nacos-headless,即:nacos-headless.default.svc.cluster.local
2、因为 nacos 应用的是最新版本,所以 discovery 要加 service 属性,否则报错
discovery:
server-addr: http://nacos-headless.default.svc.cluster.local:8848
namespace: xxx-xxx-xxx-xxx
service: a-service # b-service
3、因为应用的是 springcloud:2020.0 版本,所以 pom 引入 bootstrap 包,否则 bootstrap.yml 不起作用
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
援用
SpringCloud 2020 版本教程 1:应用 nacos 作为注册核心和配置核心
k8s 集群外部怎么通过 dns 域名使得业务零碎注册到 nacos 集群阐明 - 方法论,其余通过域名拜访服务一样的
k8s 部署 nacos 3 各节点 ….N 各节点均可 集群
Kubernetes Nacos