1, Configure and start nacos
The purpose of configuring nacos is to realize dynamic routing and route to the corresponding service address according to the service name.
First of all, you need to download a nacos 2.0.3 on the official website. For the download and installation configuration of nacos, see my other article and link https://blog.csdn.net/qq_28147821/article/details/122301155?spm=1001.2014.3001.5502
After configuration, I change the default port of nacos to 8840. You can use the default port without modification
After configuration, start nacos, open the bin directory of nacos, and run startup.com with the administrator on the right CMD, as shown in the figure below, indicates successful operation.
II. Construction project
Use idea to create an empty maven project hscprovider, and delete the src folder after building.
Then create four Spring Initializr projects, as shown in the figure below, named gateway, gateway1, provider1 and provider2
Rename the configuration file to bootstrap YML and configure gateway and nacos
1. Configure gateway and gateway1
When configuring the pom file, pay special attention to the version. The version should be consistent, otherwise there will be various problems. I use 2.0.4 for gateway and springboot, and delete the original spring boot starter web in the pom file and under Dependencies, otherwise an error will be reported.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.cnki</groupId> <artifactId>gateway2</artifactId> <version>0.0.1-SNAPSHOT</version> <name>gateway2</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-context</artifactId> <version>2.0.4.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.0.4.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <version>2.0.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> <version>2.0.4.RELEASE</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
We set the gateway port to 8030 and the gateway 1 port to 8040. Other codes are the same
server: port: 8040 #port spring: application: name: gateway-hsc #Gateway name is used to display the name in nacos. The two gateway names are consistent cloud: nacos: config: file-extension: yml server-addr: 127.0.0.1:8840 #nacos address configuration center discovery: server-addr: 127.0.0.1:8840 #nacos address registry gateway: discovery: locator: enabled: true #Enable addressing by service name and load balancing #getway http://localhost:8030/providerhsc/provider/get Request path lower-case-service-id: true routes: - id: nacos-getway-provider uri: lb://providerhsc # routing address predicates: - Path=/provider/** #Assert filters: - StripPrefix=1
geteway startup class:
package com.cnki.gateway; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient //Register with nacos public class GatewayApplication { public static void main(String[] args) { SpringApplication.run(GatewayApplication.class, args); } }
2. Configure provider1 and provider2
The next step is to configure the service provider, which is our specific micro service.
The codes of provider1 and provider2 are the same. Except for the port number, provider1 configured here is 8032 and provider2 is 8033.
Configuring pom files
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.cnki</groupId> <artifactId>provider1</artifactId> <version>0.0.1-SNAPSHOT</version> <name>provider1</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.0.3.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <version>2.0.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.6.3</version> <scope>compile</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Configuration yml file: the name here is your service name provider HSC, which needs to be configured in the yml file of gateway
server: port: 8033 spring: application: name: providerhsc cloud: nacos: discovery: server-addr: 127.0.0.1:8840 config: server-addr: 127.0.0.1:8840 file-extension: yml
Configure startup class Provider1Application:
package com.cnki.provider1; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Provider1Application { public static void main(String[] args) { SpringApplication.run(Provider1Application.class, args); } }
Create a controller ProviderController. To distinguish between this, I set the return information to "this is program 1" and "this is program 2"
package com.cnki.provider1.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("provider/") public class ProviderController { @RequestMapping(value = "get", method = RequestMethod.GET) public String getStr() { return "This is program 1"; } }
3, Configure nginx
Configure nginx config
upstream gateway { server 127.0.0.1:8030; server 127.0.0.1:8040; } server { listen 80; #nginx default port server_name gateway.com; #Access domain name #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://gateway; }
Configure local host C:\Windows\System32\drivers\etc\hosts
127.0.0.1 gateway.com
This is to access the domain name locally. The default access is 127.0.0.1
Double click ngx.inx, and then start exe
The configuration is almost complete. Next, test.
4, Start test
Start gateway, gateway1, provider1, and provider2
The next step is to test.
1. First, directly test the addresses of provider1 and provider2 http://localhost:8032/provider/get And http://localhost:8033/provider/get
2. Test gateway 1 and gateway
http://localhost:8040/providerhsc/provider/get
http://localhost:8030/providerhsc/provider/get
Refresh for many times, and call provider1 and provider2 gateway repeatedly to realize polling automatically.
3. Test nginx, unify the front-end portal, test the gateway load, poll the browser, and enter the address:
http://gateway.com/providerhsc/provider/get
The test results are as follows: the cluster deployment of gateway is realized
Log in to the local nacos and you can find that there are four services in use
Next, we stop provider1 in the program, and then we request
When an error is reported, it means that nacos has not found that provider 1 has stopped in time.
After a few seconds, we kept asking, and the program kept asking for provider2, which means that nacos has offline provider1 and will not ask for provider1.
Then we start provider1 and keep asking. After about 5 seconds, the system starts calling provider1 again, which means that nacos automatically recognizes that provider1 has been started.
Then we stop gateway1, and the system will still request gateway1, causing it to rotate all the time. Then the request succeeds, indicating that nginx calls gateway1 and the request has not returned. Then nginx will call gateway2 and complete all requests.
A problem here is that nginx doesn't know that gateway1 is dead, so subsequent requests may call gateway1, resulting in a long request time. I'll study how to deal with this problem later.