springcloud之feign客户端调用

1、nacos注册中心准备

参考nacos学习笔记

启动nacos注册中心

2、工程结构

springcloud-2.0-feign-parent
├── springcloud-2.0-feign-member
│   ├── springcloud-2.0-feign-member-api
│   ├── springcloud-2.0-feign-member-service
│   ├── pom.xml
├── springcloud-2.0-feign-order
│   ├── springcloud-2.0-feign-order-api
│   ├── springcloud-2.0-feign-order-service
│   ├── pom.xml
├── pom.xml

3、parent工程

springcloud-2.0-feign-parent

pom.xml

<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>com.mine</groupId>
	<artifactId>springcloud-2.0-feign-parent</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>
	<modules>
		<module>springcloud-2.0-feign-member</module>
		<module>springcloud-2.0-feign-order</module>
	</modules>

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

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.RELEASE</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<dependencies>
		<!-- SpringBoot整合Web组件 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- SpringCloud nacos 注冊中心 -->
		<dependency>
			<groupId>com.alibaba.cloud</groupId>
			<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
			<version>2.0.1.RELEASE</version>
		</dependency>
		<!-- SpringCloud nacos 配置中心 -->
		<dependency>
			<groupId>com.alibaba.cloud</groupId>
			<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
			<version>2.0.1.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-openfeign</artifactId>
		</dependency>


		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>
	</dependencies>
</project>

4、member工程

4.1 springcloud-2.0-feign-member

pom.xml

<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>
  <parent>
    <groupId>com.mine</groupId>
    <artifactId>springcloud-2.0-feign-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>springcloud-2.0-feign-member</artifactId>
  <packaging>pom</packaging>
  <modules>
  	<module>springcloud-2.0-feign-member-api</module>
  	<module>springcloud-2.0-feign-member-service</module>
  </modules>
</project>

4.2 springcloud-2.0-feign-member-api

pom.xml

<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>
	<parent>
		<groupId>com.mine</groupId>
		<artifactId>springcloud-2.0-feign-member</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>springcloud-2.0-feign-member-api</artifactId>
</project>

MemberModel

package com.mine.service.model;

import lombok.Data;

@Data
public class MemberModel {
	public String name;

	public Integer age;
}

MemberService

package com.mine.service;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.mine.service.model.MemberModel;

public interface MemberService {
	@RequestMapping("/getMember")
	public MemberModel getMember(@RequestParam("name") String name);
}

4.3 springcloud-2.0-feign-member-service

pom.xml

<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>
	<parent>
		<groupId>com.mine</groupId>
		<artifactId>springcloud-2.0-feign-member</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>springcloud-2.0-feign-member-service</artifactId>

	<dependencies>
		<dependency>
			<groupId>com.mine</groupId>
			<artifactId>springcloud-2.0-feign-member-api</artifactId>
			<version>${project.version}</version>
		</dependency>
	</dependencies>

</project>

bootstrap.properties

server.port=8000

spring.application.name=nacos-member

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

spring.cloud.nacos.config.server-addr=127.0.0.1:8848
#spring.cloud.nacos.config.file-extension=yml
#spring.cloud.nacos.config.group=dev-group
spring.cloud.nacos.config.shared-dataids=common.properties

MemberServiceImpl

package com.mine.service.impl;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.mine.service.MemberService;
import com.mine.service.model.MemberModel;

@RestController
public class MemberServiceImpl implements MemberService {
	@Value("${server.port}")
	private String port;

	@RequestMapping("/getMember")
	public MemberModel getMember(@RequestParam("name") String name) {
		System.out.println("i am from " + port);
		MemberModel memberModel = new MemberModel();
		memberModel.setName(name);
		memberModel.setAge(10);
		return memberModel;
	}

}

NacosAppMember

package com.mine;

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

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class NacosAppMember {

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

}

5、order工程

5.1 springcloud-2.0-feign-order

pom.xml

<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>
  <parent>
    <groupId>com.mine</groupId>
    <artifactId>springcloud-2.0-feign-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>springcloud-2.0-feign-order</artifactId>
  <packaging>pom</packaging>
  <modules>
  	<module>springcloud-2.0-feign-order-api</module>
  	<module>springcloud-2.0-feign-order-service</module>
  </modules>
</project>

5.2 springcloud-2.0-feign-order-api

pom.xml

<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>
  <parent>
    <groupId>com.mine</groupId>
    <artifactId>springcloud-2.0-feign-order</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>springcloud-2.0-feign-order-api</artifactId>
</project>

OrderService

package com.mine.service;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

public interface OrderService {
	@RequestMapping("/gerMemberFromOrder")
	public String gerMemberFromOrder(@RequestParam("name") String name);
}

5.3 springcloud-2.0-feign-order-service

pom.xml

<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>
	<parent>
		<groupId>com.mine</groupId>
		<artifactId>springcloud-2.0-feign-order</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>springcloud-2.0-feign-order-service</artifactId>

	<dependencies>
		<dependency>
			<groupId>com.mine</groupId>
			<artifactId>springcloud-2.0-feign-order-api</artifactId>
			<version>${project.version}</version>
		</dependency>

		<dependency>
			<groupId>com.mine</groupId>
			<artifactId>springcloud-2.0-feign-member-api</artifactId>
			<version>${project.version}</version>
		</dependency>
	</dependencies>

</project>

bootstrap.properties

server.port=8010

spring.application.name=nacos-order

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

spring.cloud.nacos.config.server-addr=127.0.0.1:8848
#spring.cloud.nacos.config.file-extension=yml
#spring.cloud.nacos.config.group=dev-group
spring.cloud.nacos.config.shared-dataids=common.properties

MemberServiceFeign

package com.mine.service.feign.member;

import org.springframework.cloud.openfeign.FeignClient;

import com.mine.service.MemberService;

@FeignClient("nacos-member")
public interface MemberServiceFeign extends MemberService {

}

OrderServiceImpl

package com.mine.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.mine.service.OrderService;
import com.mine.service.feign.member.MemberServiceFeign;

@RestController
public class OrderServiceImpl implements OrderService {
	@Autowired
	private MemberServiceFeign memberServiceFeign;

	@RequestMapping("/gerMemberFromOrder")
	public String gerMemberFromOrder(@RequestParam("name") String name) {
		return memberServiceFeign.getMember(name).toString();
	}

}

NacosAppOrder

package com.mine;

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

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class NacosAppOrder {

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

}

6、启动

启动nacos注册中心
启动NacosAppOrder与NacosAppMember
注意:feign客户端调用,默认支持ribbon客户端负载均衡,所以订单中心与会员中心都可以修改端口号启动多台实现负载均衡效果。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值