Alibaba Nacos 学习(三):Spring Cloud Nacos Discovery - FeignClient,Nacos 服务注册与发现

Alibaba Nacos 学习(一):Nacos介绍与安装

Alibaba Nacos 学习(二):Spring Cloud Nacos Config

Alibaba Nacos 学习(三):Spring Cloud Nacos Discovery - FeignClient,Nacos 服务注册与发现

Alibaba Nacos 学习(四):Nacos Docker

Alibaba Nacos 学习(五):K8S Nacos搭建,使用nfs

接上一篇

https://www.cnblogs.com/woxpp/p/11908262.html

 

服务端新增引入依赖


            org.springframework.cloud
            spring-cloud-starter-alibaba-nacos-discovery
            0.2.1.RELEASE
        

 

启动类开启服务注册支持

@EnableSwagger2
@SpringBootApplication
@EnableDiscoveryClient
public class Application {

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

增加服务注册地址  application.properties

spring.cloud.nacos.discovery.server-addr=192.168.50.31:8848

消费端POM.XML如下,引入ribbon与openfeign支持

"1.0" encoding="UTF-8"?>
"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">
    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.1.RELEASE
         
    
    nacos.consumer
    nacos.consumer
    nacos.consumer
    Demo project for Spring Boot

    
        1.8
        2.0.3.RELEASE
        0.2.1
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-alibaba-nacos-discovery
            0.2.1.RELEASE
        

        
            org.springframework.cloud
            spring-cloud-starter-netflix-ribbon
            2.0.0.RELEASE
        

        
            org.springframework.cloud
            spring-cloud-starter-openfeign
            2.0.0.RELEASE
        
        
            io.springfox
            springfox-swagger2
            2.5.0
        
        
        
            io.springfox
            springfox-swagger-ui
            2.5.0
        
    

创建服务引用对象,利用FeignClient

package example.dockertest.example.dockertest.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@FeignClient(value = "service-provider")
public interface ICustomerService {
    @RequestMapping(value = "/config/get", method = RequestMethod.GET)
    @ResponseBody
    String get();
}

调用服务方法

@RestController
@RequestMapping("/configConsumer")
public class ConfigController {
    @Autowired
    private ICustomerService customerService;

    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ResponseBody
    public String get() {
        return customerService.get();
    }
}

提供服务发现

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

}

启动测试是否能够调用

Alibaba Nacos 学习(三):Spring Cloud Nacos Discovery - FeignClient,Nacos 服务注册与发现_第1张图片

 

你可能感兴趣的:(Alibaba Nacos 学习(三):Spring Cloud Nacos Discovery - FeignClient,Nacos 服务注册与发现)