一、创建一个maven工程
在pom文件中引入依赖,Spring Boot 版本为2.0.3.RELEASE,Spring Cloud版本为Finchley.RELEASE。这个pom文件作为父pom文件,起到依赖版本控制的作用,其他module工程继承该pom。代码如下:
<?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>
<groupId>com.xiaoshi.hyr</groupId>
<artifactId>hyr-tranvel</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>hyr-tranvel</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/>
</parent>
<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.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<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>
<!-- hystrix 依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<!-- zuul 依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<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>
二、创建服务注册中心
2.1、创建一个model工程,作为服务注册中心,命名为 hyr-eureka-server
创建完后的工程,其pom.xml继承了父pom文件,并引入spring-cloud-starter-netflix-eureka-server的依赖,代码如下:
<?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>com.xiaoshi.hyr</groupId>
<artifactId>hyr-tranvel</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.xiaoshi.hyr</groupId>
<artifactId>hyr-eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hyr-eureka-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</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>
2.2、其配置文件如下 appication.yml:
server:
port: 5100
spring:
profiles:
active: dev
application:
#表示注册到eureka的服务名
name: hyr-eureka-server
eureka:
instance:
hostname: localhost
client:
#表示是否将自己注册到 hyr-eureka-server 默认为true
register-with-eureka: false
#表示是否从 hyr-eureka-server 获取注册信息, 默认为true
fetch-registry: false
service-url:
#表示注册到 hyr-eureka-server 的地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
2.3、在启动类添加注解 @EnableEurekaServer
package com.xiaoshi.hyr.hyreurekaserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class HyrEurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(HyrEurekaServerApplication.class, args);
}
}
2.4、启动项目,打开浏览器访问:http://localhost:5100/ ;界面如下:
因为没有注册服务,所以不可能有服务被发现
三、创建一个服务提供者 hyr-server-order(也可以是消费者)
3.1、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>com.xiaoshi.hyr</groupId>
<artifactId>hyr-tranvel</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.xiaoshi.hyr</groupId>
<artifactId>hyr-server-order</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hyr-server-order</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</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>
3.2、配置文件 appication.yml 如下:
server:
port: 5200
spring:
profiles:
active: dev
application:
#表示注册到eureka的服务名
name: hyr-server-order
eureka:
client:
service-url:
#表示注册到 hyr-eureka-server 的地址
defaultZone: http://localhost:5100/eureka/
3.3、主类中添加 @EnableDiscoveryClient 注解
package com.xiaoshi.hyr.hyrserverorder;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class HyrServerOrderApplication {
public static void main(String[] args) {
SpringApplication.run(HyrServerOrderApplication.class, args);
}
}
3.4、添加一个controller
package com.xiaoshi.hyr.hyrserverorder.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String sayHello(){
return "我是订单服务";
}
}
3.5、启动项目 ,打开浏览器访问:http://localhost:5100/
这时,服务名为HYR-SERVER-ORDER ,端口号为5200 的服务已经注册在服务中了;
这时,访问 http://localhost:5200/hello
四、创建服务消费者,这里使用 hyr-server-user (可以是服务提供者)
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>com.xiaoshi.hyr</groupId>
<artifactId>hyr-tranvel</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.xiaoshi.hyr</groupId>
<artifactId>hyr-server-order</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hyr-server-order</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</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>
<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>
5.2、配置文件 appication.yml 如下:
server:
port: 5300
spring:
profiles:
active: dev
application:
#表示注册到eureka的服务名
name: hyr-server-user
eureka:
client:
service-url:
#表示注册到 hyr-eureka-server 的地址
defaultZone: http://localhost:5100/eureka/
5.3、主类中添加 @EnableDiscoveryClient 注解
package com.xiaoshi.hyr.hyrserveruser;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class HyrServerUserApplication {
public static void main(String[] args) {
SpringApplication.run(HyrServerUserApplication.class, args);
}
}
5.4、添加controller
package com.xiaoshi.hyr.hyrserveruser.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Value("${server.port}")
private String port;
@RequestMapping("/hello")
public String sayHello(){
return "我是订单服务 , " + "port : " + port;
}
}
5.5、启动项目 ,打开浏览器访问:http://localhost:5100/
这时,服务名为HYR-SERVER-ORDER ,端口号为5200 的服务已经注册在服务中了;
这时,访问 http://localhost:5300/hello
六、服务调用,使用 feign 调用;hyr-server-order 调用 hyr-server-user
6.1、改造 hyr-server-order 服务;
pom.xml 增加依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
6.2、添加一个HelloServer接口
package com.xiaoshi.hyr.hyrserverorder.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
// hyr-provider-user 表示服务提供者的名字
@FeignClient("hyr-server-user")
public interface HelloService {
//服务提供者的接口地址
@RequestMapping("/hello")
String hello();
}
6.3、改造controller
package com.xiaoshi.hyr.hyrserverorder.controller;
import com.xiaoshi.hyr.hyrserverorder.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Value("${server.port}")
private String port;
@Autowired
HelloService helloService;
@RequestMapping("/feign")
public String hello(){
return helloService.hello();
}
@RequestMapping("/hello")
public String sayHello(){
return "我是订单服务";
}
}
6.4 通过 hyr-server-order 去调用 hyr-server-user ; 浏览器访问 : http://localhost:5200/feign :如下,调用成功;
七、在 hyr-server-order 中加入 Hystrix 功能;
pom.xml 中添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
改造 controller ,这里设置休眠模拟请求超时;
package com.xiaoshi.hyr.hyrserverorder.controller;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.xiaoshi.hyr.hyrserverorder.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Value("${server.port}")
private String port;
@Autowired
HelloService helloService;
@RequestMapping("/feign")
//表示如果调用接口超时,则开启服务降级
@HystrixCommand(fallbackMethod = "fallback")
public String hello() throws InterruptedException {
Thread.sleep(5000);
return helloService.hello();
}
public String fallback() {
return "fallback";
}
@RequestMapping("/hello")
public String sayHello(){
return "我是订单服务";
}
}
再次访问 :http://localhost:5200/feign ;
八、添加 zuul 功能
8.1、创建项目 hyr-zuul ,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>com.xiaoshi.hyr</groupId>
<artifactId>hyr-tranvel</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.xiaoshi.hyr</groupId>
<artifactId>hyr-zuul</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hyr-zuul</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</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-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
8.2、配置文件 appication.yml 如下:
server:
port: 5400
spring:
profiles:
active: dev
application:
#表示注册到eureka的服务名
name: hyr-zuul
eureka:
client:
service-url:
#表示注册到 hyr-eureka-server 的地址
defaultZone: http://localhost:5100/eureka/
zuul:
routes:
api-order:
path: /api-order/**
serviceId: hyr-server-order
api-user:
path: /api-user/**
serviceId: hyr-server-user
8.3、自定义 MyZuulFilter 继承 ZuulFilter,如下:
package com.xiaoshi.hyr.hyrzuul;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
/**
* 自定义zuul 过滤请求
*/
@Component
public class MyZuulFilter extends ZuulFilter {
/**
* filterType : 返回一个字符串代表过滤类型,在zuul中定义了 4 种
* pre : 路由之前
* routing :路由之时
* post :路由之后
* error : 发送错误调用
* @return
*/
@Override
public String filterType() {
return "pre";
}
/**
* filterOrder 过滤的顺序
* @return
*/
@Override
public int filterOrder() {
return 0;
}
/**
* shouldFilter 表示是否过滤,可以写判断逻辑;true 代表永远过滤
* @return
*/
@Override
public boolean shouldFilter() {
return true;
}
/**
* 过滤的具体逻辑代码
* @return
*/
@Override
public Object run() {
RequestContext currentContext = RequestContext.getCurrentContext();
HttpServletRequest request = currentContext.getRequest();
Object token = request.getParameter("token");
if(token == null || token == ""){
currentContext.setSendZuulResponse(false);
currentContext.setResponseStatusCode(401);
try {
currentContext.getResponse().setContentType("text/html;charset=UTF-8");
currentContext.getResponse().getWriter().write(" token 为空 !!!");
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
return null;
}
}
8.4、主类添加 @EnableZuulProxy 注解,如下:
package com.xiaoshi.hyr.hyrzuul;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableZuulProxy
@EnableDiscoveryClient
public class HyrZuulApplication {
public static void main(String[] args) {
SpringApplication.run(HyrZuulApplication.class, args);
}
}
8.5、启动项目 ,浏览器访问 http://localhost:5400/api-user/hello
这里没有 token ,所以被拦截了
加上 token 信息再次访问:http://localhost:5400/api-user/hello?token=sdj ,访问成功
访问:http://localhost:5400/api-order/hello?token=sdj ,访问成功
以上是自己的一点学习心得,如有错误的地方,还请指正,谢谢