SpringCloud third chapter: the service consumer (Feign)

To undertake the removal of a ribbon

Need to start

  • EurekaService port number 8100
  • EurekaClient * 2 port numbers 8101,8102
  • feign service port number 8105
  1. Create a new project called service-feign SpringBoot

  2. Modify pom file to add the following dependency

    • spring-cloud-starter-openfeign
    • spring-boot-starter-web
    • spring-cloud-starter-netflix-eureka-client

    pom file as follows

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    >
    ><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.kthirty</groupId>
    > <artifactId>springboot-parent</artifactId>
    > <version>0.0.1-SNAPSHOT</version>
    > <relativePath/>
    > </parent>
    > <artifactId>service-feign</artifactId>
    > <version>0.0.1-SNAPSHOT</version>
    > <packaging>jar</packaging>
    > <name>service-feign</name>
    > <description>Demo project for Spring Boot</description>
    > <dependencies>
    > <dependency>
    > <groupId>org.springframework.boot</groupId>
    > <artifactId>spring-boot-starter-web</artifactId>
    > </dependency>
    > <dependency>
    > <groupId>org.springframework.cloud</groupId>
    > <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    > </dependency>
    > <dependency>
    > <groupId>org.springframework.cloud</groupId>
    > <artifactId>spring-cloud-starter-openfeign</artifactId>
    > </dependency>
    > <dependency>
    > <groupId>org.springframework.boot</groupId>
    > <artifactId>spring-boot-starter-test</artifactId>
    > <scope>test</scope>
    > </dependency>
    > </dependencies>
    ></project>
    >
  3. Application.yml modify the configuration file, the full file registration services as follows

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    > the Spring: 
    > the Application:
    > name: Service-Feign
    >
    > Server:
    > large column  SpringCloud third chapter: the service consumer (Feign) Port: 8105
    > Eureka:
    > Client:
    > Service-url:
    > defaultzone: HTTP: localhost //: 8100 / Eureka /
    >
  4. Modify the startup class

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    >package com.kthirty.servicefeign;
    >
    >import org.springframework.boot.SpringApplication;
    >import org.springframework.boot.autoconfigure.SpringBootApplication;
    >import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    >import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    >import org.springframework.cloud.openfeign.EnableFeignClients;
    >
    >@SpringBootApplication
    >@EnableDiscoveryClient
    >@EnableEurekaClient
    >@EnableFeignClients //开启Feign功能
    >public class {
    >
    > public static void main(String[] args) {
    > SpringApplication.run(ServiceFeignApplication.class, args);
    > }
    >}
    >
  5. 添加feign接口

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    >package com.kthirty.servicefeign.interfaces;
    >
    >import org.springframework.cloud.openfeign.FeignClient;
    >import org.springframework.web.bind.annotation.RequestMapping;
    >import org.springframework.web.bind.annotation.RequestMethod;
    >import org.springframework.web.bind.annotation.RequestParam;
    >
    >@FeignClient("client1")//指定调用服务名
    >public interface SchedualServiceHi {
    > @RequestMapping(value = "/hello",method = RequestMethod.GET)//服务链接
    > String sayHiFormClientOne(@RequestParam String name);
    >}
    >
  6. 创建HiController消费服务

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    >package com.kthirty.servicefeign.web;
    >
    >import com.kthirty.servicefeign.interfaces.SchedualServiceHi;
    >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;
    >
    >@RestController
    >public class HiController {
    > //编译器报错,无视。 因为这个Bean是在程序启动的时候注入的,编译器感知不到,所以报错。
    > @Autowired
    > private SchedualServiceHi schedualServiceHi;
    > @RequestMapping("/hi")
    > public String sayHi(@RequestParam String name){
    > return schedualServiceHi.sayHiFormClientOne(name);
    > }
    >}
    >

启动测试

同上一篇将Ribbon服务替换为feign服务即可

访问链接[http://localhost:8105/hi?name=thirty]:http://localhost:8105/hi?name=thirty

Guess you like

Origin www.cnblogs.com/sanxiandoupi/p/11713221.html