springcloud+springcloudalibaba之Eureka(二)

  • 时间:
  • 浏览:
  • 来源:互联网

概念

  • 服务治理定义

服务治理是主要针对分布式服务框架的微服务,处理服务调用之间的关系、服务发布和发现、故障监控与处理,服务的参数配置、服务降级和熔断、服务使用率监控等。

  • 服务注册与发现

Eureka采用CS的设计架构,Eureka Server作为服务器注册功能的服务器,他是服务注册中心,而系统中的其它其它微服务,使用Eureka的客户端连接到Eureka Server并维持心跳连接。这样系统的维护人员就可以通过Eureka Server来监控系统中各个微服务是否正常

  • 与dubbo对比

  • Eureka自我保护

保护模式主要用于一组客户端和Eureka Server之间存在网络分区场景下的保护,一旦进入保护模式,Eureka Server将会尝试保护其服务注册表的信息,不再删除服务注册表的数据,也就是不会注销任何微服务,属于CAP的AP

  • 取消自我保护配置
eureka:
  server:
    enable-self-preservation: false
  • 注册中心对比

 

Eureka server配置

  • pom配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>mscloud</artifactId>
        <groupId>com.cloud2020</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-server-eureka7001</artifactId>


    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
</project>
  • application.yml配置
server:
  port: 7001
eureka:
  instance:
    hostname: localhost
  client:
    #不向注册中心注册自己
    register-with-eureka: false
    #不检索服务
    fetch-registry: false
    service-url:
      #设置与Eurekaserver交互的地址
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  • 添加注解
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain {
    public static void main(String[] args) {
        SpringApplication.run(EurekaMain.class, args);
    }
}

Eureka client配置

  • pom配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>mscloud</artifactId>
        <groupId>com.cloud2020</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-provider-order80</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.cloud2020</groupId>
            <artifactId>cloud-api-commoms</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

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

</project>
  • application.yml配置

server:
  port: 80

spring:
  application:
    name: cloud-consumer-order

eureka:
  client:
    fetch-registry: true
    register-with-eureka: true
    service-url:
      defaultZone: http://localhost:7001/eureka
  • 添加注解

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

Eureka集群

eureka7001.com的application.yml

server:
  port: 7001

eureka:
  instance:
    hostname: eureka7001.com
  client:
    #不向注册中心注册自己
    register-with-eureka: true
    #不检索服务
    fetch-registry: true
    service-url:
      #设置与Eurekaserver交互的地址
      defaultZone: http://eureka7002.com:7002/eureka/

eureka7002.com的application.yml

server:
  port: 7002

eureka:
  instance:
    hostname: eureka7002.com
  client:
    #不向注册中心注册自己
    register-with-eureka: true
    #不检索服务
    fetch-registry: true
    service-url:
      #设置与Eurekaserver交互的地址
      defaultZone: http://eureka7001.com:7001/eureka/

 order服务的application.yml配置

server:
  port: 80

spring:
  application:
    name: cloud-consumer-order

eureka:
  client:
    fetch-registry: true
    register-with-eureka: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka

支付服务集群

  • 修改指定ip:端口的的配置变为服务名
    private static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";

    @PostMapping("/create")
    public CommonResult<Payment> createPayment(@RequestBody Payment payment) {
        return restTemplate.postForObject(PAYMENT_URL + "/payment/create", payment, CommonResult.class);
    }
  • restemplate增加负载均衡
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate() {
        return  new RestTemplate();
    }

 actuator配置

  • application.yml配置
eureka:
  instance:
    instance-id: payment8001
    prefer-ip-address: true
  •  显示效果

discovery信息查看

    @Resource
    private DiscoveryClient discoveryClient;

    @GetMapping("/discovery")
    public CommonResult discovery() {
        List<String> list =  discoveryClient.getServices();
        list.forEach(v -> log.info(v));

        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        instances.forEach(v -> log.info(v.getInstanceId() + ":" + v.getPort() + ":" + v.getUri()));
        return new CommonResult(200, "成功");
    }

 使用zookeeper做配置中心

  • 安装zookeeper(先安装jdk)
wget https://mirrors.bfsu.edu.cn/apache/zookeeper/zookeeper-3.6.2/apache-zookeeper-3.6.2-bin.tar.gz
tar -zxvf apache-zookeeper-3.6.2-bin.tar.gz
cd apache-zookeeper-3.6.2-bin/
cd conf/
cp zoo_sample.cfg zoo.cfg
cd ..
cd bin/
./zkServer.sh start
./zkCli.sh -server 127.0.0.1:2181(验证)
  • 写pom
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
        </dependency>
  • 写yml
spring:
  cloud:
    zookeeper:
      connect-string: 106.55.146.154:2181
  •  修改注解
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain8080 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentMain8080.class, args);
    }
}
  • 验证
./zkCli.sh -server 127.0.0.1:2181

 

  •  注意

zookeeper调用服务名区分大小写,而eureka不区分

 consul做配置中心

  • consul简介

  •  中文地址

https://www.springcloud.cc/spring-cloud-consul.html

  • consul安装
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
sudo yum -y install consul
consul agent -dev   -client 0.0.0.0 -ui
  • 改pom
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>
  • 写yml
spring:
  cloud:
    consul:
      host: 106.55.146.154
      port: 8500
      discovery:
        service-name: ${spring.application.name}
        heartbeat:
          enabled: true
  • 修改注解
@SpringBootApplication
@EnableDiscoveryClient
public class OrderMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderMain80.class, args);
    }
}

 

  •  验证

 

本文链接http://www.dzjqx.cn/news/show-617305.html