目录
1.创建maven项目
2.idea打开maven项目
3.修改pom.xml文件
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lizz</groupId>
<artifactId>eureka-consumer-feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-consumer-feign</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RC2</spring-cloud.version>
</properties>
<dependencies>
<!--web项目 默认使用内置tomcat-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--feign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--test 相关jar包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<!--spring cloud版本依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<!--打包生成的文件名及路径-->
<finalName>../eureka-consumer-feign</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<!--maven远程仓库-->
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>
4.创建yml配置文件
在src/main/resources目录下创建application.yml文件(使用application.properties也可以)
spring:
application:
name: eureka-consumer-feign #服务名
server:
port: 8603 #服务访问端口
servlet:
context-path: / #访问路径
eureka:
instance: #实例
prefer-ip-address: true #是否使用IP地址进行访问
#实例显示名
instanceId: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@
client:
serviceUrl:
#Eureka Server服务集群
defaultZone: http://192.168.180.15:9991/eureka/,http://192.168.180.15:9992/eureka/,http://192.168.180.15:9993/eureka/
5.创建Feign Service
创建 com/lizz/eurekaconsumerfeign/client/HiFeignService.java
package com.lizz.eurekaconsumerfeign.client;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* Created by zhenzhong.li on 19/1/14.
*/
//绑定需要调用微服务
@FeignClient("eureka-provider")
public interface HiFeignService {
// 调用"eureka-provider"服务下的"provider/{name}/hi"接口
// 此位置的mapping与controller上的mapping是共享全局唯一的,
// 如果相同会报错ambiguous mapping
@RequestMapping("provider/{name}/hi")
String getHiFeign(@PathVariable(value = "name") String name);
}
6.创建消费服务
创建文件.../controller/ConsumerFeignController.java
package com.lizz.eurekaconsumerfeign.controller;
import com.lizz.eurekaconsumerfeign.client.HiFeignService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* ConsumerFeignController
*
* @author zhenzhong.li
* @date 19/1/3
*/
@RestController
@RequestMapping(value = "/consumer-feign/", produces = "application/json; charset=utf-8")
public class ConsumerFeignController {
private static final Logger logger = LoggerFactory.getLogger(ConsumerFeignController.class);
@Autowired
private HiFeignService hiFeignService;//注入微服务Feign Client接口
@RequestMapping("getHiFeign/{name}")
public String getHiFeign(@PathVariable String name) {
logger.info("*********getHiFeign name:{}",name);
//调用微服务接口
String msg=hiFeignService.getHiFeign(name);
logger.info("*********getHiFeign msg:{}",msg);
return msg;
}
}
7.开启Eureka消费者服务
在启动文件中增加@EnableDiscoveryClient或@EnableEurekaClient标签,开启服务发现Client,增加@EnableFeignClients标签,开启Feign Client。
执行main方法,启动程序。
EnableDiscoveryClient:Spring Cloud内置通用服务发现客户端
EnableEurekaClient:Eureka特定服务发现客户端
package com.lizz.eurekaconsumerfeign;
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 //开启服务发现 client
@EnableFeignClients //开启feign client
public class EurekaConsumerFeignApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaConsumerFeignApplication.class, args);
}
}
8.查看Eureka控制台
注册成功,其他Eureka服务信息一致。