0
点赞
收藏
分享

微信扫一扫

使用nacos作为注册中心来搭建spring-cloud-openfeign的简单demo

米小格儿 2022-07-13 阅读 104


文章目录

  • ​​1.启动nacos​​
  • ​​2.服务端项目搭建 [demo-spring-cloud-openfeign-provider]​​
  • ​​2.1.源码​​
  • ​​2.2.pom.xml​​
  • ​​2.3.配置文件:application.yml​​
  • ​​2.4.创建后端服务:OpenfeiginProviderClient​​
  • ​​2.5.创建启动类:OpenfeignProviderApp​​
  • ​​2.6.启动后验证:服务验证/say/{message}​​
  • ​​3.客户端项目搭建 [demo-spring-cloud-openfeign-consumer]​​
  • ​​3.1.源码​​
  • ​​3.2.pom.xml​​
  • ​​3.3.配置文件:application.yml​​
  • ​​3.4.创建调用后端provider服务的service:OpenfeignConsumerService​​
  • ​​3.5.创建客户端入口类:OpenfeignConsumerClient​​
  • ​​3.6.创建启动类:OpenfeignConsumerApp​​
  • ​​3.7.nacos注册中心​​
  • ​​3.8.启动验证​​

1.启动nacos

1.不在赘述,可以参考下面的安装启动地址

2.服务端项目搭建 [demo-spring-cloud-openfeign-provider]

2.1.源码

https://gitee.com/gaoxinfu_admin/demo-spring-cloud/tree/master/demo-spring-cloud-openfeign-provider

2.2.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">
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.2.5.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<groupId>com.gaoxinfu.demo.spring.cloud.openfeign</groupId>
<artifactId>demo-spring-cloud-openfeign-provider</artifactId>
<version>1.0-SNAPSHOT</version>

<name>demo-spring-cloud-openfeign-provider</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>

<!--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.2.2.RELEASE</version>
</dependency>


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

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

重点截图

使用nacos作为注册中心来搭建spring-cloud-openfeign的简单demo_maven


使用nacos作为注册中心来搭建spring-cloud-openfeign的简单demo_maven_02

2.3.配置文件:application.yml

server:
port: 8079
spring:
application:
name: openfeign-provider
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848

2.4.创建后端服务:OpenfeiginProviderClient

package com.gaoxinfu.demo.spring.cloud.openfeign;

import org.springframework.web.bind.annotation.*;

/**
* @Description:
* @Author: gaoxinfu
* @Date: 2020-10-27 17:40
*/

@RestController
public class OpenfeiginProviderClient {

@RequestMapping(value = "/say/{message}",method = RequestMethod.GET)
public String say(@PathVariable String message){
return "OpenfeiginProviderClient = "+message;
}
}

2.5.创建启动类:OpenfeignProviderApp

package com.gaoxinfu.demo.spring.cloud.openfeign;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
* Hello world!
*
*/
@SpringBootApplication
@EnableDiscoveryClient
public class OpenfeignProviderApp
{
public static void main( String[] args )
{
SpringApplication.run(OpenfeignProviderApp.class,args);
}
}

2.6.启动后验证:服务验证/say/{message}

http://127.0.0.1:8079/say/gaoxinfu

使用nacos作为注册中心来搭建spring-cloud-openfeign的简单demo_maven_03

3.客户端项目搭建 [demo-spring-cloud-openfeign-consumer]

3.1.源码

​​https://gitee.com/gaoxinfu_admin/demo-spring-cloud/tree/master/demo-spring-cloud-openfeign-consumer​​

3.2.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">
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.2.5.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<groupId>com.gaoxinfu.demo.spring.cloud.openfeign</groupId>
<artifactId>demo-spring-cloud-openfeign-consumer</artifactId>
<version>1.0-SNAPSHOT</version>

<name>demo-spring-cloud-openfeign-consumer</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>0.2.2.RELEASE</version>
</dependency>

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

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

3.3.配置文件:application.yml

server:
port: 8080
spring:
application:
name: openfeign-consumer
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848

3.4.创建调用后端provider服务的service:OpenfeignConsumerService

package com.gaoxinfu.demo.spring.cloud.openfeign;

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

/**
* @Description:
* @Author: gaoxinfu
* @Date: 2020-10-27 18:03
*/
@FeignClient("openfeign-provider")
public interface OpenfeignConsumerService {

@RequestMapping(value="/say/{message}",method = RequestMethod.GET)
String say(@PathVariable String message);

}

3.5.创建客户端入口类:OpenfeignConsumerClient

package com.gaoxinfu.demo.spring.cloud.openfeign;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
* @Description:
* @Author: gaoxinfu
* @Date: 2020-10-27 18:02
*/
@RestController
public class OpenfeignConsumerClient {

@Resource
private OpenfeignConsumerService openfeignConsumerService;

@RequestMapping(value = "/say/{message}",method = RequestMethod.GET)
public String say(@PathVariable String message){
return openfeignConsumerService.say(message);
}

}

3.6.创建启动类:OpenfeignConsumerApp

package com.gaoxinfu.demo.spring.cloud.openfeign;

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

/**
* Hello world!
*
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class OpenfeignConsumerApp
{
public static void main( String[] args )
{
SpringApplication.run(OpenfeignConsumerApp.class,args);
}
}

3.7.nacos注册中心

使用nacos作为注册中心来搭建spring-cloud-openfeign的简单demo_ide_04

3.8.启动验证

通过客户端,可以访问到服务端了

http://127.0.0.1:8080/say/gaoxinfu

使用nacos作为注册中心来搭建spring-cloud-openfeign的简单demo_ide_05


举报

相关推荐

0 条评论