zoukankan      html  css  js  c++  java
  • 第十二章 SpringCloud之Nacos Client Provider

    ##########服务提供者############

    1、pom.xml文件配置

    <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>com.test</groupId>
            <artifactId>springcloud</artifactId>
            <version>1.0-SNAPSHOT</version>
            <relativePath>../</relativePath> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.test</groupId>
        <artifactId>nacos-server</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>nacos-server</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
            <spring-cloud.version>Greenwich.SR1</spring-cloud.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-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <!--nacos组件-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
                <version>0.2.1.RELEASE</version>
            </dependency>
        </dependencies>
    
    
        <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>
            </dependencies>
        </dependencyManagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    View Code

    主要关注:

            <!--nacos组件-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
                <version>0.2.1.RELEASE</version>
            </dependency>    

    2、application.properties文件配置

    server.port=8001   
    spring.application.name=springcloud-nacos-discovery-provider  #虚拟主机名
    spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848   #nacos注册中心地址

    3、添加注解

    package com.test.nacosserver;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    
    @SpringBootApplication
    @EnableDiscoveryClient
    public class NacosServerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(NacosServerApplication.class, args);
        }
    
    }

    4、服务提供类

    package com.test.nacosserver.controller;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class TestController {
    
        @GetMapping("/test/{string}")
        public String test(@PathVariable String string) {
            return "Hello Nacos :" + string;
        }
    }

    ##########服务提消费者############

    1、pom.xml文件

    <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>com.test</groupId>
            <artifactId>springcloud</artifactId>
            <version>1.0-SNAPSHOT</version>
            <relativePath>../</relativePath> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.test</groupId>
        <artifactId>nacos-client-consumer</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>nacos-client-consumer</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
            <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
                <version>0.2.1.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>net.minidev</groupId>
                <artifactId>json-smart</artifactId>
                <version>2.3</version>
                <scope>compile</scope>
            </dependency>
        </dependencies>
    
        <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>
            </dependencies>
        </dependencyManagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    View Code

    主要关注:

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
                <version>0.2.1.RELEASE</version>
            </dependency>  

    2、application.properties文件配置

    server.port=8002
    spring.application.name=springcloud-nacos-discovery-consumer
    spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

    3、添加注解

    package com.test.nacosclientconsumer;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.cloud.client.loadbalancer.LoadBalanced;
    import org.springframework.context.annotation.Bean;
    import org.springframework.web.client.RestTemplate;
    
    @SpringBootApplication
    @EnableDiscoveryClient
    public class NacosClientConsumerApplication {
      //使用了Ribbon
        @LoadBalanced
        @Bean
        public RestTemplate restTemplate() {
            return new RestTemplate();
        }
    
        public static void main(String[] args) {
            SpringApplication.run(NacosClientConsumerApplication.class, args);
        }
    
    }

    4、服务调用

    package com.test.nacosclientconsumer.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    
    @RestController
    public class TestConrtroller {
    
        @Autowired
        private RestTemplate restTemplate;
    
        @GetMapping("test/{string}")
        public String test(@PathVariable String string) { //使用虚拟主机名进行调用
            return restTemplate.getForObject("http://springcloud-nacos-discovery-provider/test/" + string, String.class);
        }
    }
  • 相关阅读:
    微信JS SDK Demo
    微信jssdk常见错误及解决方法
    多机定时任务处理
    python zip压缩文件 并移动到指定目录
    Nginx + Uswgi + Django的部署
    pycharm 安装第三方库报错:AttributeError: 'module' object has no attribute 'main'
    zabbix监控
    转:老张喝茶 教你同步异步 阻塞与非阻塞
    odoo开发笔记 -- 多对多字段追加数据
    xml文件对比工具推荐:Altova XMLSpy 2013
  • 原文地址:https://www.cnblogs.com/ywjfx/p/12156343.html
Copyright © 2011-2022 走看看