aiyali

导航

Spring Cloud Alibaba-----Nacos 注册中心实践

官网地址: https://spring-cloud-alibaba-group.github.io/github-pages/greenwich/spring-cloud-alibaba.html#_spring_cloud_alibaba_nacos_discovery

 

 

如图新建3个项目,80为服务消费者,8001和8002 为服务提供者,其他module可忽律

1.父pom  添加引用

<dependencyManagement>
<dependencies>
<!-- spring boot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>

<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>

...
...
  </dependencies>
</dependencyManagement>

2.8001和8002 pom  添加引用
<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<!-- alibaba nacos-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>


</dependencies>

3. 8001和8002 application.yml 配置
server:
port: 8002
spring:
application:
name: nacos-provider
cloud:
nacos:
discovery:
#nacos 注册中心地址
server-addr: 127.0.0.1:8848


management:
endpoints:
web:
exposure:
#暴露所有端点
include: "*"

4.启动类要加上  @EnableDiscoveryClient

5. 8001和8002 controller
@RestController
public class ProviderController {

@Value("${server.port}")
private String port;

@GetMapping("/provider/order/{id}")
public String getOrderNum(@PathVariable("id") String id){
String uuid= UUID.randomUUID().toString();
return "RequestId:"+id+"-----服务器:"+port+"--订单号:"+uuid;
}
}

6. 消费者80 的pom 添加依赖和yml
<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<!-- alibaba nacos-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

<!--openfegin -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>

server:
port: 80
spring:
application:
name: nacos-consume
cloud:
nacos:
discovery:
#nacos 注册中心地址
server-addr: 127.0.0.1:8848


management:
endpoints:
web:
exposure:
#暴露所有端点
include: "*"


7. 消费者80采用 OpenFegin,调用provider
@Component
@FeignClient(value = "nacos-provider")
public interface ProviderService {

@GetMapping("/provider/order/{id}")
String getOrderNum(@PathVariable("id") String id);
}

8.消费者controller
@RestController
public class ConsumeController {

@Autowired
private ProviderService providerService;

@GetMapping("/consume/order/{id}")
public String getOrderNum(@PathVariable("id") String id){
return providerService.getOrderNum(id);
}
}

启动测试

 

 

由于我本地80端口冲突,临时换成8999 

 

 

 

 
 



 

 

posted on 2020-05-13 18:16  aiyali  阅读(138)  评论(0编辑  收藏  举报