springboot+nacos入门

一、nacos与Eureka有相同的三个对象:注册中心、服务提供者、服务消费者

首先在官方下载nacos程序包,点击\nacos\bin\startup.cmd启动
springboot+nacos入门_第1张图片

二、编写provider

1.修改pom,添加maven依赖



<properties>
	<java.version>1.8</java.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-starter-alibaba-nacos-discovery</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>
</dependencies>
<dependencyManagement>
	<dependencies>
		<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>
<build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
		</plugin>
	</plugins>
</build>

2、修改yml配置文件

server:
  port: 8888

spring:
  main:
    allow-bean-definition-overriding: true
  application:
    name: TIS
  server:
    ip: 192.168.70.107
  cloud:
    nacos:
      config:
        file-extension: yml
        server-addr: ${spring.server.ip}:18001
        extension-configs:
          - data-id: are-test.yaml
            group: test
            refresh: true
      discovery:
        server-addr: ${spring.server.ip}:18001
  profiles:
    include: common
    active: local

spring.cloud.nacos.discovery.server-addr指定注册中心的地址,如果你不需要注册该服务,也可以去掉该项,并删除discovery依赖
spring.cloud.nacos.config.server-addr指定配置中心的地址
file-extension指定配置中心中配置文件的格式
3、启动类添加@EnableDiscoveryClient

三、编写consumer

与provider基本一致,使用resttemplate调用服务测试

@Autowired
LoadBalancerClient loadBalancerClient;

@ResponseBody
@RequestMapping("/test")
public String test() {
     
    // 通过spring cloud common中的负载均衡接口选取服务提供节点实现接口调用
    ServiceInstance serviceInstance = loadBalancerClient.choose("nacos-provider");
    System.out.println(serviceInstance.getUri());
    String url = serviceInstance.getUri() + "/testService";
    RestTemplate restTemplate = new RestTemplate();
    String result = restTemplate.getForObject(url, String.class);
    return "Invoke : " + url + ", return : " + result;
}

或者

import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
import static org.springframework.web.bind.annotation.RequestMethod.GET;

@Controller
@RequestMapping("discovery")
public class DiscoveryController {
     

    @NacosInjected
    private NamingService namingService;

    @RequestMapping(value = "/get", method = GET)
    @ResponseBody
    public List<Instance> get(@RequestParam String serviceName) throws NacosException {
     
        return namingService.getAllInstances(serviceName);
    }
}

你可能感兴趣的:(springboot+nacos入门)