总的pom如下:
pom.xml
eureka-server的pom.xml
one-service的pom.xml
two-service的pom.xml
zuul-service的pom.xml
配置文件:
euraka注册中心:
eureka-server:
application.yml
#端口号
server:
port: 8888
#Eureka实例名,集群中根据这里相互识别
eureka:
instance:
hostname: eureka-server
#客户端
client:
service-url:
defaultZone: http://localhost:8889/eureka/
server:
#是否开启安全保护,默认是开启的,如果默认开启,注册中心的服务列表就算有些服务断开了,也会继续保存
enable-self-preservation: false
spring:
application:
name: eureka-service
服务1,服务2通过euraka在注册中心eureka-server注册,
服务1:
one-service:
application.yml
#端口号
server:
port: 8083
#Eureka实例名,集群中根据这里相互识别
spring:
application:
name: eureka-service
eureka:
#客户端
client:
#注册中心地址
service-url:
defaultZone: http://localhost:8888/eureka/
服务2:
two-service:
application.yml
#端口号
server:
port: 8082
#Eureka实例名,集群中根据这里相互识别
spring:
application:
name: eureka-service
eureka:
#客户端
client:
#注册中心地址
service-url:
defaultZone: http://localhost:8888/eureka/
服务zuul:
zuul-service:
application.yml
#端口号
server:
port: 8084
#Eureka实例名,集群中根据这里相互识别
spring:
application:
name: zuul-service
eureka:
#客户端
client:
#注册中心地址
service-url:
defaultZone: http://localhost:8888/eureka/
zuul:
routes:
eureka-service: /api-a/**
启动类:
eureka-server的启动类
EurekaServerApplication.java
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication{
public static void main(String[] args){
SpringApplication.run(EurekaServerApplication.class,args);
}
}
one-service的启动类
OneServiceApplication.java
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @SpringCloudApplication注解,它整合了@SpringBootApplication、@EnableEurekaClient、@EnableCircuitBreaker,主要目的还是简化配置。
* 这里暂时需要 @SpringBootApplication、@EnableEurekaClient
*/
@SpringCloudApplication
public class OneServiceApplication {
public static void main(String[] args) {
SpringApplication.run(OneServiceApplication.class, args);
}
}
two-service的启动类
TwoServiceApplication.java
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @SpringCloudApplication注解,它整合了@SpringBootApplication、@EnableEurekaClient、@EnableCircuitBreaker,主要目的还是简化配置。
* 这里暂时需要 @SpringBootApplication、@EnableEurekaClient
*/
@SpringCloudApplication
public class TwoServiceApplication{
public static void main(String[] args){
SpringApplication.run(TwoServiceApplication.class,args);
}
}
zuul-service的启动类
ZuulServiceApplication.java
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
/**
* @SpringCloudApplication注解,它整合了@SpringBootApplication、@EnableEurekaClient、@EnableCircuitBreaker,主要目的还是简化配置。
* 这里暂时需要 @SpringBootApplication、@EnableEurekaClient、@EnableZuulProxy
*/
@EnableZuulProxy
@SpringCloudApplication
public class ZuulServiceApplication{
public static void main(String[] args){
SpringApplication.run(ZuulServiceApplication.class,args);
}
}
对one-service,two-service服务测试
one-service:
TestController.java
package com.example;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController{
@RequestMapping(value = "/test" ,method = RequestMethod.GET)
public String test() {
return "==============>>>>>>>hello,I am springCloud one-service<<<<<<<================="; //测试代码直接返回一个字符串,不再调用service层等等。
}
}
two-service:
Testcontroller.java
package com.example;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController{
@RequestMapping(value = "/test" ,method = RequestMethod.GET)
public String test() {
return "==============>>>>>>>hello,I am springCloud two-service<<<<<<<================="; //测试代码直接返回一个字符串,不再调用service层等等。
}
}
部分效果
项目目录结构:
eureka服务的注册
service-one,service-two两个服务的测试
zuul轮询结果