美文网首页
3 SpringCloud服务注册与发现:Eurefa

3 SpringCloud服务注册与发现:Eurefa

作者: lijiaccy | 来源:发表于2018-01-24 21:02 被阅读0次

服务发现的架构图

引用dubbo的图

Eurefa的Server

创建项目





然后一直下一步等待项目创建好。


创建好的结构

在配置文件中加上

server.port=8888
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

在启动类EruefaServerApplication上加上@EnableEurekaServer

@SpringBootApplication
@EnableEurekaServer
public class EruefaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EruefaServerApplication.class, args);
    }
}

启动该类,在浏览器中输入 http://localhost:8888

Eurefa的Client

以上面相同的方式新建一个项目
修改配置文件

spring.application.name=helloService
eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/

在启动类上加上@EnableEurekaClient,然后启动该类
刷新 http://localhost:8888


这个就正常访问了。

eurefa服务高可用(集群)

首先在C:\Windows\System32\drivers\etc的HOSTS文件里面加上,没有的HOSTS自己创建一个

127.0.0.1   pee1
127.0.0.1   pee2

不知道要不要重启,我这随手重启了。

然后创建一个项目eurefa_cluster-server,目录结构是


主要是配置文件
application-pee1.properties
spring.application.name=eureka-server
server.port=1111
eureka.instance.hostname=pee1
eureka.client.serviceUrl.defaultZone=http://pee2:2222/eureka/

application-pee2.properties

spring.application.name=eureka-server
server.port=2222
eureka.instance.hostname=pee2
eureka.client.serviceUrl.defaultZone=http://pee1:1111/eureka/

AppClusterServer

@SpringBootApplication
@EnableEurekaServer
public class AppClusterServer {
    public static void main(String[] args) {
        SpringApplication.run(AppClusterServer.class,args);
    }
}

然后打jar包。
开两个cmd窗口,都进入target目录。
然后其中一个窗口执行

 java -jar eurefa_cluster-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=pee1

另一个窗口执行

 java -jar eurefa_cluster-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=pee2

会出现



分别访问

 http://pee1:1111
 http://pee2:2222

会发现


已经连接上了,Application名字都是EUREKA-SERVER.。Status中两台可用
当断掉其中一台pee2,然后刷新pee1:1111



Status中只剩一台1111。
这个集群如何使用的我慢慢学吧。

代码放在https://github.com/lijiaccy/springcloud

相关文章

网友评论

      本文标题:3 SpringCloud服务注册与发现:Eurefa

      本文链接:https://www.haomeiwen.com/subject/cuebbxtx.html