By annotating @enableeurekaclient, you indicate that you are a eurekaclient, also a provider of services.
The startup class code is as follows:
1 PackageCom.cloud.microservice.demo;2 3 Importorg.springframework.boot.SpringApplication;4 Importorg.springframework.boot.autoconfigure.SpringBootApplication;5 Importorg.springframework.cloud.client.discovery.EnableDiscoveryClient;6 Importorg.springframework.cloud.netflix.eureka.EnableEurekaClient;7 ImportOrg.springframework.context.annotation.ComponentScan;8 9 @SpringBootApplicationTen @EnableDiscoveryClient One @EnableEurekaClient A@ComponentScan ("Com.cloud.microservice.demo") - Public classdemoproviderapplication { - the Public Static voidMain (string[] args) { -Springapplication.run (demoproviderapplication.class, args); - } -}
In order to separate the start class and interface, I created a new provider package here, put the rest interface Userprovider.java under this path, the project structure is as follows:
The Userprovider.java code is as follows:
1 PackageCom.cloud.microservice.demo.provider;2 3 Importorg.springframework.cloud.netflix.feign.FeignClient;4 Importorg.springframework.web.bind.annotation.RequestMapping;5 ImportOrg.springframework.web.bind.annotation.RequestMethod;6 ImportOrg.springframework.web.bind.annotation.RequestParam;7 ImportOrg.springframework.web.bind.annotation.RestController;8 9@FeignClient (name = "Ms-demo-provider")Ten @RestController One@RequestMapping ("/demo/user") A Public classUserprovider { - -@RequestMapping (value= "/1.0/findall", method=requestmethod.get) the Publicstring FindAll (@RequestParam string name) - { - return"Hello,this is" +name; - } +}
The configuration file application.yml is as follows: Registerwitheureka indicates whether to register itself to the Eureka server, because the current application is Eureka Service, in order to see the registration information on the Eureka service, this is set to true;fetchregistry representation Whether to obtain registration information from the Eureka Server.
1 Server:2port:80903 4 Eureka:5 instance:6 Hostname:localhost7 Client:8Registerwitheureka:true9Fetchregistry:falseTen serviceurl: OneDefaultzone:http://${eureka.instance.hostname}:9090/eureka/ A - Spring: - Application: theName:ms-demo-provider
Start the project, open the http://localhost:9090, that is, the Eureka server URL, you will find a service is registered in the service, the service name is Ms-demo-provider, the port is 8090.
Enter Http://localhost:8090/demo/user/1.0/findAll?name=helloworld in the browser address bar, for example, to indicate that the service interface is functioning properly.
Springcloud Series Research---Eureka service registration