当前位置:   article > 正文

Nacos 整合 spring boot 和 spring cloud_springblade springboot 如何通过nacos跨工程调用springcloud服务

springblade springboot 如何通过nacos跨工程调用springcloud服务

Nacos搭建

Nacos是什么

服务发现和服务健康监测
	可以作为微服务的注册中心,为其他的微服务提供注册与发现
动态配置服务
	可以对配置文件进行动态的刷新
  • 1
  • 2
  • 3
  • 4

Nacos搭建

  1. 下载Nacos的安装包
    gitHub下载地址
    由于github下载过于缓慢,这里提供一份1.3.1的百度云下载地址
    百度云下载地址
    提取码 :d14q
  2. 启动服务器
    解压完成后,进入bin目录,双击startup.cmd启动
  3. 登录管理平台
    启动成功后访问 http://localhost:8848/nacos
    账号和密码都是 nacos

整合springBoot

整合配置中心

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    
    <groupId>org.example</groupId>
    <artifactId>nacos-study</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>config</artifactId>
    <properties>
        <nacos-config-spring-boot.version>0.2.1</nacos-config-spring-boot.version>
    </properties>

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

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

        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>nacos-config-spring-boot-starter</artifactId>
            <version>${nacos-config-spring-boot.version}</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>nacos-config-spring-boot-actuator</artifactId>
            <version>${nacos-config-spring-boot.version}</version>
        </dependency>
    </dependencies>

</project>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

配置文件application.yml

nacos:
  config:
    server-addr: 127.0.0.1:8848
server:
  port: 8080
  • 1
  • 2
  • 3
  • 4
  • 5

ConfigApplication启动类

@SpringBootApplication
// 加载dataId为 example的配置源,并开启自动更新
@NacosPropertySource(dataId = "example", autoRefreshed = true, groupId = "DEFAULT_GROUP")
public class ConfigApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigApplication.class, args);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

ConfigController

@Controller
@RequestMapping("/config")
public class ConfigController {

    @NacosValue(value = "${useLocalCache:false}",autoRefreshed = true)
    private boolean useLocalCache;

