spring-cloud 基于nacos实现服务注册与服务调用,从理论到实践--小白篇

spring-cloud基本概念

一、服务调用

1、订单服务访问会员服务经过

  • 服务注册

    1、订单服务和会员服务都需要配置 服务注册中心的IP和port

    2、订单服务和会员服务启动时告诉服务中心自己的服务名称以及IP和port

    >OrderServer:http://${ip2:port2}/
    >
    >VipServer:http://${ip3:port3}/

    3、订单服务在调用会员服务的代码处需要标明

    • 会员服务名称:VipServer

    • 具体接口的调用uri: 例如 /getUserInfo

  • 服务调用

    1、订单服务通过会员服务名称 (VipServer)从注册中心拿到 会员服务的访问ip和port

    http://${ip3:port3}/

    2、订单服务拼接得到完整的请求url:http://${ip3:port3}/getUerInfo

    3、订单服务通过此url向会员服务发起http请求

    4、至此订单服务远程调用会员服务完成。

2、代码示例

1)、注册中心

注册中心我们选择阿里开源的nacos组件

1、nacos 下载 (我这里用的是1.3.2 win64版本的)

官网:https://nacos.io/zh-cn/docs/quick-start.html

百度网盘:链接:https://pan.baidu.com/s/1R8v39cWPN74VGFQ9ksIZYw 提取码:7o46

2、nacos 安装和启动

1、nacos依赖jdk,jdk配置此处不表

2、直接解压就可以使用(个性化配置自行百度)

3、启动 进入bin目录 cmd命令行执行:

startup.cmd -m standalone

单节点启动即可。

4、浏览器页面访问:

http://localhost:8848/nacos

默认登录用户名:nacos 密码:nacos

2)、服务注册

2.1、订单服务

项目结构

 

依赖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>
​
<groupId>com.hhf.www</groupId>
<artifactId>OrderServer</artifactId>
<version>1.0-SNAPSHOT</version>
​
<dependencies>
​
   <!--  spring boot web服务基本依赖-->
   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
       <version>2.0.4.RELEASE</version>
   </dependency>
​
   <!-- nacos 作为服务注册中心的必要依赖-->
   <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
       <version>0.2.2.RELEASE</version>
   <!--   解决 java.lang.ClassNotFoundException: org.apache.http.impl.client.HttpClientBuilder jar包冲突问题-->
       <exclusions>
        <exclusion>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </exclusion>
        </exclusions>
   </dependency>
​
</dependencies>
​
</project>

配置文件bootstrap.yml

spring:
application:
name: OrderServer  # 服务名称
cloud:
nacos:
 discovery:
   server-addr: 192.168.24.39:8848  # 服务注册中心ip和port
​
​
server:
port: 6666  # 当前web服务 port

启动项OrderServerApp

package com.hhf.www;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication // spring boot 启动项注解
@EnableDiscoveryClient  // 将服务注册到服务注册中心的注解
public class OrderServerApp {
public static void main(String[] args) {
   SpringApplication.run(OrderServerApp.class);
}
}

2.2、会员服务

项目结构

 

依赖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>
​
<groupId>com.hhf.www</groupId>
<artifactId>VipServer</artifactId>
<version>1.0-SNAPSHOT</version>
​
<dependencies>
​
   <!--  spring boot web服务基本依赖-->
   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
       <version>2.0.4.RELEASE</version>
   </dependency>
​
   <!-- nacos 作为服务注册中心的必要依赖-->
   <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
       <version>0.2.2.RELEASE</version>
   <!--   解决 java.lang.ClassNotFoundException: org.apache.http.impl.client.HttpClientBuilder jar包冲突问题-->
       <exclusions>
        <exclusion>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </exclusion>
        </exclusions>
   </dependency>
​
</dependencies>
​
</project>

配置文件bootstrap.yml

spring:
application:
name: VipServer  # 服务名称
cloud:
nacos:
 discovery:
   server-addr: 192.168.24.39:8848  # 服务注册中心ip和port
​
​
server:
port: 9999  # 当前web服务 port

启动项OrderServerApp

package com.hhf.ww;
​
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
​
@SpringBootApplication // spring boot 启动项注解
@EnableDiscoveryClient  // 将服务注册到服务注册中心的注解
public class VipServerApp {
public static void main(String[] args) {
   SpringApplication.run(VipServerApp.class);
}
}

2.3、服务注册中心信息

启动2个服务后可以看到,2个服务都已经注册到了nacos服务注册中心了

点击查看服务详情

 

可以看到服务的ip和port以及是否健康(在线)等消息

至此服务注册已经完成。

3)、服务调用

3.1、服务提供方

(会员服务)添加接口--VipServer

代码

@RestController
public class UserController {
​
@RequestMapping("/getUserInfo")
public Map<String,String> getUserInfo(){
   Map<String,String>  userInfo = new HashMap<String, String>();
   userInfo.put("name","张三");
   userInfo.put("sex","男");
   userInfo.put("age","18");
   return userInfo;
}
}

接口测试

接口正常

3.2、服务调用方

(订单服务)添加接口--OrderServer

代码

@RestController
public class OrderController {

@Autowired
RestTemplate  restTemplate;  // 由于下面将 RestTemplate 对象注入到了 spring 容器中所以此处可以直接引用

@Bean  // 将创建的对象注入到 spring 容器中去
@LoadBalanced  // 负载均衡用
public RestTemplate getRestTemplate(){
   return  new RestTemplate();
}

@RequestMapping("/getUserInfo")
public Map<String,String> getUserInfo(){
   // 发起get请求
   Map<String,String>  userInfo =   restTemplate.getForObject("http://VipServer/getUserInfo",Map.class);
   return userInfo;
}

}

可以看到此处http请求调用 会员服务(VipServer)并没有写会员服务明确的ip和port

只写了会员服务注册到nacos(注册中心)时注册的服务名称:VipServer

3.3、服务调用

通过OrderServer 远程调用 VipServer的服务

端口6666是订单服务的

但是通过订单服务调用到了会员服务的接口了,并且顺利得到响应了。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值