欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springcloud使用nacos作为注册中心用openfeign调用服务

程序员文章站 2022-06-13 13:01:21
...
  • pom.xml文件如下,这是最简洁版的,根据自己的需求添加(如mysql驱动等)
<?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.3.0.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.hao</groupId>
	<artifactId>springboot-nacos</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springboot-nacos</name>
	<description>Demo project for Spring Boot</description>

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

	<dependencies>
	
        <!--boot-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
        <!--boot项目检查检查-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!--openfeign(服务调用)  @FeignClient 使用-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>
        
        <!--nacos+cloud-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.2.1.RELEASE</version>
        </dependency>

	</dependencies>
	
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
	
</project>

  • application.yml文件如下,同样是最简洁版的
spring:
  cloud:
    nacos:
      discovery:
        ###服务注册地址
        server-addr: 127.0.0.1:8848
  application: ##服务名称
    name: springboot-nacos01

server:
  port: 8081

  • 主启动类
package com.hao.springbootnacos;

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

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients(basePackages ="com.hao.springbootnacos.service")#服务调用接口类的目录
public class SpringbootNacosApplication {

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

}

  • com.hao.springbootnacos.service包下的调用其他服务的类
package com.hao.springbootnacos.service;

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

@Service
@FeignClient(value = "cloud-payment-service")#另外一个注册在注册中心的服务名
public interface ParmentService {

    @GetMapping("/getPayMent")#服务接口
    public Object getPayMent(@RequestParam("s") String s);
}

  • 服务启动后可在注册中心发现此服务
    springcloud使用nacos作为注册中心用openfeign调用服务
  • controller测试调用
package com.hao.springbootnacos.controller;

import com.hao.springbootnacos.service.ParmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class ParmentController {


    @Autowired
    private ParmentService parmentService;#带@FeignClient的那个服务接口

   @GetMapping("/getParment")
   public Object getParment(){

       Object payMent = parmentService.getPayMent();#调用服务

       System.out.println("payMent:"+payMent);

       return payMent;

   }
}

相关标签: 微服务