Consul cluster join Gateway Service
Architecture diagram
External applications or websites through an external gateway service consumer services, the interior of the producers themselves may also be consumer, consumer behavior through internal Interior Gateway service consumption.
One internal and one external gateway and a gateway Consul Client deployed on a single server, this gateway server at least two groups, there will be external front gateway load balancing equipment, internal use Consul Client Gateway service used after inquiry, internal gateway load balancing by the Consul responsible.
Set up demonstration environment
In the Consul Cluster Server + Client mode basis, we update and start the gateway service and customer service, demo environment we only start to simulate a gateway service.
Delete spring-cloud-gateway and spring-cloud-consul-consumer both containers.
docker pull bluersw/spring-cloud-gateway:v3
docker run --name=spring-cloud-gateway -d -p 9000:9000 bluersw/spring-cloud-gateway:v3 /opt/consul/./consul agent -data-dir=/opt/consul/data -config-dir=/opt/consul/config -node=gw-cc -join 172.17.0.2
docker exec spring-cloud-gateway /usr/local/java/bin/java -jar /opt/spring-cloud-gateway-0.0.1-SNAPSHOT.jar
docker pull bluersw/spring-cloud-consul-consumer:v3
docker run --name=spring-cloud-consul-consumer -d -p 9003:9003 bluersw/spring-cloud-consul-consumer:v3 /opt/consul/./consul agent -data-dir=/opt/consul/data -config-dir=/opt/consul/config -node=consumer-cc -join 172.17.0.2
docker exec spring-cloud-consul-consumer /usr/local/java/bin/java -jar /opt/spring-cloud-consul-client-0.0.1-SNAPSHOT.jar
TAG: V3 version Gateway and consumers mirroring content
spring-cloud-gateway project configuration file is modified as follows (also registered on the Consul Client), mainly to increase prefer-ip-address or IP address can not get Consul services:
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
In order to simulate the internal consumption of other services gateway service calls, spring-cloud-consul-client project (spring-cloud-consul-consumer ) add the following code:
create Feign style proxy class
//网关服务
@FeignClient(name="PC-ApiGateWay")
public interface GatewayRemote {
//网关上的请求地址和外部用浏览器浏览的路径相同
@RequestMapping("/service-provider/hello")
public String Hello(@RequestParam String name);
}
Controller has increased by the following methods:
@Autowired
GatewayRemote gatewayRemote;
@RequestMapping("/TestGW")
public String TestGW(){
String first = gatewayRemote.Hello("first-SWS");
String second = gatewayRemote.Hello("second-SWS");
return first + " | " + second;
}
Analog external access
Direct access 127.0.0.1:9000/service-provider/hello?name=sws in the browser, get service return information:
Analog internal access
Access 127.0.0.1:9003/TestGW in a browser, get service return information:
Source
Github repository: https: //github.com/sunweisheng/spring-cloud-example