SpringCloud-Nacos 下载安装

SpringCloud-Nacos 下载安装

下载网址 https://github.com/alibaba/nacos

Nacos启动闪退问题

注意 nacos1.3.2的启动配置默认是集群模式
编辑startup.cmd
set MODE="standalone"

引用 https://blog.csdn.net/g936795350/article/details/108383645

Nacos启动报错

jdk环境变量

JAVA_HOME 要配置jdk1.8

启动Nacos

运行startup.cmd

SpringCloud 搭建Nacos 配置中心

	<dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.2.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

	 <dependencyManagement>
        <dependencies>
           <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${
     spring.cloud.version}</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>

创建nacos-priovider

在SpringCloud 下创建Module

POM 中添加

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

yml

server:
  port: 8081  #指定为8081端口

spring:
  application:
    name: nacos  #服务名
  cloud:
    nacos:
      discovery:
        register-enabled: true
        weight: 1
        server-addr: 127.0.0.1:8848 #服务注册地址(nacos默认为8848端口)

启动类application

defining a bean of type ‘com.netflix.client.config.IClientConfig’ in your configuration

启动类要和其它文件放在同一目录下,否则报以上问题

@SpringBootApplication
@EnableDiscoveryClient
public class NacosApplication {
     

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

}
@RestController
@RequestMapping("/nacos")
public class EchoController {
     
    @GetMapping(value = "/hello/{str}", produces = "application/json")
    public String hello(@PathVariable String str) {
     
        String result = "hello==>" + str;
        return result;
    }
}

创建nacos-consumer

在SpringCloud 下创建Module

POM 中添加 步骤同nacos-priovider

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

YML

server:
  port: 8082  #指定为8082端口

spring:
  application:
    name: nacos-consumer  #服务名
  cloud:
    nacos:
      discovery:
        register-enabled: true
        weight: 1
        server-addr: 127.0.0.1:8848 #服务注册地址(nacos默认为8848端口)

NacosConsumerApplication 启动类



@SpringBootApplication
@EnableDiscoveryClient
public class NacosConsumerApplication {
     

    public static void main(String[] args) {
     
        SpringApplication.run(NacosConsumerApplication.class, args);
    }
    @LoadBalanced
    @Bean
    public RestTemplate restTemplate(){
     
        return new RestTemplate();
    }
}

NacosConsumerController 控制类

@RestController
public class NacosConsumerController {
     
    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/hello")
    public String hiResttemplate(){
     
        return restTemplate.getForObject("http://nacos-priovider/hello/asd?name=resttemplate",String.class);

    }
}

你可能感兴趣的:(SpringCloud,spring,spring,boot,java)