使⽤IDEA搭建springcloud微服务(四)----微服务消费⽅
cloud-client
⼀、⼯具及说明
开发⼯具:IntelliJ IDEA 2018.2.2 (Ultimate Edition)
框架:spring boot 2.0.8、spring cloud Finchley.SR2
以通⽤户ID获取⽤户信息为例,搭建⼀套spring cloud微服务系统。
需要搭建⼀个⽗⼯程spring-cloud,⼀个服务注册中⼼eureka-server,两个微服务cloud-client,cloud-provider。
两个微服务均注册到服务注册中⼼。
⼆、微服务消费⽅的搭建
springboot结构消费⽅cloud-client的搭建,基本和服务⽅cloud-provider⼀样,还是写全吧。
1.File—>New—>Module
2.选择Spring Initializr,选择对应的JDK,
Choose Initializr Server URL 选择 default。
Next。
3.输⼊项⽬组Group:com.cloud。
组件名称Artifact:cloud-client。
Type:选择Maven Project。
修改⾃动⽣成的Package。
Next。
4.dependencies选择Cloud Discovery—>Eureka Discovery,Cloud Routing—>Feign。Spring Boot选择你需要的版本,我这选择2.0.8。
Next。
5.Project Name⼀般不做修改,和组件名称Artifact⼀样。
Content root、Module file location 均按⾃动⽣成,不做修改。
Finish。
6.配置。
将⾃动⽣成的application.properties更改为l⽂件,个⼈习惯使⽤yml⽂件。rename的快捷键是Shift+F6。
在l中加⼊以下配置:
server:
port: 8082
spring:
application:
name: cloud-client
eureka:
client:
serviceUrl:
defaultZone: user:123456@localhost:8080/eureka/      #服务注册中信地址,含有BASIC认证的⽤户名和密码  instance:
prefer-ip-address: true        #将IP注册到服务注册中⼼
#放开所有节点
management:
endpoints:
web:
exposure:
include: '*'
7.修改pom⽂件。
可以发现,pom⽂件中已⾃动引⼊了Eureka客户端、Feign模块依赖。
将按以下修改,使⽤⽗⼯程spring-cloud的spring boot依赖。
如果需要使⽤/health进⾏健康检查,则加⼊健康检查模块。
如果需使⽤Tomcat运⾏,需要加⼊tomcat⽀持模块和web模块。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 引⼊⽗⼯程的spring boot依赖 -->
<parent>
<groupId>com.cloud</groupId>
<artifactId>spring-cloud</artifactId>
<version>1.0</version>
</parent>
<groupId>com.cloud</groupId>
<artifactId>cloud-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cloud-client</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
&porting.outputEncoding>UTF-8</porting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
</properties>
<dependencies>
<!--web依赖-->
<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.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--Feign模块-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- tomcat⽀持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</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>${spring-cloud.version}</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>
</project>
8.启动类CloudClientApplication 。
在启动类上加⼊@EnableDiscoveryClient注解,声明该微服务注册到服务注册中⼼。加⼊@EnableFeignClients,声明使⽤Feign调⽤接⼝。