开发过程
consul安装配置->应用注册consul->zuul网关服务开发->调用zuul提供的url访问微服务
一、consul安装配置
1.启动
consul agent -server -bind=192.168.131.129 -client=0.0.0.0 -bootstrap-expect=1 -data-dir=/usr/data/consul_data -node=server1 > /dev/null 2>&1 &
验证:
启动成功后,不要关闭终端窗口,在浏览器之中访问:http://192.168.xxx.134:8500/ ,如若无误将会得到: Consul Agent
2.启动Client端
nohup consul agent -bind=192.168.131.129 -http-port=8500 -dns-port=8600 -client=0.0.0.0 -data-dir=/usr/data/consul_data/ -node=client1 > /dev/null 2>&1 &
指定 http 与 dns 端口号
nohup consul agent -bind=192.168.131.129 -http-port=7500 -dns-port=7600 -client=0.0.0.0 -data-dir=/usr/data/consul_data/ -node=client1 > /dev/null 2>&1 &
3.带 UI 启动
nohup consul agent -server -bind=192.168.131.129 -client=0.0.0.0 -bootstrap-expect=1 -data-dir=/usr/data/consul_data -node=server1 -ui > /dev/null 2>&1 &
4.搭建集群
搭建多个节点,在多个节点中启动 Consul ,通过 join 命令关联示例
例如: 将当前节点添加到 192.168.131.129 节点之中
consul join 192.168.131.129
成功后,会输出:Successfully joined cluster by contacting 1 node
查看集群:
consul members
Node Address Status Type Build Protocol DC Segment
server1 192.168.131.129:8301 alive server 1.6.1 2 dc1 <all>
5.删除结点
curl --request PUT http://192.168.131.129:8500/v1/agent/service/deregister/Producer-192-168-131-1
二、服务注册
1.通过SpringCloud的consul集成注册服务
(1).pom中添加相关依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
(2).配置应用名称和注册地址
spring.application.name=Producer
spring.cloud.consul.host=192.168.131.129
(3).添加启动类
package com.producer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ProducerStart {
public static void main(String[] args) {
SpringApplication.run(ProducerStart.class, args);
}
}
(4).验证
critical错误原因
通过访问 http://192.168.131.129:8500/v1/agent/checks,得到如下响应
{
"service:Producer": {
"Node": "server1",
"CheckID": "service:Producer",
"Name": "Service 'Producer' check",
"Status": "critical",
"Notes": "",
"Output": "Get http://localhost:8080/health: dial tcp [::1]:8080: connect: connection refused",
"ServiceID": "Producer",
"ServiceName": "Producer",
"ServiceTags": [],
"Definition": {},
"CreateIndex": 0,
"ModifyIndex": 0
}
}
没有配置健康检查
2.添加监控监控配置
(1).pom文件添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
(2).properties配置:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
三、Zuul网关
1.新建springcloud工程,集成zuul。
pom文件添加依赖
<!-- Zuul -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<!-- 健康监控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
配置Zuul应用配置
spring.application.name=Zuul-Route
server.port=8765
spring.cloud.consul.host=192.168.131.129
spring.cloud.consul.discovery.healthCheckUrl=http://${spring.cloud.client.ipAddress}:${server.port}/health
spring.cloud.consul.discovery.healthCheckInterval=15s
spring.cloud.consul.discovery.enabled=true
spring.cloud.consul.discovery.preferIpAddress=true
spring.cloud.consul.discovery.instanceId=${spring.application.name}
#Zuul public prefixx
zuul.prefix=/api
zuul.routes.customer.path=/brand/**
zuul.routes.customer.serviceId=sellergoods
添加启动类
package com.springcloud.zuul;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
}
}
2.验证,通过postman发送请求通过zuul路由到对应微服务。
访问地址:http://localhost:8765/api/brand/brand/findAll