springCloud+nacos实现feign调用

1. 服务端

将服务所需的api相关,放到ali nacos上,客户端直接实现即可;

2. 客户端

2.1 pom

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

2.2 application.yml

  # Nacos config
spring:
  cloud:
    nacos:
      discovery:
        # 服务中心地址
        server-addr: 111.22.333.444:8848
####Spring Cloud Server config
microservice:
  server:
    process:
      name: pfserver
      projectId: 0
      appId: 4
      status: OPEN
    auth:
      name: QESAuthCenterServer
#### feign
feign:
  client:
    config:
      default:
        connectTimeout: 20000
        readTimeout: 20000
  httpclient:
    connection-timeout: 20000
    connection-timer-repeat: 20000
    enabled: true
    max-connections: 200
    max-connections-per-route: 50

2.3 启动类

添加这两个注解:
@EnableDiscoveryClient
@EnableFeignClients

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.*;

@EnableDiscoveryClient
@RestController
@EnableFeignClients
@EnableAsync @EnableScheduling @EnableTransactionManagement @EnableConfigurationProperties
@MapperScan({ "xxx" ,"yyy"})
@ServletComponentScan
@SpringBootApplication
public class Application {...}

2.4 实现服务中心接口

package com.leinovo.qes.portal.ms;

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

/**
 * @author xxx
 */
@FeignClient("QESAuthCenterServer")
public interface AuthenticationMicroService {

    /**
     * 通过id获取用户基础信息
     * @param id
     * @return
     */
    @GetMapping(value = "/auth/user/{id}")
    String getUserById(@PathVariable(name = "id")  Long id);
}

2.5 使用feign

package com.leinovo.qes.portal.ms;

import com.leinovo.qes.platform.common.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: leimin
 * @Description: new class
 * @Date: 2020/6/18 14:18
 * @Version: 1.0
 */
@Slf4j
@Component
public class TestService {
    /**
     * 引入feign接口
     */
    @Resource
    private AuthenticationMicroService authenticationMicroService;

    /**
     * 
     * @param ids
     * @return
     */
    public List<User> getUsersById(Long id) {
        log.info("get users by id=[{}]", id);
        String json = authenticationMicroService.getUserById(id);
        return AuthenticationJsonParser.parseUsers(json);
    }
}

你可能感兴趣的:(springBoot,nacos,feign,springCloud,nacos实现feign调用)