springcloud(一):记录下搭建Eureka,zuul的使用
我们都知道springcloud架构一般是网关,注册中心,还有一些业务服务,原来都是项目上直接用,今天就把搭建过程写一下,方便自己以后查询。首先搭建一个Eureka的server端 test-eureka:在pom中引入依赖:<dependencies><dependency><groupId>org.springframework.cloud</grou
·
我们都知道springcloud架构一般是网关,注册中心,还有一些业务服务,原来都是项目上直接用,今天就把搭建过程写一下,方便自己以后查询。
首先搭建一个Eureka的server端 test-eureka:
在pom中引入依赖:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.0.0.M8</version>
</dependency>
</dependencies>
写一个eureka server的服务
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class TestEurekaServer {
public static void main(String[] args) {
SpringApplication.run(TestEurekaServer.class, args);
}
}
配置文件如下:
server:
port: 8761
spring:
application:
name: test-eureka
eureka:
instance:
hostname: localhost
client:
# eureka server自身也会注册到eureka中
registerWithEureka: false # 是否向eureka注册自身服务
fetchRegistry: false # 是否检索服务
serviceUrl:
defaultZone: http://${eureka.instance.hostname:localhost}:${server.port:8761}/eureka/
server:
eviction-interval-timer-in-ms: 1000 #设置清理的间隔时间(默认6000ms)
enable-self-preservation: false #关闭保护模式
再写一个zuul代理服务test-zuul:
pom依赖:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
<version>2.0.0.M8</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.0.M8</version>
</dependency>
<dependency>
<groupId>com.netflix.servo</groupId>
<artifactId>servo-core</artifactId>
<version>0.12.7</version>
</dependency>
</dependencies>
启动类:
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
@EnableEurekaClient
@EnableZuulProxy
public class TestZuulApplication {
public static void main(String[] args) {
SpringApplication.run(TestZuulApplication.class, args);
}
}
配置文件:
server:
port: 9000
eureka:
client:
serviceUrl:
fetch-registry: true
register-with-eureka: true
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: test-zuul
zuul:
routes:
test-controller:
path: /** # 服务的路径前缀
serviceId: test-controller # 把test-controller这个服务路由转发
再写一个业务服务test-controller:
package test.controller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class TestControllerApplication {
public static void main(String[] args) {
SpringApplication.run(TestControllerApplication.class, args);
}
}
package test.controller.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("v1")
@RestController
public class Controller1 {
@GetMapping("hello")
@ResponseBody
public String hello() {
return "hello world";
}
}
从zuul服务访问:后正常返回test-controller接口
更多推荐
已为社区贡献1条内容
所有评论(0)