超简单的SpringClound入门教程(三.服务的消费者Feign)

上一篇,讲了SpringClound中的消费者采用织带+休息来实现,这回我们用组件Feign来实现服务的消费者,Fegin中也是默认集成了Ribbon的;和Eureka结合也能实现负载均衡;

概括来说,Fegin的区别就是基于注解来实现,具备可插拔的特性;

一,项目准备

这回我们要用的项目工程依然用Eureka,service-hello(一个项目,注册两个实例)

二,创建Fegin项目;

同样的我们创建一个SpringBoot的Model,命名service-fegin;

pom依赖如下:

<?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">
   <modelVersion>4.0.0</modelVersion>

   <groupId>com.example</groupId>
   <artifactId>demo</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>demo</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.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>
   </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>

properties配置如下;

#服务端口
server.port=8885

#注册服务中心地址
eureka.client.service-url.defaultZone=http://localhost:8880/eureka/

#注册服务名
spring.application.name=service-feign

启动类如下:

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients      //开启Feign的功能:
public class FeginApplication {

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

然后我们定义一个fegin的接口,在这个接口中我们通过@ FeignClient(“服务名”)来实现消费服务提供者;

//代表改接口用费"service-hello"的服务 提供
@FeignClient(value = "service-hello")
public interface FeginService {

    @RequestMapping(value = "/hello")
    String sayHelloFromService();

}

我们再在fegin项目中暴露一个访问接口,controller;

@RestController
public class FeginController {

    @Autowired
    private FeginService feginService;


    @RequestMapping("hello")
    public String helloFegin(){
        return feginService.sayHelloFromService();
    }

}

代码基本编写完成,下面我们来启动项目;Eureka,service-hello(两个实例),最后启动service-fegin;

然后后我们访问service-fegin的接口,结果如下;刷新页面;



看到我们访问fegin的接口,会去轮询调用service-hello的两个不同实例;来实现服务的消费者;

猜你喜欢

转载自blog.csdn.net/qq_41377914/article/details/80622240