Nacos详细搭建流程,案例(简单案例)
nacos
Nacos是由阿里巴巴开源的服务治理中间件,集成了动态服务发现、配置管理和服务元数据管理功能,广泛应用于微服务架构中,简化服务治理过程。
项目地址:https://gitcode.com/gh_mirrors/na/nacos
免费下载资源
·
1、《搭建之前,我们需要有Nacos的安装包,我用的是1.1.4版本》解压完以后,进入到bin目录,双击startup.cmd进行启动,然后访问http://localhost:8848/nacos,进行登录,账号密码默认nacos,nacos。就是这个模样的
2、搭建流程,首先我们创建一个空的项目目录
3、创建完以后,查看自己的maven是否配置好
4、创建两个Maven模块,名字随意
5、将provider中加入依赖和配置文件和启动类和接口
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
</parent>
<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>
<version>0.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!-- SpringCloud的依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
server.port=8070
spring.application.name=nacos-provider
# nacos服务地址
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
# 自定义参数
myName=nacos
package com.cn;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @create: 2021-07-23 14:46
**/
@SpringBootApplication
@EnableDiscoveryClient //开启服务注册发现功能
public class nacosProviderApplication {
public static void main(String[] args) {
SpringApplication.run(nacosProviderApplication.class, args);
}
}
package com.cn.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @create: 2021-07-23 14:58
**/
@RestController
@RequestMapping("/nacos/provider")
//@RefreshScope
public class providerController {
@Value("${myName}")
private String myName;
@RequestMapping("/hello")
public String hello(){
return "你好,我是提供者" + myName;
}
}
6、将consumer中加入依赖和增加配置文件和启动类
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
</parent>
<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>
<version>0.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!-- SpringCloud的依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
server.port=8071
spring.application.name=nacos-consumer
# nacos服务地址
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
# 自定义参数
myName=nacosConsumer
package com.cn;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @create: 2021-07-23 14:46
**/
@SpringBootApplication
@EnableDiscoveryClient //开启服务注册发现功能
@EnableFeignClients //开启远程调用
public class nacosConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(nacosConsumerApplication.class, args);
}
}
6、然后启动Provider服务和consumer服务,登录Nacos,可以看到Provider和consumer已经注册进去了
7、我们利用springCloud的fegin进行远程调用,首先我们在provider中建立接口
package com.cn;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @create: 2021-07-23 14:46
**/
@SpringBootApplication
@EnableDiscoveryClient //开启服务注册发现功能
public class nacosProviderApplication {
public static void main(String[] args) {
SpringApplication.run(nacosProviderApplication.class, args);
}
}
8、在consumer中同时也建立接口
package com.cn.controller;
import com.cn.fegin.providerFegin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @create: 2021-07-23 15:00
**/
@RequestMapping("/nacos/consumer")
@RestController
public class consumerController {
@Autowired
private providerFegin providerFegin;//注入远程调用接口
@RequestMapping("/hi")
public String hi(){
return providerFegin.hello();
}
}
package com.cn.fegin;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author: 王乾
* @create: 2021-07-23 15:05
**/
@FeignClient(name = "nacos-provider")//指定远程调用方的名字
public interface providerFegin {
/**
* @Description: 调用的是服务提供方提供的接口
* @Author: 王乾
* @Date: 2021/7/23 15:07
**/
@RequestMapping("/nacos/provider/hello")//路径一定写全路径
public String hello();
}
9、将两个项目重新启动,浏览器访问http://127.0.0.1:8071//nacos/consumer/hi,查看调用情况
GitHub 加速计划 / na / nacos
29.83 K
12.75 K
下载
Nacos是由阿里巴巴开源的服务治理中间件,集成了动态服务发现、配置管理和服务元数据管理功能,广泛应用于微服务架构中,简化服务治理过程。
最近提交(Master分支:2 个月前 )
4334cd16
* Support custom client configuration timeout.(#12748)
* Add UT.(#12748) 5 天前
b04d2266
9 天前
更多推荐
已为社区贡献5条内容
所有评论(0)