Simple steps to create a Spring-boot micro service provider. Suppose you want to create a user service:
1, the introduction of Dependence (Pom.xml):
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!--other dependencies slightly ... -->
</dependencies>
<dependencyManagement>
<dependencies>
<dependency >
<groupId>org.springframework.cloud</groupId>
<artifactId> spring-cloud-dependencies</artifactid>
<version>Camden.SR3</version>
<type> pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2. Specify the registry in the configuration file (APPLICATION.YML): Assume that there are three registered centers that comprise a highly available cluster:
Eureka:
Client:
serviceurl:
defaultzone:http://192.168.0.9:58000/eureka/,http://192.168.0.10:58000 /eureka/,http://192.168.0.18:58000/eureka/
instance:
preferipaddress:true
3, the project configuration activates the output of the service information in the controller (through the @enablediscoveryclient annotation):
@EnableDiscoveryClient
@SpringBootApplication Public
class UserApplication {public
static void main ( String[] args) {
springapplication.run (userapplication.class, args);
}
}
4, through the creation of controller to provide services:
@RestController public
class Usercontroller {
protected final Logger Logger = Loggerfactory.getlogger (getclass ());
@Autowired
userservice userservice;
/**
* Get phone number
* @param userId
* @return Phone number *
*
@RequestMapping (value = "/getphonenobyuserid") based on user ID ', method = Requestmethod.get ' public
String getphonenobyuserid (@RequestParam Integer userId) {
logger.debug ("Getphonenobyuserid received.") userid={} ", userId);
Return Userservice.getphonenobyuserid (userId);
}
Service and other code slightly ...
5, start, check if the registry can see the User service: access to any of the three registries: Http://192.168.0.9:58000/