资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

SpringCloud学习(2)-创新互联

先知

在这里插入图片描述

桃江网站制作公司哪家好,找成都创新互联!从网页设计、网站建设、微信开发、APP开发、成都响应式网站建设公司等网站项目制作,到程序开发,运营维护。成都创新互联从2013年成立到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选成都创新互联

什么叫服务与注册中心?
服务注册中心是服务实现服务化管理的核心组件,类似于目录服务的作用,主要用来存储服务信息,譬如提供者 url 串、路由信息等。服务注册中心是微服务架构中最基础的设施之一。注册中心可以说是微服务架构中的“通讯录”,它记录了服务和服务地址的映射关系。在分布式架构中,服务会注册到这里,当服务需要调用其它服务时,就到这里找到服务的地址,进行调用。

使用Eureka
概念:

• Eureka 是 Netflix 公司开源的一个服务注册与发现的组件 。

• Eureka 和其他 Netflix 公司的服务组件(例如负载均衡、熔断器、网关等) 一起,被 Spring Cloud 社区整合为Spring-Cloud-Netflix 模块。

• Eureka 包含两个组件:Eureka Server (注册中心) 和 Eureka Client (服务提供者、服务消费者)。

操作:

架构图一览

在这里插入图片描述

创建Serve端 新建项目

在这里插入图片描述
在这里插入图片描述

配置文件

在这里插入图片描述

application.yaml

server:
  port: 8080

# Eureka配置
eureka:
  instance:
    ## Eureka实例的名称
    hostname: localhostA
  client:
    # false表示自己端就是注册中心,职责就是维护服务实例,并不需要去检查服务
    fetch-registry: false
    # false表示不向注册中心注册自己
    register-with-eureka: false
    # 设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

启动类配置
// 表示当前类为服务端Eureka服务端
@EnableEurekaServer
@SpringBootApplication
public class CloudA1Application {public static void main(String[] args) {SpringApplication.run(CloudA1Application.class, args);
    }

}
启动测试一下

访问自己的
localhost:端口号
在这里插入图片描述
一切正常再继续

Eureka Client包括两个服务模块:Service Provider(服务提供方)和Service Consumer(服务消费方)。

创建Client端的服务提供端 新建项目

新增依赖
org.projectlomboklomboktruecom.alibabadruid1.1.16com.baomidoumybatis-plus-boot-starter3.4.2mysqlmysql-connector-javaruntimeorg.springframework.bootspring-boot-starter-web
配置文件

在这里插入图片描述

application.yaml

server:
  port: 8081
# Eureka配置
eureka:
   client:
       # 表示将自己注册进Eureka Server默认为true
     register-with-eureka: true
       # 是否从Eureka Server抓去已有的注册信息,默认是true
     fetch-registry: true
       # 设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
     service-url:
       defaultZone: http://localhost:8080/eureka
#数据库配置
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/blog?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8
    username: root
    password: xxx
    type: com.alibaba.druid.pool.DruidDataSource
#  当前服务注册在Eureka Server的名称
  application:
    name: server-provider1


