Create a service provider based on the spring clould _spring-cloud

Source: Internet
Author: User
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/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.