Spring Cloud Spring Cloud

spring cloud 构建微服务一(eureka)服务注册与

2018-02-08  本文已影响72人  阿波罗程序猿

spring cloud的由来就不说了,可以查看官网。昨天刚刚上手spring cloud这里记录下我的学习过程,算是做一个知识储备,等以后业务增长为拆分微服务做准备。

spring cloud 版本

spring boot 与 spring cloud 的关系

创建 Eureka 服务发现

Eureka是属于Netflix OSS componentsNetflix开源软件组件中的一种,专门用来进行服务发现的组件。使用Eureka可以实现实例的注册,并且客户端可以通过spring管理的beans发现该实例。

应用名称 eureka-server eureka-client eureka-customer
端口 9000-9099 9100-9199 9200-9299
说明 提供注册服务 服务本身 消费服务
创建eureka-server 项目。使用SPRING INITIALIZR初始化项目。
初始化 eureka-server 项目
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
 * @author 张博
 * @since 2018/2/7 下午3:59
 */
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}
# 服务注册中心
spring.application.name=eureka-server
server.port=9000

eureka.instance.hostname=localhost
# 指示此实例是否向该服务注册,并被其它 eureka server 发现
eureka.client.register-with-eureka=false
# 指示此客户端是否从 eureka server 上获取 eureka 注册表信息
eureka.client.fetch-registry=false
# 设为false,关闭自我保护,默认为true
eureka.server.enable-self-preservation=true
# 清理间隔(单位毫秒,默认是0 不清除)
eureka.server.eviction-interval-timer-in-ms=1000
#eureka.client.serviceUrl.defaultZone=http://127.0.0.1:9001/eureka/
#EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.
创建eureka-client 项目。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 张博
 */
@RestController
public class UserController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping(value = "/getServices/{port}", method = RequestMethod.GET)
    public String getServices(@PathVariable String port) {
        String str = "" + discoveryClient.getServices();
        return str.concat(": ").concat(port);
    }
}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApplication.class, args);
    }
}
# 服务提供者
spring.application.name=eureka-client
server.port= 9100

# 把服务注册到 eureka-server 中
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:9000/eureka/
[eureka-client]: 9100
复制刚才创建eureka-client项目。
# 服务提供者
spring.application.name=eureka-client
server.port=9101

eureka.client.serviceUrl.defaultZone=http://127.0.0.1:9000/eureka/
可以看到,相同的应用名下,挂着2个服务

下节我们来看看如何实现客户端负载均衡的操作。

上一篇下一篇

猜你喜欢

热点阅读