微服务-注册中心与网关 1 环境 2 服务注册与发现(C

  1. 环境

中间件 作用 备考
consul 服务注册与发现 简单安装方式
  1. 服务注册与发现(Consul)

docker-compose.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
yml复制代码version: '3'

networks:
consul-net:

services:
consul1:
image: consul
container_name: node1
command: agent -server -bootstrap-expect=3 -node=node1 -bind=0.0.0.0 -client=0.0.0.0 -datacenter=dc1
networks:
- consul-net

consul2:
image: consul
container_name: node2
command: agent -server -retry-join=node1 -node=node2 -bind=0.0.0.0 -client=0.0.0.0 -datacenter=dc1
depends_on:
- consul1
networks:
- consul-net

consul3:
image: consul
container_name: node3
command: agent -server -retry-join=node1 -node=node3 -bind=0.0.0.0 -client=0.0.0.0 -datacenter=dc1
depends_on:
- consul1
networks:
- consul-net

consul4:
image: consul
container_name: node4
command: agent -retry-join=node1 -node=ndoe4 -bind=0.0.0.0 -client=0.0.0.0 -datacenter=dc1 -ui
ports:
- 8500:8500
depends_on:
- consul2
- consul3
networks:
- consul-net

2.1 启动服务

1
bash复制代码docker-compose up

2.2 管理界面

点击[管理 ]http://localhost:8500

  1. 注册中心

从上图所示,具体思路如下:

  • 首先来完成需要完成的微服务
  • 其次向consul注册服务
  1. 首先做两个微服务提供者

3.1 各自下载后,使用Springboot启动

3.2 Spring Cloud Gateway注册到服务中心(Consul)

启动后,就应该注册到服务中心
application.properties

1
2
3
4
5
6
7
8
9
properties复制代码spring.application.name=spring-cloud-provider-01
server.port=9001
spring.cloud.consul.host=127.0.0.1
spring.cloud.consul.port=8500
#注册到consul的服务名称
spring.cloud.consul.discovery.serviceName=service-provider
#以下两项如果不配置健康检查一定失败
spring.cloud.consul.discovery.prefer-ip-address=true
spring.cloud.consul.discovery.health-check-path=/actuator/health

3.3 查看consul管理终端

下面出现两个微服务

3.4 调用注册中心的微服务

client: gitee.com/actual-comb…

1
2
3
4
5
6
properties复制代码spring.application.name=spring-cloud-consul-client
server.port=9003
spring.cloud.consul.host=127.0.0.1
spring.cloud.consul.port=8500
#设置不需要注册到 consul 中
spring.cloud.consul.discovery.register=false

3.4.1 先看一种调用方式:

TestConsul.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
java复制代码package com.cloud.consul.client.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cloud.consul.client.service.GatewayRemote;
import com.cloud.consul.client.service.ServiceProviderRemote;

@RestController
public class TestConsul {

@Autowired
ServiceProviderRemote remote;

...
@RequestMapping("/TestHello")
public String TestHello(){
String first = remote.Hello("first-SWS");
String second = remote.Hello("second-SWS");
return first + " | " + second;
}

@RequestMapping("/Test")
public String Test(){
return "OK";
}

......
}

ServiceProviderRemote.java

1
2
3
4
5
6
7
8
9
10
11
12
java复制代码package com.cloud.consul.client.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(name= "service-provider")
public interface ServiceProviderRemote {

@RequestMapping("/hello")
public String Hello(@RequestParam String name);
}

看结果

从结果来看,以及Client调用代码来看只是连接了注册中心,并不知道服务的IP和端口是多少,就能得到想要的结果了。

3.4.2 通过网关来调用

TestConsul.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
java复制代码package com.cloud.consul.client.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cloud.consul.client.service.GatewayRemote;
import com.cloud.consul.client.service.ServiceProviderRemote;

@RestController
public class TestConsul {
...
@Autowired
GatewayRemote gatewayRemote;

...
@RequestMapping("/Test")
public String Test(){
return "OK";
}

@RequestMapping("/TestGW")
public String TestGW(){
String first = gatewayRemote.Hello("first-SWS");
String second = gatewayRemote.Hello("second-SWS");
return first + " | " + second;
}
}

服务网关:gitee.com/actual-comb…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
yml复制代码server:
port: 9000
spring:
cloud:
consul:
host: 127.0.0.1
port: 8500
discovery:
register: true
prefer-ip-address: true
health-check-path: /actuator/health
gateway:
routes:
- id: test_route
uri: lb://service-provider
predicates:
- Path=/service-provider/{segment}
filters:
- SetPath=/{segment}
- name: Hystrix
args:
name: service-provider-fallback
fallbackUri: forward:/service-provider-error
- name: Retry
args:
retries: 3
statuses: BAD_GATEWAY,BAD_REQUEST
default-filters:
- name: Hystrix
args:
name: fallbackcmd
fallbackUri: forward:/default-error
application:
name: PC-ApiGateWay

看结果

看网关配置了负载均衡,熔断机制

4 访问网关

4.1 负载均衡

下面地址反复刷新,看下运行结果
http://localhost:9000/service-provider/hello?name=luds

刷新

如果此时把service-provider

再次刷新上面的地址

截至到这里,注册中心就练习完毕了
(这里是不是感觉配置文件是不是挺多的)

参考:github.com/sunweisheng…

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%