云迁移-springcould-eureka集群搭建

一.鉴于公司springcould项目要直接上云特此记录
二.eureka集群搭建
1.eureka是什么?
eureka是服务注册与发现中心,是springcloud的基础,eureka服务器大概类似于一个记录用的文本,提供服务的eureka客户端在上面留下记录(ip+端口+接口),这就是注册,消费服务的消费服务的eureka客户端则可以在上面拉取并缓存下来注册上去的接口,这就是服务发现,eureka服务器接口更新的时候,也会发送给各个客户端。
这里注意:eureka服务端和客户端没什么本质区别,保存的信息也都差不多。有页面可以看注册情况。
2.eureka集群?
eureka集群就是多个eureka服务器互相注册。注册完之后,根据(服务器更新接口会同步给其他客户端,还有服务端和客户端本质差不多的原则),多个eureka服务器的接口信息会同步,服务提供客户端只要注册到一个服务端即可,保证集群效果最好同步到所有服务端
3.服务端集群搭建步骤:
1)pom添加依赖

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

2)主方法上加上注解@EnableEurekaServer

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

3)然后配置端口、名称和向哪个服务端注册的url,多个服务端互相向其他服务端注册
服务端1:ip1
向服务端2,3注册

eureka:
  instance:
    prefer-ip-address: true
    hostname: ${spring.cloud.client.ip-address}
    instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
  client:
  	#服务端应该改成false
    registerWithEureka: false
    #服务端应该改成false
    fetchRegistry: false
    service-url:
      #待修改IP,向其他服务端注册
      defaultZone: http://ip2:4001/eureka,http://ip3:4001/eureka

服务端2:ip2
向服务端1,3注册

eureka:
  instance:
    prefer-ip-address: true
    hostname: ${spring.cloud.client.ip-address}
    instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
  client:
  	#服务端应该改成false
    registerWithEureka: false
    #服务端应该改成false
    fetchRegistry: false
    service-url:
      #待修改IP,向其他服务端注册
      defaultZone: http://ip1:4001/eureka,http://ip3:4001/eureka

服务端3:ip3
向服务端1,2注册

eureka:
  instance:
    prefer-ip-address: true
    hostname: ${spring.cloud.client.ip-address}
    instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
  client:
  	#服务端应该改成false
    registerWithEureka: false
    #服务端应该改成false
    fetchRegistry: false
    service-url:
      #待修改IP,向其他服务端注册
      defaultZone: http://ip1:4001/eureka,http://ip2:4001/eureka

配置的解释

eureka: 
  instance:
    hostname: eureka8761 	# eureka 服务端的实例名称
  client: 
    register-with-eureka: false     # false 表示不向本端注册中心注册自己。
    fetch-registry: false     # false 表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url: 
      # 有多台服务器时,用逗号隔开
      # Eureka 实例之间互相注册,即把自己注册到另外两个服务注册中心实例中,以便于客户端的数据同步
      defaultZone: http://eureka8761:8761/eureka/,http://eureka8762:8762/eureka/
server:
  port: 80

spring:
  application:
    name: cloud-order-service

eureka:
  client:
    #表示是否将自己注册进EurekaServer默认为true。
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetchRegistry: true
    service-url:
      #单机版
      #defaultZone: http://localhost:7001/eureka
      # 集群版
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
  instance:
      instance-id: payment8001
      #访问路径可以显示IP地址
      prefer-ip-address: true
      #Eureka客户端向服务端发送心跳的时间间隔,单位为秒(默认是30秒)
      #lease-renewal-interval-in-seconds: 30
      #Eureka服务端在收到最后一次心跳后等待时间上限,单位为秒(默认是90秒),超时将剔除服务
      #lease-expiration-duration-in-seconds: 90

4.服务提供者
1)pom添加依赖

	<!--Eureka 客户端-->
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
	</dependency>

2)配置

eureka:
  client:
    register-with-eureka: true  # 是否向注册中心注册自己
    fetchRegistry: true   # 是否从注册中心抓取已有的注册信息,默认true,集群必须设置为true
    service-url:
      # 集群中各个服务注册中心的地址
      defaultZone: http://ip1:4001/eureka,http://ip2:4001/eureka,http://ip3:4001/eureka
  instance:
    instance-id: 服务实例Id
    prefer-ip-address: true  #访问路径可以显示IP地址

3)Eureka 客户端注解 @EnableEurekaClient

@SpringBootApplication
@EnableEurekaClient
public class LbsQuartzApplication {
	public static void main(String[] args) throws Exception {
		SpringApplication.run(LbsQuartzApplication.class, args);
	}

}

5.服务发现者
1)pom添加依赖

	<!--Eureka 客户端-->
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
	</dependency>

2)配置

eureka:
  client:
    register-with-eureka: false  # 是否向注册中心注册自己
    fetchRegistry: true   # 是否从注册中心抓取已有的注册信息,默认true,集群必须设置为true
    service-url:
      # 集群中各个服务注册中心的地址
      defaultZone: http://ip1:4001/eureka,http://ip2:4001/eureka,http://ip3:4001/eureka
  instance:
    instance-id: 服务实例Id
    prefer-ip-address: true  #访问路径可以显示IP地址

3)加上@EnableEurekaClient
调用方法有很多,这里写两个,本系统用的是@FeignClient(name = “服务实例id”)
第一种方法:这里调用已注册的服务实例可以在主函数上添加注解@EnableFeignClients(),并写一个接口,接口上添加@FeignClient(name = “服务实例id”)

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients()
public class LbsUsercenterApplication {

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


}
@FeignClient(name = "服务实例id")
public interface LocateQuartzFeignClient {
	@RequestMapping(value = "/jobManager/add", method = RequestMethod.POST)
	BaseResult<Object> addJob(@RequestBody JobInfoReq req);

}

第二种方法:在主函数加@EnableDiscoveryClient并在方法上加载DiscoveryClient

@EnableEurekaClient 
@EnableDiscoveryClient
@SpringBootApplication 
public class ApplicationMain{

    public static void main(String[] args) {
        SpringApplication.run(ApplicationMain.class, args);
    } 
 }
@RestController
@RequestMapping("payment")
public class PaymentController {

    // 服务发现
    @Resource
    private DiscoveryClient discoveryClient;

    @GetMapping("discovery")
    public Object discovery() {
        List<String> services = discoveryClient.getServices();
        services.forEach(System.out::println);
		// 获得服务名下的实例列表
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        instances.forEach(instance -> {
            System.out.println(instance.getServiceId() + "\t" + instance.getHost() + "\t" + instance.getPort() + "\t" + instance.getUri());
        });
        return this.discoveryClient;
    }

}

第一种方法是集成了Feign,这个等下次再详细介绍

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值