您的位置:首页 > 编程语言 > Java开发

springcloud服务与服务之间的调用(Feign)

2020-04-19 21:15 525 查看
  • springcloud是什么?

           Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智能路由,控制总微代理,线,一次性令牌,全局锁定,领导选举,分布式会话,集群状态)。分布式系统的协调导致锅炉板模式,使用Spring Cloud开发人员可以快速站起来实现这些模式的服务和应用程序。它们适用于任何分布式环境,包括开发人员自己的笔记本电脑,裸机数据中心和Cloud Foundry等托管平台。

  • 什么分布式系统?

         分布式就是将一个系统拆分成很多的子系统,比如淘宝系统,他有许许多多的子系统:会员系统、订单系统、积分系统等等,这样已拆分就大大的减轻了服务器的压力,如果想深入了解分布式的架构,还请百度分布式架构,这里不多做说明。

  • 什么是Feign

          既然一个系统被拆分成了无数个子系统,那系统之间是如何进行通信的呢?假如我现在在订单系统中想获取到会员的信息,此时,你能怎么做?

          可能你会说用外链接查询,这个也没啥错,但是正规的分布式系统都是将对应的表分给了对应的库,所以这里链接查询是走不通的,这时,你可能在想,要是我能直接调用会员服务的方法那该多好,Feign就来了,他解决了服务与服务之间的通讯。

          Feign是一种声明式、模板化的HTTP客户端。在Spring Cloud中使用Feign, 我们可以做到使用HTTP请求远程服务时能与调用本地方法一样的编码体验,开发者完全感知不到这是远程方法,更感知不到这是个HTTP请求,类似于Dubbo的RPC。

  •  eureka

        如果想实现服务与服务之前的调用,那么必须要有eureka这个注册中心,eureka在这里不多做解释,直接看代码:

      1.创建一个eureka-service的springboot项目。

                    
       2.maven依赖:     

[code]<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.ymy</groupId>
<artifactId>sprintcloud</artifactId>
<version>0.0.1-SNAPSHOT</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<!--eureka server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<!-- spring boot test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>

不需要的jar可以不引用。

3.添加配置文件:

[code]server:
port: 8769
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

这样eureka服务算是大功告成。

  • 服务端1(被调用端1)

      创建一个springboot项目:

                     

       pom文件:
            

[code]<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.ymy.client1</groupId>
<artifactId>cloud-client1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>boot1</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

</project>

    新建配置文件:application.yml

       

[code]eureka:
client:
serviceUrl:
defaultZone: http://localhost:8769/eureka/
server:
port: 8762
spring:
application:
name: service-hi

启动类:我将controller直接写在了启动类了,你们可以新建一个controller包

[code]package com.ymy;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@EnableEurekaClient
@SpringBootApplication
@RestController
@RequestMapping("c")
public class App1 {

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

@Value("${server.port}")
String port;
@GetMapping("hello")
public String hello(@RequestParam String name){

return "hello "+name+" 我的服务端口是:"+port;
}
}
  • 服务端2(被调用端2)

      新建一个springboot项目:

           

        pom文件:
 

[code]<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.ymy.client1</groupId>
<artifactId>cloud-client1</artifactId>
<version>0.0.1-SNAPSHOT</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

</project>

新建配置文件:application.yml

[code]eureka:
client:
serviceUrl:
defaultZone: http://localhost:8769/eureka/
server:
port: 8763
spring:
application:
name: service-hi

启动类:

    

[code]package com.ymy;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@EnableEurekaClient
@SpringBootApplication
@RestController
@RequestMapping("c")
public class App2 {

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

@Value("${server.port}")
String port;
@GetMapping("hello")
public String hello(@RequestParam String name){

return "hello "+name+" 我的服务端口是:"+port;
}
}
  • 客服端(调用端)

新建一个springboot项目

     

pom配置文件:
 

[code]<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.ymy.client3</groupId>
<artifactId>cloud-client3</artifactId>
<version>0.0.1-SNAPSHOT</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

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

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 断路器 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
</dependencies>

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

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

<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

</project>

 

新建application.yml文件
    

[code]eureka:
client:
serviceUrl:
defaultZone: http://localhost:8769/eureka/
server:
port: 8765
spring:
application:
name: service-feign
feign:
hystrix:
enabled: true

controller类:

[code]package com.ymy.controller;

import javax.inject.Inject;

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

import com.ymy.feign.ClientFeign;
import com.ymy.feign.HystrixClientFeign;
@RestController
public class MyController {

@Inject
private ClientFeign clientFeign;
//	@Autowired
//	private HystrixClientFeign hystrixClientFeign;

@GetMapping("hi")
public String hi(@RequestParam String name){
return clientFeign.getHello(name);
}

/*@GetMapping("hihi")
public String hihi(@RequestParam String name){
System.out.println("哈哈哈哈");
return hystrixClientFeign.hello(name);
}*/
}

重点来了,Feign的运用:

ClientFeign类:

[code]package com.ymy.feign;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(value = "service-hi", fallback = HystrixClientFeign.class)
public interface ClientFeign {
@GetMapping("c/hello")
String getHello(@RequestParam(value = "name") String name);
}

HystrixClientFeign类:

  

[code]package com.ymy.feign;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

@Component
public class HystrixClientFeign implements ClientFeign{

//	@Autowired
//	private RestTemplate  restTemplate;

//	@HystrixCommand(defaultFallback="hiError")
//	public String hello(String name){
//		return restTemplate.getForObject("http://service-hi/hello?name="+name, String.class);
//	}

/*public String hiError(String name) {
return "hi," + name + ",sorry,error!";
}*/

@Override
public String getHello(String name) {
return "hello " + name + ", error!";
}
}

代码已经贴的差不多了,下面我们来看运行结果:

 

两个服务端我做的是集群,客服端以轮询的方式访问,有兴趣的小伙伴可以试试哦,需要源码的可以留言哦。

  • 点赞 2
  • 收藏
  • 分享
  • 文章举报
卖托儿索的小火柴 发布了47 篇原创文章 · 获赞 308 · 访问量 2万+ 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: