In the last one, we have set up a registration center spring cloud micro service, but only one registry is not enough.
Then we have to try to provide services.
Note: This version of the development environment is a series of java1.8, spring boot2.x IDE for the Intelli IDEA.
Service Provider
Suppose we now have a demand, need an interface, when we pass a name, it will return a greeting word such as incoming body
, input was hello body!
.
Create a spring cloud projects
In the specific procedure is not described in detail here, you may be required to see the [spring cloud services practice two micro] contents.
The following steps:
- Idea before selecting the directory
firstCloud
, right-click> New> Module, selectSpring Initialzr
. - Engineering Group
- Project Metadata to the Group
com.xingyys
, Artifact isproducer
- Direct next, until completion.
pom configuration
In the producer directory, modify pom.xml file, add dependencies:
<?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 http://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.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xingyys</groupId>
<artifactId>producer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>producer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Profiles
Modify the configuration file application.properties under resources.
spring.application.name=producer
server.port=9000
eureka.client.service-url.defaultZone=http://localhost:8000/eureka/
Startup class
Modify ProducerApplication.java com.xingyys.producer under:
@SpringBootApplication
@EnableDiscoveryClient
public class ProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class, args);
}
}
With @EnableDiscoveryClient
, it means the producer has registered the function of the service.
Add Controller
Here add a controller to provide services com.xingyys.producer / controller / HelloController.java we need:
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(@RequestParam String name) {
return "Hello " + name + " !";
}
}
Compile and run
producer code and configuration files so that you can, the next run producer to see results:
cd producer
mvn clean package -Dmaven.test.skip=true
java -jar target/producer-0.0.1-SNAPSHOT.jar
访问http://localhost:9000/hello?name=xingyys
,返回Hello xingyys !
,表示注册成功,producer可以提供服务了.