java Eureka配置

springCloud学习记录

Eureka(注册中心)

eureka为服务注册,分为服务端和客户端,客户端注册进服务端,服务端可配置集群互相守望。服务端不处理业务,只负责服务注册,客户端才负责数据处理 ;服务者和消费者都需要注册。在openfeign之后都是用openfeign访问。

关键注解

启动类
@EnableEurekaServer // 服务端注册
@EnableEurekaClient // 消费端注册
@EnableDiscoveryClient // 可获取当前服务的相关信息(可选)

一、服务端

pom:
导入eureka服务器端相关包

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

yml:
eureka服务端相关配置

# 7001 eureka服务
server:
  port: 7001
spring:
  application:
    name: cloud-eureka-service
eureka:
  instance:
    # eureka服务端的实例名称
    # 单机 hostname: localhost
    # 输入这个地址就能看到,已注册的服务信息
    hostname: eureka7001.com
  client:
    # false表示不向注册中心注册自己,服务端一般不需要注册自己
    register-with-eureka: false
    # false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要检索服务
    fetch-registry: false
    service-url:
      # 设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
      # 单机 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      # 相互注册,用于集群
      defaultZone: http://eureka7002.com:7002/eureka
# 7002 eureka服务
server:
  port: 7002
spring:
  application:
    name: cloud-eureka-service
eureka:
  instance:
    # eureka服务端的实例名称
    # 单机 hostname: localhost
    hostname: eureka7002.com
  client:
    # false表示不向注册中心注册自己,服务端一般不需要注册自己
    register-with-eureka: false
    # false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要检索服务
    fetch-registry: false
    service-url:
      # 设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
      # 单机 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      # 相互注册,用于集群
      defaultZone: http://eureka7001.com:7001/eureka

入口类:

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

二、消费端

pom:
导入客户端相关包

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

yml:

server:
  port: 8001
spring:
  application:
  # 为访问的服务名
    name: cloud-payment-service
  datasource:
    # 当前数据源操作类型
    type: com.alibaba.druid.pool.DruidDataSource
    # mysql驱动类
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8
    username: root
    password: 123456
eureka:
  client:
    register-with-eureka: true
    fetch-registry: 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
mybatis:
  mapper-locations: classpath*:mapper/*.xml
  type-aliases-package: com.atguigu.springcloud.entities

两个客户端服务是为了负载均衡

server:
  port: 8002
spring:
  application:
      name: cloud-payment-service
  datasource:
    # 当前数据源操作类型
    type: com.alibaba.druid.pool.DruidDataSource
    # mysql驱动类
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8
    username: root
    password: 123456
  # zipkin/sleuth链路跟踪
  zipkin:
    base-url: http://localhost:9411
  sleuth:
    sampler:
      # 采样值介于0到1之间,1表示全部采集
      probability: 1
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
#       defaultZone: http://localhost:7001/eureka
      # 集群版
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
  instance:
    # eureka 注册显示的名称
    instance-id: payment8002
    # 访问路径可以显示ip地址
    prefer-ip-address: true

mybatis:
  mapper-locations: classpath*:mapper/*.xml
  type-aliases-package: com.atguigu.springcloud.entities

启动类:

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class PaymentMain801 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentMain801.class, args);
    }
}
@SpringBootApplication
@EnableEurekaClient
public class PaymentMain802 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentMain802.class, args);
    }
}

三、访问服务

@Configuration
public class ApplicationContextConfig {
    @Bean
    @LoadBalanced // 赋予负载均衡
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

@RestController
@Slf4j
public class OrderController {
// 只有一个服务时
//    public static final String PAYMENT_URL = "http://localhost:8001";
// 多服务,CLOUD-PAYMENT-SERVICE为上方配置的服务名称
    public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id") Long id){
    	// /payment/get为eureka注册的客户端服务接口
    	// CommonResult封装类
        return restTemplate.getForObject(PAYMENT_URL + "/payment/get/"+id, CommonResult.class);
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值