单机版注册中心:
1、Eureka服务端:
pom
<!--eureka-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
yml:
server:
port: 7001
eureka:
instance:
hostname: localhost #eureka服务端的实例名字
client:
allow-redirects: false # 是否向注册中心注册本服务:true为是。注册中心不需要将自己注册进去
fetch-registry: false # false表示自己是注册中心,职责是维护服务实例,并不需要去检索服务
service-url:
#设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址,多个时用逗号隔开
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
主启动类 :
@EnableEurekaServer
@SpringBootApplication
@EnableEurekaServer //服务端:注册中心
public class EurekaApplication7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication7001.class,args);
}
}
2、EurekaClient端的--服务提供者端:
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
server:
port: 8001
spring:
application:
name: cloud-payment-service # 服务名称
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456789
eureka:
client:
register-with-eureka: true
fetchRegistry: true
service-url:
defaultZone: http://localhost:7001/eureka
主启动类添加注解:@EnableEurekaClient
3、EurekaClient端的--服务调用者端:
80Controller层
@Resource
public RestTemplate restTemplate;
public static final String PAY_URL="http://localhost:8001";
@GetMapping("/consumer/payment/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
return restTemplate.getForObject(PAY_URL+"/payment/get/"+id,CommonResult.class);
}
集群版注册中心:
搭建Eureka注册中心集群,实现负载均衡+故障容错
为了模拟实际情况,所以修改下hosts文件
C:\Windows\System32\drivers\etc下找到hosts文件添加以下内容
# test-eureka
127.0.0.1
eureka7001.com
127.0.0.1
eureka7002.com
1、Eureka服务端:
server:port: 7001eureka:instance:hostname: eureka7001.com #eureka服务端的实例名字client:register-with-eureka: false #不向注册中心注册自己fetch-registry: false #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务service-url:defaultZone: http://eureka7002.com:7002/eureka/ #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
如上是7001注册到7002上
同样7002 注册到7001上
server:port: 7002eureka:instance:hostname: eureka7002.comclient:#不注册自己register-with-eureka: false#自己是注册中心,不需要去检索服务fetch-registry: falseservice-url:#设置与Eureka Server 交互的地址查询服务和注册服务都需要依赖这个地址defaultZone: http://eureka7001.com:7001/eureka/
2、EurekaClient端的--服务提供者Service端:
server:port: 8001spring:application:name: cloud-payment-service # 服务名称datasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: org.gjt.mm.mysql.Driverurl: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=falseusername: rootpassword:*********
eureka:client:#注册自己register-with-eureka: true#需要去检索服务fetch-registry: trueservice-url:#设置与Eureka Server 交互的地址查询服务和注册服务都需要依赖这个地址defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7001.com:7001/eureka/instance:instance-id: payment8001 # 把默认的localhost:name:端口名,修改为payment8001prefer-ip-address: true #访问路径下鼠标悬停显示IP地址mybatis:type-aliases-package: com.atguigu.springcloud.entitiesmapper-locations: classpath:mapper/*.xml

如图:现有两个服务端:8001;8002 ,服务名字都是CLOUD_PAYMENT_SERVICE
3、EurekaClient端的--服务调用者端:
server:port: 80spring:application:name: cloud-consumer-order # 服务名称eureka:client:#注册自己register-with-eureka: true#需要去检索服务fetch-registry: trueservice-url:#设置与Eureka Server 交互的地址查询服务和注册服务都需要依赖这个地址defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7001.com:7001/eureka/
80Controller层
// 直接服务提供方的服务的名字public static final String PAY_URL="http://CLOUD-PAYMENT-SERVICE";@Resourcepublic RestTemplate restTemplate;@GetMapping("/consumer/payment/get/{id}")public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){return restTemplate.getForObject(PAY_URL+"/payment/get/"+id,CommonResult.class);}
RestTemplate: 是有一种优雅的HTTP请求方式---参考二
自我
保护机制:
某时刻某一个微服务不可用了,Eureka不会立刻清理,依旧会对该微服务的信息进行保存
属于CAP里面的AP分支

2.禁止自我保护:(一般生产环境中不会禁止自我保护)
7001修改:
1)出厂默认,自我保护机制是开启的
eureka.server.enable-self-preservation = true
2)使用eureka.server.enable-self-preservation = false可以禁用自我保护模式
server:
enable-self-preservation: false # 禁用自我保护模式
eviction-interval-timer-in-ms: 2000 # 2秒
8001修改:
1)添加 eureka.instance.lease-renewal-interval-in-seconds和lease-expiration-duration-in-seconds
# eureka客户端想服务端发动心跳的时间间隔,单位为秒(默认是30秒)。开发的时候可以设置小一些,以保证服务关闭后注册中心及时剔除服务
lease-renewal-interval-in-seconds: 1
# eureka服务端在收到最后一次心跳后等待时间上限,单位为秒(默认是90秒)。开发时候设置小一些
lease-expiration-duration-in-seconds: 2
来源:oschina
链接:https://my.oschina.net/u/4326175/blog/4480683