Eureka服务治理

Eureka是什么,SpringCloud用Eureka来干什么等等相关概念就不细说了,网上有大把大把的解释,甚至下面的文章都不需要看,因为网上也有大把大把的列子,为了省去搜索的时间,可以参考:
Spring资料大全 中的SpringCloud相关文档。本文的目的只是自己记录一下

目的

  • Eureka服务中心搭建
  • 其他服务注册到Eureka服务中心
  • Eureka服务中心高可用
  • Eureka服务中心相关配置详解

需要三个项目: eureka-server:服务注册中心,base-service-producer:服务提供方,hello-service:服务调用方(也可以是另外一个服务)

Eureka服务中心搭建

新建项目:eureka-server

添加依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>

添加注解


@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

    EurekaDiscoveryClient eurekaDiscoveryClient;


    public static void main(String[] args) {

        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

启动

访问 http://localhost:1111/

其他服务注册到Eureka服务中心

新建项目:base-service-producer

添加依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

配置文件

spring.application.name=base-service-producer
eureka.client.service-url.defaultZone=http://localhost:1111/eureka
server.port=10003

启动

访问:http://localhost:10003/base-service 有响应,同时,访问http://localhost:1111/eureka,发现base-service-producer:10003被注册到注册中心了

新建项目:hello-service

添加依赖

因为只是调用方,该依赖可以不添加

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

配置文件

因为只是调用方,该配置可以不添加

#服务命名
spring.application.name=hello-service
#指定服务注册中心地址
eureka.client.service-url.defaultZone=http://localhost:1111/eureka
server.port=10000
#需要启动eureka-server后启动,再看 http://localhost:1111/ 会发现hello-service已经注册到服务中心中去了

添加注释

因为只是调用方,该@EnableDiscoveryClient可以不添加

@EnableDiscoveryClient
@SpringBootApplication
public class HelloServiceApplication {

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

调用服务

@RequestMapping("/base")
public String baseSerivce() {
    ServiceInstance instance = discoveryClient.getInstances("base-service-producer").get(0);
    System.out.println(instance);
    String url = "http://"+instance.getHost() + ":" + instance.getPort()  + "/base-service";
    String result = restTemplate.getForObject(url, String.class);
    return result;
}

启动

http://localhost:10000/hello

当然,是在服务中调用服务,应该再建立一个项目,访问hello-service的hello

Eureka服务中心高可用

application-peer1.properties

spring.application.name=high-eureka-service
server.port=1111
eureka.instance.hostname=peer1
eureka.client.service-url.defaultZone=http://peer2:1112/eureka/
#需要在/etc/hosts中  加上 ip到host的映射  127.0.0.1 peer2
#在target 中运行
#java -jar high-eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
#java -jar high-eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2
#后访问 http://localhost:1112/    http://localhost:1111/ 可用看到将两个注册中心都当做服务注册到了注册中心中

application-peer2.properties

spring.application.name=high-eureka-service
server.port=1112
eureka.instance.hostname=peer2
eureka.client.service-url.defaultZone=http://peer1:1111/eureka/
#需要在/etc/hosts中  加上 ip到host的映射  127.0.0.1 peer1

启动

访问 http://localhost:1111/
访问 http://localhost:1112/

两者互相注册

Eureka服务中心相关配置详解

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容