1.服务调用
1.1 机器直连
服务A调用服务B,只需要知道服务B的ip、端口、就可以发起调用, 并获取响应结果
优点:实现简单
缺点:
- 服务提供方存在多台机器,需要自己实现负载均衡算法
- 服务提供方ip发生变更,调用方需要手动维护
1.2 ngxin反向代理
服务A调用服务B,只需要连接nginx服务即可实现负载均衡调用
优点:无需要自己实现负载均衡算法
缺点:无法感知服务的上下线
1.3 注册中心
服务B启动的时候将自身应用名称、ip、端口号注册到nacos中,服务A进行远程调用时根据应用名称从nacos中拉取对应ip、端口号,根据实现的负载均衡算法进行服务调用
优点:可以动态感知服务上下线
缺点:需要引入第三方中间件、需要实现负载均衡算法
2. nacos安装
2.1 搜索镜像
docker search nacos
2.2 拉取并运行镜像
docker run --name nacos -e MODE=standalone -p 8848:8848 -d nacos/nacos-server
2.3 查看运行中的镜像
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bc89dc58e49f nacos/nacos-server "bin/docker-startup.…" 3 hours ago Up 3 hours 0.0.0.0:8848->8848/tcp, :::8848->8848/tcp nacos
可以看到nacos容器已经正常运行
2.4 控制台访问
浏览器中输入http://localhost:8848/nacos/#/login,使用用户名:nacos
、密码:nacos
登录即可
3.实战
3.1 服务提供方
3.1.1 添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 注意此处的groupId一定要是com.alibaba.cloud -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
3.1.2 创建启动程序
@SpringBootApplication
@EnableDiscoveryClient
public class NacosProviderApplication {
public static void main(String[] args) {
SpringApplication.run(NacosProviderApplication.class, args);
}
}
启动类上需要添加
@EnableDiscoveryClient
注解
3.1.3 创建服务
@RestController
public class NacosProviderController {
@Value("${server.port}")
private Integer port;
@GetMapping("/hello")
public String hello(@RequestParam("name") String name) {
return "hello " + (StringUtils.isNotBlank(name) ? name : "default") + ",response from " + port;
}
}
3.1.4 声明nacos服务信息
spring:
application:
name: boot-cloud-nacos-provider
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
server:
port: 8081
spring.application.name必须唯一,很重要
3.1.5 多环境配置
---
server:
port: 8081
spring:
config:
activate:
on-profile: boot-cloud-nacos-provider1
---
server:
port: 8082
spring:
config:
activate:
on-profile: boot-cloud-nacos-provider2
---
server:
port: 8083
spring:
config:
activate:
on-profile: boot-cloud-nacos-provider3
配置3个profile,用于演示客户端调用
3.2 服务调用方
3.2.1 添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
一定要移除
spring-cloud-starter-alibaba-nacos-discovery
中的spring-cloud-starter-netflix-ribbon
否则会和spring-cloud-starter-loadbalancer
冲突
3.2.2 创建启动程序
@SpringBootApplication
@EnableDiscoveryClient
public class NacosConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(NacosConsumerApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
同样需要添加
@EnableDiscoveryClient
注解,同时声明一个具有负载均衡能力的RestTemplate
3.2.3 创建服务
@RestController
@RequestMapping("/rest")
public class RestTemplateController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/hello")
public String hello(@RequestParam(name = "name") String name) {
return restTemplate.getForObject("http://boot-cloud-nacos-provider/hello?name=" + name, String.class);
}
}
3.2.4 声明nacos服务信息
server:
port: 9090
spring:
application:
name: boot-cloud-nacos-consumer
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
3.3 启动服务
3.3.1 服务提供方
创建3
个SpringBoot
应用:NacosProviderApplication8081、NacosProviderApplication8082、NacosProviderApplication8083
对应Program arguments
:--spring.profiles.active=boot-cloud-nacos-provider1
、--spring.profiles.active=boot-cloud-nacos-provider2
、--spring.profiles.active=boot-cloud-nacos-provider2
依次启动3
个SpringBoot
应用
进入nacos
,点击ServiceManagement
中的Service List
3.3.2 服务调用方
直接启动服务调用方
3.4 调用服务
在浏览器中输入:http://localhost:9090/rest/hello?name=nacos,疯狂点击刷新按钮,记住一定是要疯狂点击,就可以看到如下效果