我们在上一篇:手把手教你Spring Cloud Alibaba教程:nacos安装 接下来我们来实现下基本的服务注册和发现
我们需要确认spring Cloud Alibaba version对应的nacso version 主要如下
版本说明 · alibaba/spring-cloud-alibaba Wiki
Spring Cloud Alibaba Version | Sentinel Version | Nacos Version | RocketMQ Version | Dubbo Version | Seata Version |
---|---|---|---|---|---|
2.2.9.RELEASE |
1.8.5 |
2.1.0 |
4.9.4 |
~ |
1.5.2 |
2021.0.4.0 |
1.8.5 |
2.0.4 |
4.9.4 |
~ |
1.5.2 |
2.2.8.RELEASE |
1.8.4 |
2.1.0 |
4.9.3 |
~ |
1.5.1 |
2021.0.1.0 |
1.8.3 |
1.4.2 |
4.9.2 |
~ |
1.4.2 |
2.2.7.RELEASE |
1.8.1 |
2.0.3 |
4.6.1 |
2.7.13 |
1.3.0 |
2.2.6.RELEASE |
1.8.1 |
1.4.2 |
4.4.0 |
2.7.8 |
1.3.0 |
2021.1 or 2.2.5.RELEASE or 2.1.4.RELEASE or 2.0.4.RELEASE |
1.8.0 |
1.4.1 |
4.4.0 |
2.7.8 |
1.3.0 |
2.2.3.RELEASE or 2.1.3.RELEASE or 2.0.3.RELEASE |
1.8.0 |
1.3.3 |
4.4.0 |
2.7.8 |
1.3.0 |
2.2.1.RELEASE or 2.1.2.RELEASE or 2.0.2.RELEASE |
1.7.1 |
1.2.1 |
4.4.0 |
2.7.6 |
1.2.0 |
2.2.0.RELEASE |
1.7.1 |
1.1.4 |
4.4.0 |
2.7.4.1 |
1.0.0 |
2.1.1.RELEASE or 2.0.1.RELEASE or 1.5.1.RELEASE |
1.7.0 |
1.1.4 |
4.4.0 |
2.7.3 |
0.9.0 |
2.1.0.RELEASE or 2.0.0.RELEASE or 1.5.0.RELEASE |
1.6.3 |
1.1.1 |
4.4.0 |
2.7.3 |
0.7.1 |
还要知道springBoot对应的版本
springboot和springcloud版本对应关系:Spring Cloud 或者 官网版本使用对照地址
我们目前使用的是
spring Boot 2.3.12.RELEASE
springCloud Hoxton.SR12
springCloud Alibaba 2.2.9.RELEASE
nacos 2.1.1
确定好版本后,接下来就搭建环境吧。
创建注册服务 alibaba-nacos-discovery-server
pom文件内容如下
4.0.0
org.study
alibaba-nacos-discovery-server
1.0
org.springframework.boot
spring-boot-starter-parent
2.3.12.RELEASE
UTF-8
UTF-8
1.8
Hoxton.SR12
2.2.9.RELEASE
org.springframework.boot
spring-boot-starter-web
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
org.projectlombok
lombok
1.18.2
true
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
com.alibaba.cloud
spring-cloud-alibaba-dependencies
${spring-cloud-alibaba.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @author za
* @create 2021/12/14 13:35
*/
@EnableDiscoveryClient
@SpringBootApplication
public class ServerApplication {
public static void main(String[] args) {
SpringApplication.run(ServerApplication.class);
}
@RestController
@Slf4j
static class TestController {
@GetMapping("/hello")
public String hello(@RequestParam(value = "name") String name) {
log.info("server name:"+name);
return "hello:" + name;
}
}
}
spring.application.name=alibaba-nacos-discovery-server
server.port=8001
####nacos部署的服务
spring.cloud.nacos.discovery.server-addr=nacos的ip地址:nacos端口port
创建注册服务 albaba-nacos-discovery-client-common
pom文件内容如下
4.0.0
org.study
albaba-nacos-discovery-client-common
1.0
org.springframework.boot
spring-boot-starter-parent
2.3.12.RELEASE
UTF-8
UTF-8
1.8
Hoxton.SR12
2.2.9.RELEASE
org.springframework.boot
spring-boot-starter-web
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
org.springframework.cloud
spring-cloud-starter-openfeign
org.projectlombok
lombok
1.18.2
true
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
com.alibaba.cloud
spring-cloud-alibaba-dependencies
${spring-cloud-alibaba.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
package com.zhm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
/**
* @author za
* @create 2021/12/14 13:44
*/
@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class);
}
@RestController
@Slf4j
static class TestController {
@Resource
LoadBalancerClient loadBalancerClient;
@Resource
Client client;
@GetMapping("/test")
public String test(@RequestParam(value = "name") String name) {
ServiceInstance serviceInstance = loadBalancerClient.choose("alibaba-nacos-discovery-server");
String url = serviceInstance.getUri() + "/hello?name=" + name;
log.info("client name:" + name + ":url===>" + url);
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject(url, String.class);
return "restTemplate : " + url + ", return : " + result;
}
@GetMapping(value = "feignTest")
public String feignTest(@RequestParam(value = "name") String name) {
String result = client.hello(name);
return "feign :return : " + result;
}
}
@FeignClient("alibaba-nacos-discovery-server")
interface Client {
@GetMapping("/hello")
String hello(@RequestParam(name = "name") String name);
}
}
spring.application.name=alibaba-nacos-discovery-server
server.port=9000
####nacos部署的服务
spring.cloud.nacos.discovery.server-addr=nacos的ip地址:nacos端口port
启动server和client服务。我们在nacos服务中心就能发现服务已经出现了。
最后来验证下。
一个通过LoadBalancerClient
接口在获取服务实例:
http://localhost:9000/test?name=abc
另外一个通过springcloud的feign来获取服务实例
http://localhost:9000/feignTest?name=abc
这样就实现了springcloudalibaba集成nacos注册中心 简单的注册服务和服务发现的应用了。