#MP配置
mybatis-plus:
  #  配置外部xml映射
  configuration:
    #    开启SQL日志输出
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    #    开启驼峰映射
    map-underscore-to-camel-case: true
  mapper-locations: classpath:mapper/*.xml
启动类配置
// 表示当前类客户端Eureka
@EnableDiscoveryClient
@SpringBootApplication
public class CloudB1Application {public static void main(String[] args) {SpringApplication.run(CloudB1Application.class, args);
    }

}
编写控制器类
package com.learn.cloudb1.controller;


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
public class HelloController {@GetMapping("/get1")
    public String hello(){return "hello world!";
    }
}
启动测试

访问自己的 localhost:端口号(就是服务注册中心)
可以发现多了些东西,一个报错和刚刚注册好的服务提供者

在这里插入图片描述

创建Client端的服务消费端

创建方法和创建Client端的服务服务端一样

配置文件
server:
  port: 8083

spring:
  application:
    name: service-customer1

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8080/eureka
启动类
@EnableDiscoveryClient
@SpringBootApplication
public class CloudB1Customer1Application {public static void main(String[] args) {SpringApplication.run(CloudB1Customer1Application.class, args);
    }
}
控制器
package com.learn.cloudb1_customer1.controller;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/getMes")
public class GetMesController {//从Euraka Server中获取服务提供方的服务地址信息
    @Autowired
    private DiscoveryClient ds;
    @GetMapping()
    public String getMes(){//服务提供者的名字
         //服务发现
        ListinstanceList = ds.getInstances("server-provider1");
        int port=0;
        String host="";
        //打印服务机器的信息
        for (ServiceInstance instance : instanceList) {//服务主机端口号
            port = instance.getPort();
            System.out.println("服务主机端口号:"+ port);
            //服务主机名字
            host = instance.getHost();
            System.out.println("服务主机名字:"+host);
        }
        return host+"++++"+port;
    }
}
启动测试

发现又多了一个
在这里插入图片描述

点击这里再输入自己定义的接口发现也可正常访问
在这里插入图片描述

在这里插入图片描述

高可用

准备两个Eureka Server

分别进行配置,相互注册

Eureka Client 分别注册到这两个 Eureka Server中

创建eureka-server1

server:
  port: 8761


eureka:
  instance:
    hostname: eureka-server1 # 主机名
  client:
    service-url:
      defaultZone: http://eureka-server2:8762/eureka
    register-with-eureka: true # 是否将自己的路径 注册到eureka上。eureka server 不需要的,eureka provider client 需要
    fetch-registry: true # 是否需要从eureka中抓取路径。eureka server 不需要的,eureka consumer client 需要


spring:
  application:
    name: eureka-server-ha

创建eureka-server2

server:
  port: 8762


eureka:
  instance:
    hostname: eureka-server2 # 主机名
  client:
    service-url:
      defaultZone: http://eureka-server1:8761/eureka

    register-with-eureka: true # 是否将自己的路径 注册到eureka上。eureka server 不需要的,eureka provider client 需要
    fetch-registry: true # 是否需要从eureka中抓取路径。eureka server 不需要的,eureka consumer client 需要
spring:
  application:
    name: eureka-server-ha

修改provider

server:
  port: 8001


eureka:
  instance:
    hostname: localhost # 主机名
    prefer-ip-address: true # 将当前实例的ip注册到eureka server 中。默认是false 注册主机名
    ip-address: 127.0.0.1 # 设置当前实例的ip
    instance-id: ${eureka.instance.ip-address}:${spring.application.name}:${server.port} # 设置web控制台显示的 实例id
    lease-renewal-interval-in-seconds: 3 # 每隔3 秒发一次心跳包
    lease-expiration-duration-in-seconds: 9 # 如果9秒没有发心跳包,服务器呀,你把我干掉吧~
  client:
    service-url:
      defaultZone: http://eureka-server1:8761/eureka,http://eureka-server2:8762/eureka # eureka服务端地址,将来客户端使用该地址和eureka进行通信
spring:
  application:
    name: eureka-provider # 设置当前应用的名称。将来会在eureka中Application显示。将来需要使用该名称来获取路径

修改consumer

server:
  port: 9000


eureka:
  instance:
    hostname: localhost # 主机名
  client:
    service-url:
      defaultZone:  http://eureka-server1:8761/eureka,http://eureka-server2:8762/eureka  # eureka服务端地址,将来客户端使用该地址和eureka进行通信
spring:
  application:
    name: eureka-consumer # 设置当前应用的名称。将来会在eureka中Application显示。将来需要使用该名称来获取路径

高可用测试:停掉一个eureka,依然可以访问consumer。

Eureka配置详情

实例信息配置
eureka:
instance:
hostname: localhost # 主机名
prefer-ip-address: # 是否将自己的ip注册到eureka中,默认false 注册 主机名
ip-address: # 设置当前实例ip
instance-id: # 修改instance-id显示
lease-renewal-interval-in-seconds: 30 # 每一次eureka client 向 eureka server发送心跳的时间间隔
lease-expiration-duration-in-seconds: 90 # 如果90秒内eureka server没有收到eureka client的心跳包,则剔除该服务
客户端特性配置
eureka:
client:
service-url:
# eureka服务端地址,将来客户端使用该地址和eureka进行通信
defaultZone:
register-with-eureka: # 是否将自己的路径 注册到eureka上。
fetch-registry: # 是否需要从eureka中抓取数据。
注册中心端配置
eureka:
server: #是否开启自我保护机制,默认true
enable-self-preservation:
eviction-interval-timer-in-ms: 120 2月#清理间隔(单位毫秒,默认是60*1000)
instance:
lease-renewal-interval-in-seconds: 30 # 每一次eureka client 向 eureka server发送心跳的时间间隔
lease-expiration-duration-in-seconds: 90 # 如果90秒内eureka server没有收到eureka clien
仪表盘配置
eureka:
dashboard:
enabled: true # 是否启用eureka web控制台
path: / # 设置eureka web控制台默认访问路径

OK!入门结束

当然常用的做法是将多个模块/项目建立在一个maven父工程中,这样更容易管理与使用

其他更详细的适合入门的文章
https://www.cnblogs.com/h–d/p/12635204.html
https://blog.csdn.net/jc_hook/article/details/122413858

报错解决方法
https://blog.csdn.net/hadues/article/details/105023709

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


文章标题:SpringCloud学习(2)-创新互联
本文URL:http://cdkjz.cn/article/decpho.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220