码迷,mamicode.com
首页 > 编程语言 > 详细

SpringCloud之Nacos服务发现(十六)

时间:2019-10-25 09:51:33      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:main   item   ping   depend   scope   run   src   art   yml   

一 服务提供配置

  • pom.xml

 <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--nacos 一定要注意版本问题-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        <version>0.9.0.RELEASE</version>
    </dependency>
</dependencies>

 

  • application.yml

    server:
      port: 8881
    spring:
      application:
        name: nacos-product
      cloud:
        nacos:
          discovery:
            server-addr: 192.168.180.113:8848

     

  • 启动类

    @SpringBootApplication
    @EnableDiscoveryClient
    public class NacosProductApplication {
    ?
        public static void main(String[] args) {
            SpringApplication.run(NacosProductApplication.class, args);
        }
    ?
    }
     
  • web层

     1 /**
     2  * @author WGR
     3  * @create 2019/10/25 -- 0:29
     4  */
     5 @RestController
     6 @RequestMapping("/user")
     7 public class UserController {
     8     @GetMapping
     9     public String getUser(){
    10         return "admin";
    11     }
    12 }

     

测试结果:

技术图片

二 提供消费配置

  • pom.xml

    <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
            <!--web-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    ?
            <!--nacos 一定要注意版本问题-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
                <version>0.9.0.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>
        </dependencies>

     

  • application.yml

    server:
      port: 8882
    spring:
      application:
        name: nacos-consumer
      cloud:
        nacos:
          discovery:
            server-addr: 192.168.180.113:8848

     

  • 启动类配置

    @SpringBootApplication
    @EnableFeignClients
    public class NacosConsumerApplication {
    ?
        public static void main(String[] args) {
            SpringApplication.run(NacosConsumerApplication.class, args);
        }
    ?
    }
    ?

     

  • web层

    /**
     * @author WGR
     * @create 2019/10/25 -- 0:46
     */
    @RestController
    @RequestMapping("/test")
    public class TestController {
    ?
        @Autowired(required = false)
        private TestService testService;
    ?
        @GetMapping("/user")
        public String getUser() {
            return testService.getUser();
        }
    }

     

  • service层

    /**
     * @author WGR
     * @create 2019/10/25 -- 0:47
     */
    @FeignClient("nacos-product")
    public interface TestService {
    ?
        @GetMapping("user")
        String getUser();
    ?
    }
    ?

    测试:http://localhost:8882/test/user

    技术图片

 技术图片

SpringCloud之Nacos服务发现(十六)

标签:main   item   ping   depend   scope   run   src   art   yml   

原文地址:https://www.cnblogs.com/dalianpai/p/11735937.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!