承接上一篇去除ribbon
需要启动
- EurekaService 端口号8100
- EurekaClient*2 端口号8101、8102
- feign服务 端口号8105
新建一个SpringBoot项目名为service-feign
修改pom文件添加以下依赖
- spring-cloud-starter-openfeign
- spring-boot-starter-web
- spring-cloud-starter-netflix-eureka-client
pom文件如下
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>
>修改application.yml配置文件,注册服务完整文件如下
1
2
3
4
5
6
7
8
9
10
11>spring:
> application:
> name: service-feign
>
>server:
> 大专栏 SpringCloud第三章节:服务消费者(Feign) port: 8105
>eureka:
> client:
> service-url:
> defaultZone: http://localhost:8100/eureka/
>修改启动类
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;
>
>
>
>
>//开启Feign功能
>public class {
>
> public static void main(String[] args) {
> SpringApplication.run(ServiceFeignApplication.class, args);
> }
>}
>添加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;
>
>"client1")//指定调用服务名 (
>public interface SchedualServiceHi {
> "/hello",method = RequestMethod.GET)//服务链接 (value =
> String sayHiFormClientOne(@RequestParam String name);
>}
>创建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;
>
>
>public class HiController {
> //编译器报错,无视。 因为这个Bean是在程序启动的时候注入的,编译器感知不到,所以报错。
>
> private SchedualServiceHi schedualServiceHi;
> "/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