一、工具及说明
开发工具: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。
两个微服务均注册到服务注册中心。
二、微服务服务方的搭建
-
File—>New—>Module
-
选择Spring Initializr,选择对应的JDK,
Choose Initializr Server URL 选择 default。
Next。
-
输入项目组Group:com.cloud。
组件名称Artifact:cloud-provider。
Type:选择Maven Project。
修改自动生成的Package。
Next。
-
dependencies选择Cloud Discovery—>Eureka Discovery。
Spring Boot选择你需要的版本,我这选择2.0.8。
Next。
-
Project Name一般不做修改,和组件名称Artifact一样。
Content root、Module file location 均按自动生成,不做修改。
Finish。
-
配置。
将自动生成的application.properties更改为application.yml文件,个人习惯使用yml文件。
rename的快捷键是Shift+F6。
在application.yml中加入以下配置:server: port: 8081 spring: application: name: cloud-provider eureka: client: serviceUrl: defaultZone: http://user:123456@localhost:8080/eureka/ #服务注册中信地址,含有BASIC认证的用户名和密码 instance: prefer-ip-address: true #将IP注册到服务注册中心 #放开所有节点 management: endpoints: web: exposure: include: '*'
-
修改pom文件。
可以发现,pom文件中已自动引入了Eureka客户端依赖。
将按以下修改,使用父工程spring-cloud的spring boot依赖。
如果需要使用/health进行健康检查,则加入健康检查模块。
如果需使用Tomcat运行,需要加入tomcat支持模块和web模块。<?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> <!-- 引入父工程的spring boot依赖 --> <parent> <groupId>com.cloud</groupId> <artifactId>spring-cloud</artifactId> <version>1.0</version> </parent> <groupId>com.cloud</groupId> <artifactId>cloud-provider</artifactId> <version>0.0.1-SNAPSHOT</version> <name>cloud-provider</name> <description>Demo project for Spring Boot</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.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> <!-- Eureka客户端模块 --> <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> <!-- 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>
-
启动类CloudProviderApplication。
在启动类上加入@EnableDiscoveryClient注解,声明该微服务注册到服务注册中心。import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @EnableDiscoveryClient @SpringBootApplication public class CloudProviderApplication { public static void main(String[] args) { SpringApplication.run(CloudProviderApplication.class, args); } }
-
使用Tomcat启动,需创建类ServletInitializer。
import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; public class ServletInitializer extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(CloudProviderApplication.class); } }
-
创建获取用户信息的接口
10.1 项目结构
10.2 UserControllerimport com.cloud.provider.entity.User; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @GetMapping("/{id}") public User findById(@PathVariable Long id) { User findOne = new User(); if (id == 1) { findOne.setAge(20); findOne.setName("zhangsan"); findOne.setUsername("zhangsan"); findOne.setId(1L); findOne.setBalance(800D); } else { findOne.setAge(18); findOne.setName("lisi"); findOne.setUsername("lisi"); findOne.setId(2L); findOne.setBalance(2000D); } return findOne; } }
10.3 User
public class User { private Long id; private String username; private String name; private Integer age; private Double balance; public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } public String getUsername() { return this.username; } public void setUsername(String username) { this.username = username; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public Integer getAge() { return this.age; } public void setAge(Integer age) { this.age = age; } public Double getBalance() { return this.balance; } public void setBalance(Double balance) { this.balance = balance; } }