1、 单机版eureka
- 新建module
cloud-eureka-server7001
- 修改pom文件,引入以下的依赖
<dependencies>
<!-- eureka依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<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.zhubayi.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
- 1.x与2.x说明
以前的老版本(当前使用2018)
org.springframework.cloud
spring-cloud-starter-eureka
现在新版本(当前使用2020.2)
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
- 修改配置文件
application.yml
server:
port: 7001
eureka:
instance:
hostname: localhost #eureka服务端的实例名称
client:
#false表示不向注册中心注册自己。
register-with-eureka: false
#false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
#设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
- 主启动类
EurekaMain7001
package com.zhubayi.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @author zhubayi
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class,args);
}
}
记得添加@EnableEurekaServer注解
测试:在浏览器输入http://localhost:7001/
出现这个界面说明成功了
5. 这时没有服务提供者我们去修改一下cloud-provider-payment8001
模块把它注册进7001
在cloud-provider-payment8001
模块pom添加一个添加一个依赖
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
这时cloud-provider-payment8001
的依赖
- 在
cloud-provider-payment800
的application.yml
添加一下配置
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: 1
#Eureka服务端在收到最后一次心跳后等待时间上限,单位为秒(默认是90秒),超时将剔除服务
#lease-expiration-duration-in-seconds: 2
7. 再主启动类上添加@EnableEurekaClient
注解
package com.zhubayi.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @author zhubayi
*/
@SpringBootApplication
@EnableEurekaClient
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class,args);
}
}
- 启动测试,
先要启动EurekaServer
,打开浏览器输入:http://localhost:7001/
可以看到CLOUD-PAYMENT-SERVICE
注册了进来
- 把
cloud-consumer-order80
注册进7001
cloud-consumer-order80
在pom添加以下依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- 修改
cloud-consumer-order80
的配置文件
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
- 在主启动类上添加
@EnableEurekaClient
package com.zhubayi.springcloud;
import com.zhubayi.myrule.MySelfRule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
/**
* @author zhubayi
*/
@EnableEurekaClient
@SpringBootApplication
//@RibbonClient(name = "CLOUD-PAYMENT-SERVICE",configuration = MySelfRule.class)
public class OrderMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderMain80.class,args);
}
}
测试:先要启动EurekaServer,7001服务,再要启动服务提供者provider,8001服务
可以看到这个服务已经注册进来了。
浏览器输入:http://localhost/consumer/order/get/1
成功拿到了数据.!
2、 集群版eureka
-
参考cloud-eureka-server7001,新建cloud-eureka-server7002,就把7001的拷贝一份,改改7002的配置文件.
-
修改
windows的域名映射
-
7002的配置文件
server:
port: 7002
eureka:
instance:
hostname: eureka7002.com #eureka服务端的实例名称
client:
register-with-eureka: false #false表示不向注册中心注册自己。
fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
#集群指向其它eureka
defaultZone: http://eureka7001.com:7001/eureka/
#单机就是7001自己
#defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
#server:
#关闭自我保护机制,保证不可用服务被及时踢除
#enable-self-preservation: false
#eviction-interval-timer-in-ms: 2000
7002启动类
package com.zhubayi.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @author zhubayi
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7002 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7002.class,args);
}
}
- 7001配置文件
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com #eureka服务端的实例名称
client:
register-with-eureka: false #false表示不向注册中心注册自己。
fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
#集群指向其它eureka
defaultZone: http://eureka7002.com:7002/eureka/
#单机就是7001自己
#defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
#关闭自我保护机制,保证不可用服务被及时踢除
server:
enable-self-preservation: false
eviction-interval-timer-in-ms: 2000
7001的启动类
package com.zhubayi.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @author zhubayi
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class,args);
}
}
启动7001,7002测试
- 把将支付服务8001微服务发布到上面2台Eureka集群配置中
修改8001的配置文件
- 将订单服务80微服务发布到上面2台Eureka集群配置中
启动8001和80
7001端口
7002端口
可以看到成功注册了进来,浏览器输入:http://localhost/consumer/order/get/2
成功获取到了数据。
支付服务提供者8001集群环境构建
- 参考cloud-provider-payment8001,新建cloud-provider-payment8002,就是把8001拷贝一份,然后把8002的端口改了。
- 配置文件
server:
port: 8002
spring:
application:
name: cloud-payment-service
datasource:
url: jdbc:mysql://127.0.0.1:3306/springcloud2020?useUnicode=true&characterEncoding=utf-8&useSSL=NO&serverTimezone=Asia/Shanghai
type: com.alibaba.druid.pool.DruidDataSource
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.zhubayi.springcloud.entities
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
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: payment8002
#访问路径可以显示IP地址
prefer-ip-address: true
#Eureka客户端向服务端发送心跳的时间间隔,单位为秒(默认是30秒)
#lease-renewal-interval-in-seconds: 1
#Eureka服务端在收到最后一次心跳后等待时间上限,单位为秒(默认是90秒),超时将剔除服务
#lease-expiration-duration-in-seconds:
订单服务访问地址不能写死,把OrderController
改一改
private static final String PAYMENT_URL="http://CLOUD-PAYMENT-SERVICE";
然后使用@LoadBalanced注解赋予RestTemplate负载均衡的能力
@LoadBalanced //开启负载均衡
启动测试:先要启动EurekaServer,7001/7002服务再要启动服务提供者provider,8001/8002服务
启动好之后浏览器输入:http://localhost/consumer/order/get/2
成功的注册了进来。
端口是8001
再刷新一下
端口变成了8002
Ribbon和Eureka整合后Consumer可以直接调用服务而不用再关心地址和端口号,且该服务还有负载功能了。