1, Create local project
- Because microservice is an aggregate project, we need to create a blank folder locally as the local project workspace. All project modules are in the workspace. Remember that it must be a blank folder without anything in it or any Chinese on the path
- Open idea, select file - > open, go in and select the newly created folder to open our workspace
- Click file – > New – > project in the open new space to create a maven project. After the new project is created, delete the src folder without deleting it. However, in order to avoid the embarrassment caused by the wrong location of the code written later, it is best to delete it. My project here has been completed and deleted, so I won't demonstrate it
- Create a sub Module as the service consumer, select 01 SCA with the mouse and right-click
- Refer to step 4 to create a sub Module service provider named SCA provider and a sub Module gateway service named SCA gateway. The project structure is as follows
2, Register service with Nacos
1. Aggregation projects rely on version control as a whole
Set the dependency version in the parent project 01 SCA, and open the pom.xml file of 01 SCA to add the required dependency. Why
<!--First step: Define the version management of the core dependencies in the subproject(be careful,Just version management)--> <dependencyManagement> <dependencies> <!--spring boot Core dependent version definition(spring Official definition)--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.3.2.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!--Spring Cloud Microservice specification(from spring Official definition)--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR9</version> <type>pom</type><!--If scope yes import,type Must be pom--> <scope>import</scope><!--Introducing third-party dependent version design--> </dependency> <!--Spring Cloud alibaba Dependent version management (Refer to official instructions)--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.2.6.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <!--Step 2: Add the required public dependencies of the subproject--> <dependencies> <!--lombok rely on,If necessary in the subproject lombok,No need to import--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>provided</scope><!--provided Indicates that this dependency is only valid at compile time--> </dependency> <!--Unit test dependency,When unit test is required in subproject,There is no need to introduce this dependency again--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope><!--test Indicates that only test Use this dependency under the directory--> <exclusions> <exclusion><!--Eliminate unnecessary dependencies--> <groupId>org.junit.vintage</groupId> <artifactId>junit-jupiter-vintage</artifactId> </exclusion> </exclusions> </dependency> <!--Other dependencies...--> </dependencies> <!--Step 3: Define the unified compilation and running version of the current project module and subproject--> <build><!--Project build configuration,We are based on maven Complete the compilation of the project,test,Packaging and other operations, All based on pom.xml Complete the operation of this column,However, the compiled and packaged configurations are written to build element Internal,The specific compilation and packaging configuration,Need again plugin To achieve,plugin Element is not required,maven There is a default plugin to configure,Common plug-ins can be viewed in the local library--> <plugins> <!--adopt maven-compiler-plugin Plug in settings item Unified jdk Compile and run versions--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <!--If the local library does not have this version,A red font error will appear here,Go to the official warehouse and find that there are no 3.8.2 Version dependency, now use the latest version 3.8.1--> <version>3.8.1</version> <configuration> <source>8</source> <target>8</target> </configuration> </plugin> </plugins> </build>
2. Add Service Dependencies
Add the dependencies of consumer service SCA consumer and producer service SCA provider, and open their pom.xml to add the dependencies below
<dependencies> <!--Web service--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--Registration and discovery of services(We're going to talk about service registration nacos)--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencies>
3. Configure Nacos service registration
- Create a new application.yml file in the resources folder under the SCA provider project, and add the following configuration
server: port: 8081 #Set the port number of the service spring: application: name: sca-provider #Service name must be configured for service registration cloud: nacos: discovery: server-addr: localhost:8848 #Set the access address of the service to be consistent with that of nacos
- Referring to the above, create an application.yml file under the SCA consumer project, and add the following configuration
server: port: 8090 spring: application: name: sca-consumer #Service name must be configured for service registration cloud: nacos: discovery: server-addr: localhost:8848
- Create a startup class for both SCA consumer and SCA provider projects
4. Verify whether the service registration is successful
- Start nacos first, and refer to the startup mode https://blog.csdn.net/qq_44413478/article/details/121479001
- Start the SCA provider and SCA consumer projects by using the two newly created startup classes above. Refresh the list of nacos and find that the two services have been registered successfully
3, Service invocation demo
- Create an object in the producer service SCA provider as the called resource
- Create a class in the consumer service SCA consumer to call the resources in the producer above
It should be noted here that the RestTemplate object is used for dependency injection. This object is an http request tool provided by spring. It can easily use common REST requests. It is built-in in spring, so we don't need to give it to the spring container and inject the dependency directly
- Restart the projects of SCA provider and SCA consumer and enter in the browser http://localhost:8090/consumer/doRestEcho1
To sum up, it is successful to invoke producer resources in consumer services
summary
The content of this section is just to briefly understand the registration and discovery of nacos service station and the most basic call of resources between services, as well as the normal call methods, which will be supplemented later