    @RequestMapping("/get")
    @ResponseBody
    public boolean get(){
        return useLocalCache;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

配置到这里基本完成,然后启动服务 接来下所有的请求调用,全部都使用postman来完成 获取刚刚自定义的配置
http://localhost:8080/config/get 此时发现得到的返回值为false 然后调用Nacos的Api,来动态修改这个配置,注意这里请求方式必须为POST
http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=example&group=DEFAULT_GROUP&content=useLocalCache=true
结果返回true,表示调用成功 这时我们再次调用上面获取配置的接口
http://localhost:8080/config/get 发现返回值以及是true,表示动态刷新成功了

启动服务发现

pom文件

 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
		
	<groupId>org.example</groupId>
    <artifactId>nacos-study</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <modelVersion>4.0.0</modelVersion>

    <artifactId>discovery</artifactId>
    <properties>
        <nacos-discovery-spring-boot.version>0.2.1</nacos-discovery-spring-boot.version>
    </properties>

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

        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>nacos-discovery-spring-boot-starter</artifactId>
            <version>${nacos-discovery-spring-boot.version}</version>
        </dependency>
    </dependencies>

</project>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

application.yml配置文件

nacos:
  discovery:
    server-addr: 127.0.0.1:8848
server:
  port: 8081
  • 1
  • 2
  • 3
  • 4
  • 5

DiscoveryApplication启动类

@SpringBootApplication
public class DiscoveryApplication {

    public static void main(String[] args){
            SpringApplication.run(DiscoveryApplication.class, args);
        }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

通过NamingService来获取指定名称注册的实例信息

@RestController
@RequestMapping("/discovery")
public class DiscoveryController {

    @NacosInjected
    private NamingService namingService;

    @RequestMapping("/get")
    public List<Instance> get(@RequestParam String serviceName) throws NacosException {
        return namingService.getAllInstances(serviceName);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

此时我们来获取一个名字为discovery实例的信息
http://localhost:8081/discovery/get?serviceName=discovery
发现返回值为空,因为我们还没有主动的去向Nacos服务端注册discovery实例的信息
我们通过下面的API来注册实例,注意请求方式必须为PUT
http://127.0.0.1:8848/nacos/v1/ns/instance?serviceName=discovery&ip=127.0.0.1&port=8081
此时在调用上面的查询API,就能看到对应的数据了

整合springCloud

整合配置中心

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
       <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
     <groupId>org.example</groupId>
    <artifactId>nacos-study</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>spring-cloud</artifactId>

    <properties>
        <spring-boot.version>2.3.2.RELEASE</spring-boot.version>
        <spring-cloud.version>Hoxton.SR7</spring-cloud.version>

    </properties>
    <dependencies>
  		<dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>2.2.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</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>

</project>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

bootstrap.yaml配置文件

server:
  port: 8082
spring:
  application:
    name: cloudConfig
  cloud:
    nacos:
      config:
        server-addr: 127.0.0.1:8848
        file-extension: yaml
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

CloudConfigApplication启动类

@SpringBootApplication
public class CloudConfigApplication {

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

ConfigController测试类

@RestController
@RequestMapping("/config")
@RefreshScope
public class ConfigController {

    /**
     * 这里备注一下,如果配置文件为yaml格式的,在调用刷新配置文件接口的时候
     * http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=cloudConfig.yaml&group=DEFAULT_GROUP&content=useLocalCache=true
     *
     * 在最后那里,因为yaml配置文件里面参数和冒号“:”中间有一个空格,所有这里也要加一个空格,并且,等于符号也要改成冒号“:”要改成下面这样
     * http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=cloudConfig.yaml&group=DEFAULT_GROUP&content=useLocalCache: true
     */

    @Value("${useLocalCache:false}")
    private boolean useLocalCache;

    @RequestMapping("/get")
    public boolean get() {
        return useLocalCache;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

首先我们来修改useLocalCache的值为true,注意请求方式为post
http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=cloudConfig.yaml&group=DEFAULT_GROUP&content=useLocalCache: true
这里的dataId的值由三部分组成
${prefix}-${spring.profiles.active}.${file-extension}
prefix:代表应用的名称,也就是spring.application.name配置的值,也可以通过spring.cloud.nacos.config.prefix来指定
spring.profiles.active:对应当前环境的profile,也就是spring.profiles.active配置的值,如果没有的话,这个值可以忽略,对应的-也忽略
file-extension:为当前配置类容的数据格式,通过spring.cloud.nacos.config.file-extension来配置,目前只支持properties 和 yaml
上面的api返回结果true,就证明,修改成功,这时可以去对应的管理平台查看
此时我们调用***http://localhost:8082/config/get***
可以看见返回值为true,证明修改成功

启动服务发现

这里需要一个生产者和一个消费者来证明Nacos提供的服务的注册和发现
在这里插入图片描述

生产者

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <groupId>org.example</groupId>
    <artifactId>nacos-study</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

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

    <artifactId>spring-cloud</artifactId>


    <properties>
        <spring-boot.version>2.3.2.RELEASE</spring-boot.version>
        <spring-cloud.version>Hoxton.SR7</spring-cloud.version>

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

</project>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56

bootstrap.yaml配置文件

server:
  port: 8011
spring:
  application:
    name: cloud-provider
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

ProviderApplication启动类和接口提供

@SpringBootApplication
@EnableDiscoveryClient
public class ProviderApplication {

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

    @RestController
    class EchoController {
        @RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
        public String echo(@PathVariable String string) {
            return "Hello Nacos Discovery " + string;
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

消费者

pom文件,和上面生产者的一样

bootstrap.yaml配置文件

server:
  port: 8012
spring:
  application:
    name: cloud-consumer
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

ConsumerApplication启动类,以及测试方法

@SpringBootApplication
@EnableDiscoveryClient
public class ConsumerApplication {

    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

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

    @RestController
    public class TestController {

        private final RestTemplate restTemplate;

        @Autowired
        public TestController(RestTemplate restTemplate) {
            this.restTemplate = restTemplate;
        }

        @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
        public String echo(@PathVariable String str) {
            return restTemplate.getForObject("http://cloud-provider/echo/" + str, String.class);
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

此时调用 http://localhost:8012/echo/2020
获取到返回值 Hello Nacos Discovery 2020

本案例github地址

[点这里](https://github.com/longhao1213/nacos-study)
  • 1
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号