微服务-注册中心与网关

917 阅读2分钟

1. 环境

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

2. 服务注册与发现(Consul)

docker-compose.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 启动服务

docker-compose up

2.2 管理界面

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

3. 注册中心

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

  • 首先来完成需要完成的微服务
  • 其次向consul注册服务

3. 首先做两个微服务提供者

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

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

启动后,就应该注册到服务中心 application.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…

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

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

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

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…

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…