vlambda博客
学习文章列表

Nacos_02|nacos-注册中心&openFeign使用


接引之书JieYinzhishu 第656页



Nacos_02|nacos-注册中心&openFeign使用

Maven3.6

Tomcat8.5

Jdk8
IDEA 2018

Nacos_02|nacos-注册中心&openFeign使用


消费者


Nacos_02|nacos-注册中心&openFeign使用

Nacos_02|nacos-注册中心&openFeign使用

    package com.atguigu.consumerdemo.controller;

    import com.atguigu.consumerdemo.feign.ProviderClient;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;

    @RestController
    public class Hiconsumer {

        //.11
        @Autowired
        private ProviderClient providerClient;

        @GetMapping("/hi")
        public String hi(){
            String hello = this.providerClient.hello();    //.12
            return "hi consumer,========="+hello;
        }
    }




Nacos_02|nacos-注册中心&openFeign使用

    package com.atguigu.consumerdemo.feign;

    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.GetMapping;

    //.10
    /**
     * OpenFeign用法:
     * 1.这是方法是一个接口;
     * 2.注解括号里面的名字是提供者的服务名;
     * 3.想调用哪个方法,就从提供者里面复制过来就行,不用复制实现;
     * 4.然后在自己(消费者)的方法调用实现;
     */

    @FeignClient("service-provider")
    public interface ProviderClient {

        @GetMapping("/hello")
        public String hello();
    }




Nacos_02|nacos-注册中心&openFeign使用

    package com.atguigu.consumerdemo;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.openfeign.EnableFeignClients;

    @SpringBootApplication
    //@EnableDiscoveryClient   //.7   这个注解,在消费者这边是可以省略的
    @EnableFeignClients         //.9
    public class ConsumerDemoApplication {

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

    }




Nacos_02|nacos-注册中心&openFeign使用

Nacos_02|nacos-注册中心&openFeign使用

    server.port=8080
    spring.application.name=service-consumer
    #.6
    ### Nacos注册中心端口号
    spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848




Nacos_02|nacos-注册中心&openFeign使用

    <?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>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.2.6.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.atguigu</groupId>
        <artifactId>consumer-demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>consumer-demo</name>
        <description>Demo project for Spring Boot</description>

        <properties>
            <java.version>1.8</java.version>
        </properties>

        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!--.5-->
            <!--nacos服务中心-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
                <version>0.2.2.RELEASE</version>
            </dependency>
            <!--.8-->
            <!--openfeign,因为它是原生的,在springcloud里面已经有版本号,(注:这是消费者引入)-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
            <!--热部署-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </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>
        </dependencies>
        <!-- SpringCloud的依赖 -->
        <dependencyManagement>
            <dependencies>
                <!--spring cloud Hoxton.SR1-->
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Hoxton.SR1</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>





Nacos_02|nacos-注册中心&openFeign使用


提供者


Nacos_02|nacos-注册中心&openFeign使用

Nacos_02|nacos-注册中心&openFeign使用

    package com.atguigu.providerdemo.controller;

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;

    //.4
    @RestController
    public class HelloController {

        @Value("${myName}")
        private String myName;

        @GetMapping("/hello")
        public String hello(){
            return "hello"+myName;
        }
    }




Nacos_02|nacos-注册中心&openFeign使用

    package com.atguigu.providerdemo;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;


    @SpringBootApplication
    @EnableDiscoveryClient
      //.3
    public class ProviderDemoApplication {

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

    }




Nacos_02|nacos-注册中心&openFeign使用

Nacos_02|nacos-注册中心&openFeign使用

    server.port=8070
    spring.application.name=service-provider
    #.2
    ### Nacos注册中心端口号
    spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

    myName=nacos




Nacos_02|nacos-注册中心&openFeign使用

    <?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>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.2.6.RELEASE</version>
            <relativePath/> <!-- 如果要用2.2.x版本的springboot,那么SpringCloud就要用H版本,否则可能会有版本问题出现 -->
        </parent>
        <groupId>com.atguigu</groupId>
        <artifactId>provider-demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>provider-demo</name>
        <description>Demo project for Spring Boot</description>

        <properties>
            <java.version>1.8</java.version>
        </properties>

        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!--.1-->
            <!--
            nacos服务中心,
            注意:版本 0.2.x.RELEASE 对应的是 Spring Boot 2.x 版本,版本 0.1.x.RELEASE 对应的是 Spring Boot 1.x 版本。
            -->

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
                <version>0.2.2.RELEASE</version>
            </dependency>
            <!--热部署-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </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>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-commons</artifactId>
                <version>2.2.1.RELEASE</version>
                <scope>compile</scope>
            </dependency>
        </dependencies>
        <!-- SpringCloud的依赖 -->
        <dependencyManagement>
            <dependencies>
                <!--spring cloud Hoxton.SR1-->
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Hoxton.SR1</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>



Nacos_02|nacos-注册中心&openFeign使用

Nacos_02|nacos-注册中心&openFeign使用

Nacos_02|nacos-注册中心&openFeign使用

Nacos_02|nacos-注册中心&openFeign使用


上下滑动,可查看更多▲

目录大纲 Directory outline