A. Eureka Introduction
Eureka has three roles
1. the Register Server : service registry, which is a Eureka Server, service registration and discovery
2. Provider Server : service providers, it is a Eureka Client, to provide services
3. Consumer Server : consumer service, it is a Eureka Client, Consumer Services
Service consumption process:
1. The need for a service registry Eureka server
2. The service provider Eureka Client registered with the service registry Eureka Server, will own the IP, service name submitted to the registry Eureka server by Rest API
3. Service consumers, but also registered with the service registry Eureka Server, at the same time to get a list of service registration, the list contains all the registered service information to the service center, the information contained in the IP address of the service, so consumers http service can be remote call consumer service providers through the IP address
Two projects to build
1. First create a new maven project
Deleted src directory, modify the pom pom packaging is
2. The new Eureka Server Registry Module on new projects above
You've created, jar package to be downloaded, add application.yml file
server: port: 8761 # Eureka-server 服务端接口 eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
Next you need to add on the entrance class @EnableEurekaServer turn Eureka Server features
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
So far, Eureka Server set up is completed by http: // localhost: 8761 to access, but have not yet registered any instance
3. Eureka Client write
Ibid, create a new Module, select Eureka Discovery Client in spring cloud discovery
After a good add support for new web in the pom file
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
Add a profile application.yml, the Eureka client registered on the server side
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ server: port: 8762 spring: application: name: eureka-client
Open function on the inlet @EnableEurekaClient
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class EurekaClientApplication { public static void main(String[] args) { SpringApplication.run(EurekaClientApplication.class, args); } }
Write a test interface
import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * @author Devin Zhang * @className HelloController * @description TODO * @date 2020/3/17 14:58 */ @RestController public class HelloController { @Value("${server.port}") private String port; @GetMapping("/sayHello") public String sayHello(String msg) { return "hello " + msg + ", my port is :" + port; } }
At this point the client Eureka Client build complete, start at Eureka Server can see the end of the service has been registered
By calling http: // localhost:? 8762 / sayHello msg = devin can see the client interface can also be a normal visit