单机Eureka构建步骤
1.建module
2.改pom
<dependencies>
<!--eureka server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!--引入自己定义的api通用包,可以使用payment支付Entity-->
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!--boot web actuator-->
<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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
3.写yml
server: port: 7001 eureka: instance: hostname: localhost #eureka服务端的实例名称 client: #false表示不向注册中心注册自己 register-with-eureka: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务 fetch-registry: false service-url: #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址 defaultZ4 one: http://${eureka.instance.hostname}:${server.port}/eureka/ #链接对应上面的地址和端口号
4.主启动
/**
* @Author: cws
* @Date: 19:47 2020/8/12
* @Description:
* @Version v1.0
*/
@SpringBootApplication
@EnableEurekaServer //标识eureka的服务注册中心
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class, args);
}
}
5.测试(不需要业务类)
服务注册
支付微服务8001(即项目cloud-provider-payment8001)入驻进eurekaServer
<!--eureka-client 在pom文件中引入客户依赖包--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
application.yml 里面添加Eureka配置
#服务端口号 server: port: 8001 #服务名称 spring: application: name: cloud-payment-service datasource: type: com.alibaba.druid.pool.DruidDataSource #当前数据源操作类型 driver-class-name: org.gjt.mm.mysql.Driver #mysql驱动包 url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: 123456 eureka: client: #表示是否将自己注册进EurekaServer默认为true register-with-eureka: true #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡 fetch-registry: true service-url: defaultZone: http://localhost:7001/eureka #被控制的端口和地址 mybatis: mapperLocations: classpath:mapper/*.xml type-aliases-package: com.atguigu.springcloud.entities #映射mapper文件所有实体类所在包
主启动添加 @EnableEurekaClient
/** * @Author: cws * @Date: 23:55 2020/8/11 * @Description: * @Version v1.0 */ @SpringBootApplication @EnableEurekaClient #表示成为客户端 public class PaymentMain8001 { public static void main(String[] args) { SpringApplication.run(PaymentMain8001.class, args); } }
启动测试
订单微服务80入驻进eurekaServer
增加依赖(步骤跟上面一样)
application.yml 里面添加Eureka配置
#服务端口号 server: port: 80 #服务名称 spring: application: name: cloud-order-service eureka: client: #表示是否将自己注册进EurekaServer默认为true register-with-eureka: true #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡 fetch-registry: true service-url: defaultZone: http://localhost:7001/eureka #端口号默认为80所以不用设置
主启动添加 @EnableEurekaClient(步骤跟上面一样)
启动测试
Eureka集群搭建
添加 application.yml 时先修改本机端口映射
C:WindowsSystem32driversetcHOSTS #修改文件的路径
###################SpringCloud2020################ 127.0.0.1 eureka7001.com 127.0.0.1 eureka7002.com 127.0.0.1 eureka7003.com
修改7001和7002的application.yml
7001的配置: server: port: 7001 eureka: instance: hostname: eureka7001.com #eureka服务端的实例名称 client: #false表示不向注册中心注册自己 register-with-eureka: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务 fetch-registry: false service-url: #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址 defaultZ4 one: http://eureka7002.com:7002/eureka/ #设置的地址是映射的集群ip和端口号
7002的配置: server: port: 7002 eureka: instance: hostname: eureka7002.com #eureka服务端的实例名称 client: #false表示不向注册中心注册自己 register-with-eureka: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务 fetch-registry: false service-url: #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址 defaultZ4 one: http://eureka7001.com:7001/eureka/ #设置的地址是映射的集群ip和端口号
测试集群
在7002里能看见7001既守望7001端口,就算配置成功
将两个微服务发布到Eureka集群配置中
8001项目 和80项目的application.yml
添加同样的配置代码:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
测试效果:
创建module cloud-provider-payment8002
这个模块跟8001模块一样的只不过端口不一样,所以前期创建模块我就省略步骤了,大家copy即可,我把步骤贴出来
10.1 改POM
10.2 建 application.yml 文件
10.3 主启动
10.4 业务类
10.5 修改 Controller
8002 Controller(8001 Controller也是同理):
开启负载均衡
首先到80服务下去修改写死的对外暴露的服务url为Eureka注册中心的名字
@RestController @Slf4j public class OrderController { // public static final String PAYMENT_URL = "http://localhost:8001"; 单机版本 public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE"; //改为eureka的主机名称 @Resource private RestTemplate restTemplate; @GetMapping("/consumer/payment/create") public CommonResult<Payment> create( Payment payment) { log.info("*******消费者启动创建订单*******"); return restTemplate.postForObject(PAYMENT_URL + "/payment/create", payment, CommonResult.class);//地址 参数 对象类型 }
在我们80项目的restTemplate加入spring容器的bean中增加一个负载均衡的注解@LoadBalanced
/** * @Author: cws * @Date: 13:59 2020/8/12 * @Description: * @Version v1.0 */ @Configuration public class ApplicationContextConfig { @Bean //依赖注入容器 获取对象 @LoadBalanced //使用@LoadBalanced注解赋于RestTemplate负载均衡的能力 public RestTemplate getRestTemplate() { return new RestTemplate(); } }
测试:
actuator微服务信息完善
1.1 在8001与8002项目中分别添加
安全检查正常
配置访问路径可以访问IP
#修改自定义的服务名称 instance: instance-id: payment8001 prefer-ip-address: true #鼠标悬浮显示ip地址
添加 DiscoveryClient 发现微服务
修改8001项目controller层
package com.atguigu.springcloud.controller; import com.atguigu.springcloud.entities.CommonResult; import com.atguigu.springcloud.entities.Payment; import com.atguigu.springcloud.service.PaymentService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.web.bind.annotation.*; import java.util.List; /** * @Author: cws * @Date: 22:07 2020/8/12 * @Description: * @Version v1.0 */ @RestController @Slf4j public class PaymentController { @Autowired private PaymentService paymentService; @Value("${server.port}") private String serverPort; @Autowired private DiscoveryClient discoveryClient; @GetMapping(value = "/payment/get/{id}") public CommonResult getPaymentById(@PathVariable("id") Long id) { Payment result = paymentService.getPaymentById(id); if (result!=null) { return new CommonResult(200, "查询成功机器端口为:"+serverPort , result); } else { return new CommonResult(404, "查询失败" , null); } } @PostMapping(value = "/payment/create") public CommonResult create(@RequestBody Payment payment) { int result = paymentService.create(payment); log.info("=========插入结果========"+result); //测试插入结果的日志 if (result > 0) { return new CommonResult(200, "插入成功机器端口为:"+serverPort , result); } else { return new CommonResult(404, "插入失败" , null); } } @GetMapping("/payment/discovery") public Object discovery() { List<String> services = discoveryClient.getServices(); for (String service : services) { log.info("==============" + service); //获取discovery消费者上的信息 } //获取discovery服务中心的信息 List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE"); for (ServiceInstance instance : instances) { log.info("================"+instance.getServiceId()+" "+instance.getHost()+" "+instance.getPort()+" "+instance.getUri()); } return discoveryClient; } }
记得主启动要加上开启discovery注解
/** * @Author: cws * @Date: 22:17 2020/8/12 * @Description: * @Version v1.0 */ @SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient public class PaymentMain8002 { public static void main(String[] args) { SpringApplication.run(PaymentMain8002.class, args); } }