nacos作为注册中心的简单项目配置

注册中心(nacos)

github地址

接入方式-中文版

一、版本管理

  • Spring Boot 2.2.5.RELEASE
  • Spring Cloud Hoxton.SR3
  • Spring Cloud Alibaba 2.2.1.RELEASE

二、依赖引入

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

<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
</properties>

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

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>2.2.1.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

三、接入项目

1、注册中心依赖

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

2、bootstrap.properties

spring.cloud.nacos.server-addr=127.0.0.1:8848
spring.application.name=test-demo

有时新建的properties文件会图标异常,配置没有提示。

IntelliJ IDEA按一下步骤操作:

  • File ==> Project Settings ==> Modules ==> Spring
  • 点击右侧上方的绿色图标(Spring的icon)
  • 点击“+”号
  • 选择文件后,一路“OK”即可。

参考连接

3、application.yml

spring:
  cloud:
    nacos:
      config:
        server-addr: 127.0.0.1:8848 # nacos-server 地址
  application:
    # 项目名称
    name: test-demo # 项目名称

4、启动类配置

@SpringBootApplication
//  @EnableDiscoveryClient注解开启服务注册与发现功能
@EnableDiscoveryClient
public class ProviderApplication {

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

四、OpenFeign

1、引入open-feign

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

2、启动类

@SpringBootApplication
// Feign扫描的包路径
@EnableFeignClients(basePackages = "xxx.xxx.xxx")
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

3、service

// test-demo为想要调用接口在注册中心中的名称
@FeignClient("test-demo")
public interface DemoService {
    // 此处路径要写全。比如调用接口的请求路径为/demo/show,此处也为/demo/show
	@RequestMapping("/demo/show")
	public String show();
}

4、controller

@Controller
public class DemoController {
    
    // 正常注入即可
	@Autowired
	DemoService demoService;
		
	@RequestMapping("/hello")
	public String helloName() {
		return demoService.show();
	}
}

猜你喜欢

转载自www.cnblogs.com/luckyzs/p/13166373.html