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.0modelVersion>
	<groupId>com.minegroupId>
	<artifactId>springcloud-2.0-feign-parentartifactId>
	<version>0.0.1-SNAPSHOTversion>
	<packaging>pompackaging>
	<modules>
		<module>springcloud-2.0-feign-membermodule>
		<module>springcloud-2.0-feign-ordermodule>
	modules>

	<parent>
		<groupId>org.springframework.bootgroupId>
		<artifactId>spring-boot-starter-parentartifactId>
		<version>2.0.3.RELEASEversion>
	parent>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloudgroupId>
				<artifactId>spring-cloud-dependenciesartifactId>
				<version>Finchley.RELEASEversion>
				<type>pomtype>
				<scope>importscope>
			dependency>
		dependencies>
	dependencyManagement>

	<dependencies>
		
		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-webartifactId>
		dependency>
		
		<dependency>
			<groupId>com.alibaba.cloudgroupId>
			<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
			<version>2.0.1.RELEASEversion>
		dependency>
		
		<dependency>
			<groupId>com.alibaba.cloudgroupId>
			<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
			<version>2.0.1.RELEASEversion>
		dependency>

		<dependency>
			<groupId>org.springframework.cloudgroupId>
			<artifactId>spring-cloud-starter-openfeignartifactId>
		dependency>


		<dependency>
			<groupId>org.projectlombokgroupId>
			<artifactId>lombokartifactId>
		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.0modelVersion>
  <parent>
    <groupId>com.minegroupId>
    <artifactId>springcloud-2.0-feign-parentartifactId>
    <version>0.0.1-SNAPSHOTversion>
  parent>
  <artifactId>springcloud-2.0-feign-memberartifactId>
  <packaging>pompackaging>
  <modules>
  	<module>springcloud-2.0-feign-member-apimodule>
  	<module>springcloud-2.0-feign-member-servicemodule>
  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.0modelVersion>
	<parent>
		<groupId>com.minegroupId>
		<artifactId>springcloud-2.0-feign-memberartifactId>
		<version>0.0.1-SNAPSHOTversion>
	parent>
	<artifactId>springcloud-2.0-feign-member-apiartifactId>
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.0modelVersion>
	<parent>
		<groupId>com.minegroupId>
		<artifactId>springcloud-2.0-feign-memberartifactId>
		<version>0.0.1-SNAPSHOTversion>
	parent>
	<artifactId>springcloud-2.0-feign-member-serviceartifactId>

	<dependencies>
		<dependency>
			<groupId>com.minegroupId>
			<artifactId>springcloud-2.0-feign-member-apiartifactId>
			<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.0modelVersion>
  <parent>
    <groupId>com.minegroupId>
    <artifactId>springcloud-2.0-feign-parentartifactId>
    <version>0.0.1-SNAPSHOTversion>
  parent>
  <artifactId>springcloud-2.0-feign-orderartifactId>
  <packaging>pompackaging>
  <modules>
  	<module>springcloud-2.0-feign-order-apimodule>
  	<module>springcloud-2.0-feign-order-servicemodule>
  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.0modelVersion>
  <parent>
    <groupId>com.minegroupId>
    <artifactId>springcloud-2.0-feign-orderartifactId>
    <version>0.0.1-SNAPSHOTversion>
  parent>
  <artifactId>springcloud-2.0-feign-order-apiartifactId>
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.0modelVersion>
	<parent>
		<groupId>com.minegroupId>
		<artifactId>springcloud-2.0-feign-orderartifactId>
		<version>0.0.1-SNAPSHOTversion>
	parent>
	<artifactId>springcloud-2.0-feign-order-serviceartifactId>

	<dependencies>
		<dependency>
			<groupId>com.minegroupId>
			<artifactId>springcloud-2.0-feign-order-apiartifactId>
			<version>${project.version}version>
		dependency>

		<dependency>
			<groupId>com.minegroupId>
			<artifactId>springcloud-2.0-feign-member-apiartifactId>
			<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客户端负载均衡,所以订单中心与会员中心都可以修改端口号启动多台实现负载均衡效果。

你可能感兴趣的:(#,feign)