添加服务发现
crm 工作负载,有两个pod
添加DNS记录
自定义名称 - 选择命名空间 - 选择工作负载 - 选择crm - 类型选择 Cluster IP - 端口名自定义 - 端口是容器暴露的端口
测试
在 test 命名空间下,运行 busbox ,镜像地址为 sequenceiq/busybox
进入容器内部
运行命令 curl crm-service:8022/test/1,/test/1 是 crm 的一个调试接口
不难发现,访问服务需要加上端口,这里可以优化
服务发现找到 crm-service,点击升级
修改发布服务端口为80
不加端口也可以访问成功
java 调用
现有一个 tms 服务,想调用 crm 的接口
tms项目,pom 文件添加依赖
<dependencies>
...
<!--Feign远程服务调用依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
...
</dependencies>
<dependencyManagement>
<dependencies>
<!--spring-cloud-alibaba依赖,能对nacos进行版本管理-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--springcloud依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR10</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
使用 feign 调用
@FeignClient(name = "crm-service",url = "crm-service")
public interface ITestCrmService {
@GetMapping(value = "/test/{id}")
NjcResponseEntity<TestInfoRespDTO> selectTest(@PathVariable("id") Long id);
}