Nacos 注册中心
Nacos ,spring cloud alibaba nacos 中的部分呢
推荐使用doker 安装
这里使用 聚会工程实现,来快速上手
项目搭建
pom 文件
父工程
<groupId>org.example</groupId>
<artifactId>springcloud-nacos</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<description>nacos作为注册中心</description>
<modules>
<module>my-goods</module>
<module>my-order</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<!-- <version>2.2.3.RELEASE</version>-->
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>
<com-alibaba-cloud.version>2.2.0.RELEASE</com-alibaba-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${com-alibaba-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
消费者
<modelVersion>4.0.0</modelVersion>
<artifactId>my-goods</artifactId>
<name>my-goods</name>
<description>商品</description>
<parent>
<groupId>org.example</groupId>
<artifactId>springcloud-nacos</artifactId>
<version>1.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
生产者
<modelVersion>4.0.0</modelVersion>
<artifactId>my-order</artifactId>
<name>my-order</name>
<description>订单服务</description>
<parent>
<groupId>org.example</groupId>
<artifactId>springcloud-nacos</artifactId>
<version>1.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
goods 代码
提供调用服务,简单的字符串传输
server:
port: 8081
spring:
application:
name: my-goods
cloud:
nacos:
server-addr: .....:8848
@RestController
public class GoodsController {
@GetMapping("goods/{message}")
public String greet(@PathVariable String message){
return "goods 被访问 :"+ message;
}
}
order 代码
服务调用方
server:
port: 8080
spring:
application:
name: my-order
cloud:
nacos:
server-addr: ...:8848
@RestController
public class HelloController {
/*
spring-cloud-alibaba-nacos-discovery 内置了Ribbon,可以直接注入 LoadBalancerClient
*/
@Autowired
private LoadBalancerClient loadBalancerClient;
// 在配置类中注入,或者自己new,配置类比较简单,就不贴出来了
@Autowired
private RestTemplate restTemplate;
@GetMapping("hello")
public String hello(){
return "测试";
}
@GetMapping("hello/{message}")
public String hello(@PathVariable String message) {
//获取注册中心的 服务端信息
ServiceInstance serviceInstance = loadBalancerClient.choose("my-goods");
String path = String.format("http://%s:%s/goods/%s", serviceInstance.getHost(), serviceInstance.getPort(), message);
//发起请求
String result = restTemplate.getForObject(path, String.class);
return String.format("%s from %s %s", result, serviceInstance.getHost(), serviceInstance.getPort());
}
}
测试效果
Nacos默认的 负载均衡的策略是轮询,这里做个测试
查看是否注册成功