【SpringCloudAlibaba】nacos注册中心(一)实现feign远程调用

尊重版权,内容学习来源于蚂蚁课堂

本文开始记录SpringCloudAlibaba,使用SpringBoot2.0+,注册中心使用nacos(windows版本)

官网 :https://nacos.io/zh-cn/docs/what-is-nacos.html

 工程初步结构:

本文使用到的Module: nacos-users(服务消费者)  nacos-service-server-data(服务生产者)

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>springboot-nacos</groupId>
    <artifactId>springboot-nacos</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>nacos-service</module>
        <module>nacos-app</module>
        <module>nacos-users</module>
        <module>nacos-auth</module>
        <module>nacos-common</module>
        <module>nacos-gateway</module>
    </modules>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <spring-boot.version>2.1.1.RELEASE</spring-boot.version>
        <spring-boot-admin.version>2.1.1</spring-boot-admin.version>
        <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
        <fast.json.version>1.2.47</fast.json.version>
        <lombok.version>1.16.22</lombok.version>
        <java.version>1.8</java.version>
        <skipTests>true</skipTests>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fast.json.version}</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <!--解决jar包冲突-->
        <dependency>
            <groupId>com.netflix.archaius</groupId>
            <artifactId>archaius-core</artifactId>
            <version>0.7.6</version>
            <exclusions>
                <exclusion>
                    <groupId>com.google.guava</groupId>
                    <artifactId>guava</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>


</project>

2.创建空module  nacos-service  nacos-service-api

   创建api模块 nacos-service-api-data

   nacos-service-api 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>
        <artifactId>nacos-service</artifactId>
        <groupId>springboot-nacos</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>nacos-service-api</artifactId>
    <packaging>pom</packaging>
    <modules>
        <module>nacos-service-api-data</module>
        <module>nacos-service-api-rbac</module>
    </modules>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>
    </dependencies>
</project>

3.nacos-service-api-data 编写简单的接口 

package com.google.api;


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

@FeignClient(value = "nacos-service-server-data")
public interface HelloWorldService {

    @GetMapping("/helloWorld")
    String helloWorld(@RequestParam("age") Integer age);
}

4.创建空module nacos-service-server

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>
        <artifactId>nacos-service</artifactId>
        <groupId>springboot-nacos</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>nacos-service-server</artifactId>
    <packaging>pom</packaging>
    <modules>
        <module>nacos-service-server-data</module>
    </modules>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>0.9.0.RELEASE</version>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

5.创建服务工程module nacos-service-server-data

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>
        <artifactId>nacos-service-server</artifactId>
        <groupId>springboot-nacos</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>nacos-service-server-data</artifactId>

    <dependencies>
        <dependency>
            <groupId>springboot-nacos</groupId>
            <artifactId>nacos-service-api-data</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

编写接口实现类:

package com.google.data.impl;

import com.google.api.HelloWorldService;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author wk
 * @Description:
 * @date 2020/3/3 13:12
 **/
@RestController
public class HelloWorldServiceImpl implements HelloWorldService {

    @Override
    public String helloWorld(Integer age) {
        return "hello world!" + " " + age;
    }
}

服务启动类:

package com.google;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
 * @author wk
 * @Description:
 * @date 2020/3/2 16:06
 **/
@SpringBootApplication
public class DataServiceApplication {

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

yml配置文件:

spring:
  cloud:
    nacos:
      discovery:
        ###服务注册地址
        server-addr: 127.0.0.1:8848
  application:
    name: nacos-service-server-data
server:
  port: 8081

logging:
  path: F:/idea_workspace/springboot-nacos/logs/${spring.application.name}

6.创建服务调用方-测试服务nacos-users

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>
        <artifactId>springboot-nacos</artifactId>
        <groupId>springboot-nacos</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>nacos-users</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>0.9.0.RELEASE</version>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>springboot-nacos</groupId>
            <artifactId>nacos-service-api-data</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>
    </dependencies>
</project>

yml配置:

spring:
  cloud:
    nacos:
      discovery:
        ###服务注册地址
        server-addr: 127.0.0.1:8848
  application:
    name: nacos-users
server:
  port: 8091

logging:
  path: F:/idea_workspace/springboot-nacos/logs/${spring.application.name}

启动类:

package com.google;

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

/**
 * @author wk
 * @Description:
 * @date 2020/3/2 20:49
 **/
@SpringBootApplication
@EnableFeignClients
public class NacosCustUacApplication {

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

调用feign服务类:

package com.google.service;

import com.google.api.HelloWorldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author wk
 * @Description:
 * @date 2020/3/3 13:21
 **/
@RestController
public class HelloService {

    @Autowired
    private HelloWorldService helloWorldService;

    /**
     * 调用feign服务
     *
     * @return
     */
    @GetMapping("/hello")
    public String hello() {
        String s = helloWorldService.helloWorld(99);
        return "feignclient调用 " + s;
    }
}

7.测试运行:

启动nacos,启动服务。

启动成功后,可在http://localhost:8848/nacos/#/serviceManagement?dataId=&group=&appName=&namespace=

页面看到两个服务

调用   /helloWorld


踩坑记录:

1.java.lang.ClassNotFoundException: com.netflix.config.CachedDynamicIntProperty

调用feign服务报错,Springboot2.1.1版本

参考解决方法https://blog.csdn.net/shenhonglei1234/article/details/101213519

2.nacos com.netflix.client.ClientException: Load balancer does not have available server for client: nacos-service-server-data
访问报错
@FeignClient("nacos-service-server-data")这样写就报上面那个错了
@FeignClient(value = "nacos-service-server-data")这样写就没问题。。。
参考:https://blog.csdn.net/weixin_34259232/article/details/92390854

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值