0
点赞
收藏
分享

微信扫一扫

SpringCloud集成Nacos进行服务的注册与发现Demo

IT程序员 2022-07-13 阅读 37


文章目录

  • ​​创建服务提供者项目[demo-spring-cloud-nacos-service-provider]​​
  • ​​项目源码地址​​
  • ​​项目结构​​
  • ​​引入jar包​​
  • ​​父类包​​
  • ​​服务发现包和 web项目​​
  • ​​配置文件:application.yml​​
  • ​​Controller服务:NacosServiceProviderController​​
  • ​​启动类:NacosServiceProviderApp​​
  • ​​启动后验证​​
  • ​​运行启动类查看nacos​​
  • ​​访问服务​​
  • ​​创建服务消费者项目[demo-spring-cloud-nacos-service-consumer]​​
  • ​​源码地址​​
  • ​​项目结构​​
  • ​​引入jar包​​
  • ​​父类包​​
  • ​​服务发现包和 web项目​​
  • ​​涉及到客户端负载的开启 @LoadBalanced​​
  • ​​Controller服务:NacosServiceProviderController​​
  • ​​启动类:NacosServiceConsumerApp​​
  • ​​验证​​
  • ​​nacos客户端​​
  • ​​从consumer去调用provider​​

创建服务提供者项目[demo-spring-cloud-nacos-service-provider]

项目源码地址

https://gitee.com/gaoxinfu_admin/demo-spring-cloud/tree/master/demo-spring-cloud-nacos-service-provider

项目结构

SpringCloud集成Nacos进行服务的注册与发现Demo_ide

引入jar包

父类包

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

服务发现包和 web项目

需要提供给外部服务的调用,所以需要引用web包

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

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>

配置文件:application.yml

主要配置端口,项目名称,nacos地址

server:
port: 8079
spring:
application:
name: nacos-service-provider
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848

Controller服务:NacosServiceProviderController

package com.gaoxinfu.demo.spring.cloud;

import org.springframework.web.bind.annotation.*;

/**
* @Description:
* @Author: gaoxinfu
* @Date: 2020-10-19 17:33
*/
@RestController
public class NacosServiceProviderController {

@RequestMapping(value= "/findName/{name}",method = RequestMethod.GET)
public String findName(@PathVariable String name){
return "Nacos Service Provider:欢迎 "+name+" 访问";
}
}

启动类:NacosServiceProviderApp

package com.gaoxinfu.demo.spring.cloud;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class NacosServiceProviderApp
{
public static void main( String[] args )
{
SpringApplication.run(NacosServiceProviderApp.class,args);
}
}

启动后验证

运行启动类查看nacos

SpringCloud集成Nacos进行服务的注册与发现Demo_ide_02

访问服务

​​http://127.0.0.1:8079/findName/gaoxinfu​​

SpringCloud集成Nacos进行服务的注册与发现Demo_spring_03

综上,服务端已经能够正常访问,并注册到nacos上

创建服务消费者项目[demo-spring-cloud-nacos-service-consumer]

源码地址

​​https://gitee.com/gaoxinfu_admin/demo-spring-cloud/tree/master/demo-spring-cloud-nacos-service-consumer​​

项目结构

SpringCloud集成Nacos进行服务的注册与发现Demo_项目结构_04

引入jar包

父类包

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

服务发现包和 web项目

需要提供给外部服务的调用,所以需要引用web包

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

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>

涉及到客户端负载的开启 @LoadBalanced

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
</dependency>

Controller服务:NacosServiceProviderController

package com.gaoxinfu.demo.spring.cloud;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
* @Description:
* @Author: gaoxinfu
* @Date: 2020-10-20 10:17
*/
@RestController
public class NacosServiceConsumerContoller {


@Resource
private RestTemplate restTemplate;

@GetMapping("/findName/{name}")
public String findName(@PathVariable String name){
return restTemplate.getForObject("http://nacos-service-provider/findName/"+name,String.class);
}
}

启动类:NacosServiceConsumerApp

package com.gaoxinfu.demo.spring.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
*
*/
@SpringBootApplication
@EnableDiscoveryClient
public class NacosServiceConsumerApp {


@LoadBalanced
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}

public static void main( String[] args ){
SpringApplication.run(NacosServiceConsumerApp.class,args);
}
}

验证

nacos客户端

SpringCloud集成Nacos进行服务的注册与发现Demo_ide_05

从consumer去调用provider

http://127.0.0.1:8080/findName/gaoxinfu

SpringCloud集成Nacos进行服务的注册与发现Demo_ide_06


举报

相关推荐

0 条评论