1. Create a project
-
1. Create a
spring-boot
project -
2. Create three modules:
file
,system
, andgateway
modules -
3.
file
andsystem
configure startup information respectively, and create a simple controllerserver.port=9000 spring.application.name=file server.servlet.context-path=/file
-
4. Introduce dependencies in the root directory
<properties> <java.version>1.8</java.version> <spring-cloud.version>2021.0.8</spring-cloud.version> </properties> ... <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
-
5. Introduce dependency packages into the gateway module
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> </dependencies>
-
6.
application.properties
configuration of gateway moduleserver.port=8000 spring.application.name=gateway server.servlet.context-path=/gateway spring.main.web-application-type=reactive # Register a service spring.cloud.gateway.routes[0].id=system spring.cloud.gateway.routes[0].uri.=http://localhost:9001 spring.cloud.gateway.routes[0].predicates[0].name=Path spring.cloud.gateway.routes[0].predicates[0].args[0]=/system/** # Register a service spring.cloud.gateway.routes[1].id=file spring.cloud.gateway.routes[1].uri.=http://localhost:9000 spring.cloud.gateway.routes[1].predicates[0].name=Path spring.cloud.gateway.routes[1].predicates[0].args[0]=/file/**
-
7. Start three projects respectively to test whether individual interface requests and routing requests using the gateway are passed.
http://localhost:9000/file/hello
http://localhost:9001/system/hello1
http://localhost:8000/file/hello
http://localhost:9000/system/hello1
2. Installation of nacos
-
1. Download from the official website address. Download different packages according to different computer systems. Unzip and enter the
bin
directory. The prerequisite is thatjdk
must be installed on the computer.# This is a window computer startup.cmd -m standalone
-
2. Install using
docker
docker search nacos docker pull nacos/nacos-server
mkdir -p /home/docker/nacos/conf mkdir -p /home/docker/nacos/data mkdir -p /home/docker/nacos/logs chmod a + w /home/docker/nacos
docker run -it --name nacos \ -p 8848:8848 \ -e MODE=standalone \ -v /home/docker/nacos/conf/data:/home/nacos/conf/data \ -v /home/docker/nacos/conf/logs:/home/nacos/conf/logs \ -d nacos/nacos-server
If authentication is required, please add the
application.properties
filedocker run -it --name nacos \ -p 8848:8848 \ -e MODE=standalone \ -v /home/docker/nacos/conf/application.properties:/home/nacos/conf/application.properties \ -v /home/docker/nacos/conf/data:/home/nacos/conf/data \ -v /home/docker/nacos/conf/logs:/home/nacos/conf/logs \ -d nacos/nacos-server
3. Use nacos
as configuration center
-
1. Add dependency packages to the project that needs to obtain
nacos
<!-- nacos configuration --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <version>2.2.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> </dependency>
-
2. Here we introduce the
system
module and create abootstrap.properties
file in theresources
directory.# The name of the registration center spring.application.name=system # Start the environment, nacos will read different configurations according to the environment dataId:system-dev.properties spring.profiles.active=dev # The address of nacos spring.cloud.nacos.config.server-addr=localhost:8848 #Configure center file suffix name spring.cloud.nacos.config.file-extension=properties
-
3. Create a configuration file on the
nacos
control paneltest.nacos=hello1234566
-
4. Add the configuration of
test.nacos
in the project’sapplication.properties
. The value is arbitrary.server.port=9001 #spring.application.name=system server.servlet.context-path=/system test.nacos=hello
-
5. Get the configuration file from the controller of
system
package com.example.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RefreshScope public class HelloController {<!-- --> @Value("${test.nacos}") private String nacosName; @GetMapping("hello1") public String hello() {<!-- --> System.out.println(nacosName + "====="); return "System Hello"; } }
-
6. Modify the configuration file on
nacos
to see if the latest configuration data is obtained.
4. Multi-environment configuration
-
1. Manually add startup variables
-
2. Configure production startup
-Dspring.profiles.active=prod
-
3. Copy a production configuration file in
nacos
-
4. Modify the data of
system-prod.properties
and start thesystem
project
- 5. Request the interface to see if the configuration file read is the production configuration file.
5. Namespace
-
1. Maybe you only have one project in a company, so you can directly use the default namespace of
public
innacos
. If a company has multiple projects, each project There are different configuration files. At this time, namespaces must be used. To simply understand, a project is a namespace. -
2. For example, I create a namespace of
test1
and create 2 configuration files in it -
3. Use the namespace in the
spring-boot
project to read the configuration file# The name of the registration center spring.application.name=system # Start the environment, nacos will read different configurations according to the environment dataId:system-dev.properties spring.profiles.active=dev # The address of nacos spring.cloud.nacos.config.server-addr=localhost:8848 #Configure center file suffix name spring.cloud.nacos.config.file-extension=properties # Read namespace configuration spring.cloud.nacos.config.namespace=test1
6. Use nacos
to implement the registration center
-
1. Because all modules must be registered in
nacos
, dependency packages are directly introduced under the module. All dependencies in the configuration center above can also be imported into the module.<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.2.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> </dependency>
-
2. Just add the following 2 lines of code to the
bootstrap.properties
file# Service registration spring.cloud.nacos.discovery.server-addr=localhost:8848 spring.cloud.nacos.discovery.namespace=test1
-
3. If it is currently a gateway module, you need to add an additional load balancing dependency.
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> </dependency> </dependencies>
-
4. Start the service and view the
nacos
control panel -
5. To start one more
system
project, just change the startup port number when starting.
-
6. At this time, you can modify the gateway address from the previous
localhost
tolb
server.port=8000 spring.application.name=gateway server.servlet.context-path=/gateway spring.main.web-application-type=reactive # Register a service spring.cloud.gateway.routes[0].id=system #spring.cloud.gateway.routes[0].uri.=http://localhost:9001 spring.cloud.gateway.routes[0].uri=lb://system spring.cloud.gateway.routes[0].predicates[0].name=Path spring.cloud.gateway.routes[0].predicates[0].args[0]=/system/** # Register a service spring.cloud.gateway.routes[1].id=file #spring.cloud.gateway.routes[1].uri.=http://localhost:9000 spring.cloud.gateway.routes[1].uri=lb://file spring.cloud.gateway.routes[1].predicates[0].name=Path spring.cloud.gateway.routes[1].predicates[0].args[0]=/file/**
-
7. Test whether it is possible to use the gateway request
7. Improve openfeig
-
1. Use openfeig to request other services
-
2. Change from the previous writing method to the following writing method
@FeignClient(name = "system", url = "http://localhost:9001/system") public interface FeignSystemHello {<!-- --> /** * When the hell1 method is called elsewhere, the http://localhost:9001/system/hello route will be called. * @return */ @GetMapping("hello1") String hello1(); }
@FeignClient("system") public interface FeignSystemHello {<!-- --> @GetMapping("system/hello1") String hello1(); }
-
3. Test request address
http://localhost:8000/file/systemHello
-
4. The above may report an error, and you need to exclude something in the
pom.xml
in the root directory.<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.2.3.RELEASE</version> <exclusions> <exclusion> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </exclusion> </exclusions> </dependency>
-
5. Use environment variables in the test controller of
system
to obtain the current port.@RestController @RefreshScope public class HelloController {<!-- --> @Value("${test.nacos}") private String nacosName; @Autowired Environment environment; @GetMapping("hello1") public String hello() {<!-- --> String port = environment.getProperty("local.server.port"); System.out.println(nacosName + "====="); return "System Hello" + port; } }
8. Modify nacos
to log in using account and password
- 1. Modify
/nacos/conf/application.properties
-
2. But to enable the login function, you need to configure the following values, online tool
nacos.core.auth.caching.enabled=true nacos.core.auth.server.identity.key=nacos nacos.core.auth.server.identity.value=nacos nacos.core.auth.plugin.nacos.token.secret.key=OGQ2ZTNmMDQ4MjVjNGQ0OTg4YzZjMGE1NDE4MDgwMTE=
-
3. Enter the desktop and modify the account and password.