参考网址:
http://blog.csdn.net/lc0817/article/details/53266212
环境:JDK1.8
工具:Eclipse
1.新建Maven Project
pom.xml:
<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>springcloudtest</groupId>
<artifactId>springcloudtest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>discovery</module>
<module>service0</module>
<module>service1</module>
<module>gateway</module>
</modules>
<!--以下dependency来自官方-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
</project>
2.新建Maven Module discovery
新建一个包:
cn.demo.discovery
在包里面新建一个Class:Discovery,代码如下:
package cn.demo.discovery;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class Discovery {
public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(Discovery.class, args);
}
}
在resources目录下新建一个配置文件application.yml(注意yml格式与properties文件不同):
配置信息如下(注意排版):
server:
port: 8080 #注册中心占用8080端口,冒号后面必须要有空格
eureka:
instance:
hostname: localhost #冒号后面必须要有空格
client:
registerWithEureka: false #冒号后面必须要有空格
fetchRegistry: false #冒号后面必须要有空格
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #其实就是 http://localhost:8080/eureka/,冒号后面必须要有空格
运行项目:
控制台输出信息:
浏览网页,http://localhost:8080/:
3.新建Maven Module service0
在springcloudtest项目中添加maven模块
新建一个Class:Service0,代码如下:
package service0;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class Service0 {
@GetMapping("/service0")
public String service0(){
return "service0";
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(Service0.class, args);
}
}
在resources目录下新建一个配置文件application.yml(注意yml格式与properties文件不同):
spring:
application:
name: service0 #冒号后面必须要有空格
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8080/eureka/ #冒号后面必须要有空格
# instance:
# hostname: localhost #冒号后面必须要有空格
# instance-id: http://localhost:8081 #冒号后面必须要有空格
server:
port: 8081 #冒号后面必须要有空格
先运行discovery项目,再运行本项目(同上):
浏览服务器,http://localhost:8080/,可以看到service0:
4.新建Maven Module service1
操作方法同service0
Service1.java,代码如下:
package service1;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class Service1 {
@GetMapping("/service1")
public String service1(){
return "service1";
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(Service1.class, args);
}
}
配置文件application.yml,配置信息如下:
spring:
application:
name: service1 #冒号后面必须要有空格
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8080/eureka/ #冒号后面必须要有空格
# instance:
# hostname: localhost #冒号后面必须要有空格
# instance-id: http://localhost:8082 #冒号后面必须要有空格
server:
port: 8082 #冒号后面必须要有空格
先运行discovery项目,再运行service0和service1(同上)。
浏览服务器,http://localhost:8080/,可以看到service0和service1:
4.新建Maven Module gateway(网关)
新建一个包cn.demo.gateway:
新建一个Class:Gatewa,代码如下:
package cn.demo.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
public class Gateway {
public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(Gateway.class, args);
}
}
配置文件application.yml,配置信息如下:
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8080/eureka/
spring:
application:
name: gateway
server:
port: 8083
zuul:
routes:
service0: /service/0/**
service1: /service/1/**
首先启动Discovery,其次的服务和网关启动顺序随意.通过访问http://localhost:8083/service0/service0/,即可看到,gateway帮我们转发了请求.