本文主要是介绍SpringCloud Alibaba微服务实战(二) - Nacos服务注册与restTemplate消费,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
说在前面
基础环境搭建,理论,请看上一篇,在这就不扯理论了,直接上代码。
项目结构
代码实现
第一步:在父pom的项目中引入dependencyManagement
在引入父pom之前咱们先来回顾下dependencyManagement与使用他的原因
什么是dependencyManagement:他可以统一管理项目的版本号,确保应用的各个子项目的依赖和版本一致,当需要变更版本号的时候只需要在父类容器里更新,不需要任何一个子项目的修改;如果某个子项目需要另外一个特殊的版本号时,只需要在自己的模块dependencies中声明一个版本号即可。子类就会使用子类声明的版本号,不继承于父类版本号。
为什么用dependencyManagement:首先在SpringCloudAlibaba的项目中对于版本的依赖要求很严格,所有需要统一来声明管理每隔依赖的版本号。版本之间的关系参考文章:SpringCloud Alibaba 微服务架构版本说明
父类完整的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 http://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.3.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.zsy.springcloud</groupId><artifactId>cloud-alibaba</artifactId><packaging>pom</packaging><version>1.0-SNAPSHOT</version><modules><module>cloud-account</module><module>cloud-consumer</module></modules><properties><java.version>1.8</java.version><spring-cloud-alibaba.version>2.2.3.RELEASE</spring-cloud-alibaba.version><spring-cloud.version>Hoxton.SR8</spring-cloud.version></properties><dependencies><!--Spring Boot整合lombok依赖--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency></dependencies><dependencyManagement><dependencies><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><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>
</project>
第二步:构建服务提供者cloud-account进行服务注册
- 在父项目中创建子module项目名字为cloud-account,在pom中引入nacos服务注册依赖
<!--Spring web--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--SpringCloud Alibaba nacos--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency>
完整服务提供者cloud-account的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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>cloud-alibaba</artifactId><groupId>com.zsy.springcloud</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>cloud-account</artifactId><dependencies><!--Spring web--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--SpringCloud Alibaba nacos--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build><repositories><!--指定阿里云镜像库下载依赖--><repository><id>central</id><name>aliyunmaven</name><url>http://maven.aliyun.com/nexus/content/groups/public/</url></repository></repositories>
</project>
- 创建服务提供者AccountController
package com.zsy.springcloud.controller;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@Log4j2
@RestController
public class AccountController {@RequestMapping(value = "/getAccount", method = RequestMethod.GET)public Map<String,Object> getAccount() {log.info("获取客户信息服务, 服务正常启动");Map<String,Object> account = new HashMap<>();account.put("id","1");account.put("name","诗颖");log.info("id=="+account.get("id")+",name=="+account.get("name"));return account;}
}
- 配置application.yml或者properties文件
server:port: 8081
spring:#服务应用名字application:name: cloud-accountcloud:# 指定nacos控制台地址,配置注册ip:端口,注意即使是80端口也不可能省略nacos:discovery:server-addr: 127.0.0.1:8848
- 配置启动服务的启动类
package com.zsy.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient //开启服务注册
public class AccountApplication {public static void main(String [] args) {SpringApplication.run(AccountApplication.class, args);}
}
- 启动cloud-account服务项目,启动成功,如下图
- 验证服务,浏览器访问http://127.0.0.1:8848/nacos/,登录,点击服务管理,查看服务列表,如下图,服务注册成功。
第三步,构建服务消费者cloud-consumer进行服务调用
1.在父项目中创建子module项目名字为cloud-account,在pom中引入nacos服务注册依赖(同服务者)
<!--Spring web--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--SpringCloud Alibaba nacos--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency>
完整消费者cloud-consumer的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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>cloud-alibaba</artifactId><groupId>com.zsy.springcloud</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>cloud-consumer</artifactId><dependencies><!--Spring web--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--SpringCloud Alibaba nacos--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build><repositories><!--指定阿里云镜像库下载依赖--><repository><id>central</id><name>aliyunmaven</name><url>http://maven.aliyun.com/nexus/content/groups/public/</url></repository></repositories>
</project>
- 创建消费者AccountConsumerController
package com.zsy.springcloud.controller;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
@Log4j2
@RestController
public class AccountConsumerController {@Autowiredprivate RestTemplate restTemplate;@RequestMapping(value = "/getConsumerAccount", method = RequestMethod.GET)public Map<String,Object> getAccount() {Map<String,Object> account = new HashMap<>();log.info("---------消费者开始------------");//调用服务account = restTemplate.getForObject("http://cloud-account/getAccount", HashMap.class);log.info("---------消费者结束--------account{}----", account);return account;}
}
- 配置application.yml或者properties文件
server:port: 8082
spring:#服务应用名字application:name: cloud-consumercloud:# 指定nacos控制台地址,配置注册ip:端口,注意即使是80端口也不可能省略nacos:discovery:server-addr: 127.0.0.1:8848
- 配置消费者启动服务的启动类
package com.zsy.springcloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient //开启服务注册
public class ConsumerApplication {public static void main(String [] args) {SpringApplication.run(ConsumerApplication.class, args);}/*** 创建服务调用的对象* @LoadBalanced 添加此注解后,RestTemplate就具有了ribben客户端负载均衡能力*/@Bean@LoadBalancedpublic RestTemplate restTemplate() {return new RestTemplate();}
}
- 启动cloud-consumer服务项目,启动成功,如下图
6. 验证服务,浏览器访问http://localhost:8082/getConsumerAccount,返回如下图
查看消费者cloud-consumer控制台,如下图
查看服务者cloud-account控制台,如下图
Nacos服务注册与发现搭建完成,完整代码地址:点击下载
码云地址:https://gitee.com/zlzhaoe/cloud-alibaba
这篇关于SpringCloud Alibaba微服务实战(二) - Nacos服务注册与restTemplate消费的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!