springcloud(一):记录下搭建Eureka,zuul的使用

我们都知道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接口

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值