SpringCloud使用bootstrap配置Nacos的注册中心和配置中心

导入配置

 <dependencys>
    <dependency>
          <groupId>org.springframework.cloudgroupId>
          <artifactId>spring-cloud-starter-bootstrapartifactId>
      dependency>
 dependencys>

当使用对应的bootstrap进行配置的时候要注意对应的配置的优先级:bootstrap.yml>application.properties>application.yml>外部文件(多文件遵守properties>yml)

开始配置bootstrap.yml

server:
  port: 9090

spring:
  application:
    name: OFOGateway
  profiles:
    # 环境配置
    active: dev

  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
        namespace: dev
      config:
        server-addr: 127.0.0.1:8848
        namespace: e9d6c1b0-75c6-4d3c-a099-58556a6a5edd
        file-extension: yml
        shared-configs:
          - application-all.${spring.cloud.nacos.config.file-extension}

这里会匹配Nacos的OFOGateway.yml、application-all.yml配置文件

测试

/*
 * Copyright 2013-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.ofogateway.demos.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author theonefx
 */
@Controller
public class BasicController {

	//调取对应的Nacos的服务的值
    @Value("${spring.kk}")
    String kk;

    // http://127.0.0.1:8080/hello?name=lisi
    @RequestMapping("/hello")
    @ResponseBody
    public String hello(@RequestParam(name = "name", defaultValue = "unknown user") String name) {
        return "Hello " + name+kk;
    }

    // http://127.0.0.1:8080/user
    @RequestMapping("/user")
    @ResponseBody
    public User user() {
        User user = new User();
        user.setName("theonefx");
        user.setAge(666);
        return user;
    }

    // http://127.0.0.1:8080/save_user?name=newName&age=11
    @RequestMapping("/save_user")
    @ResponseBody
    public String saveUser(User u) {
        return "user will save: name=" + u.getName() + ", age=" + u.getAge();
    }

    // http://127.0.0.1:8080/html
    @RequestMapping("/html")
    public String html() {
        return "index.html";
    }

    @ModelAttribute
    public void parseUser(@RequestParam(name = "name", defaultValue = "unknown user") String name
            , @RequestParam(name = "age", defaultValue = "12") Integer age, User user) {
        user.setName("zhangsan");
        user.setAge(18);
    }
}

你可能感兴趣的:(Springboot,SpringCloud,spring,cloud,bootstrap,spring)