brief introduction
Eureka is Netflix open source rest-based service governance scheme, which is divided into Server and Client, Server is the registry, and other micro-services are registered and discovered by connecting Server through Client.
Project introduction
- sc-parent, parent module
- sc-provider, provider module
- sc-eureka, Registry
- sc-consumer-discovery, consumer module
Build parent module
Create the parent module sc-parent, pom.xml:
<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> <groupId> com.cf</groupId> <artifactId>sc-parent</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <!-- inherit Spring Boot Default value --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <!-- reduce maven-jar-plugin Plug-in version to prevent version mismatch pom.xml First line error reporting --> <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version> </properties> <dependencies> <!-- Add to web Application dependency --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <!-- SpringCloud rely on --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR2</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <!-- Executable jar Package plug-in --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!-- Specifies what compiler plug-ins will use jdk Edition --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
Establishment of registry
1. Create the sub-module project sc-eureka, pom.xml under the parent module:
<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>com.cf</groupId> <artifactId>sc-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>sc-eureka</artifactId> <dependencies> <!-- Eureka Server-side dependency --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> </project>
2. Create the startup class eureka.EurekaApplication:
package eureka; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; //Declare this class as the entry to the SpringBook service @SpringBootApplication //Declare the microservice as a registry, providing service discovery and registration functions @EnableEurekaServer public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }
3. Create a configuration file/src/main/resources/application.yml:
server: port: 8080 #Current Service Port eureka: instance: hostname: localhost #Current Eureka instance hostname client: registerWithEureka: false #Represents not registering with the registry fetchRegistry: false #Represents that this client does not need to obtain Eureka registry information from the Eureka registry serviceUrl: defaultZone: http://localhost:8080/eureka/ ##eureka external address (client connection address)
Other configuration information can refer to two configuration classes, Eureka Instance ConfigBean and Eureka ClientConfigBean.
4. Run the startup class EurekaApplication and visit http://localhost:8080/ in the browser. The following chart shows that the registry has been successfully built:
Provider and Service Registration
1. Create a sub-module project sc-provider under the parent module, pom.xml:
<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>com.cf</groupId> <artifactId>sc-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>sc-provider</artifactId> <dependencies> <!-- Eureka Client dependency, used to connect the server for service registration and discovery --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> </project>
2. Create the startup class provider.ProviderApplication:
package provider; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); } }
3. Create Controller: provider.controller.BookController
package provider.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RequestMapping("/book") @RestController public class BookController { @GetMapping("/list") public String getBookList(){ //Simulate the return of data from service return "[\"Java Beginning to Abandoning\",\"C++Beginning to Abandoning\",\"Python Beginning to Abandoning\",\"C Beginning to Abandoning\"]"; } }
4. Create a configuration file/src/main/resources/application.yml:
server: port: 8081 spring: application: name: sc-provider #The service name registered in the Eureka registry corresponds to the Application column on the Eureka interface eureka: client: serviceUrl: defaultZone: http://localhost:8080/eureka/ # Registry Access Address instance: preferIpAddress: true #Represents registering your IP with the Eureka registry. The default is false, meaning that hostname is registered with the registry
5. Start the registry sc-eureka and the provider sc-provider in turn. When the provider starts, it will register its own information to the Eureka registry and visit http://localhost:8080/ in the browser. The provider sc-provider has registered in the registry.
Consumer and service discovery
1. Create the sub-module project sc-consumer-discovery, pom.xml under the parent module:
<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>com.cf</groupId> <artifactId>sc-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>sc-consumer-discovery</artifactId> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> </project>
2. Create the startup class consumer. ConsumerDiscovery Application:
package consumer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication public class ConsumerDiscoveryApplication { public static void main(String[] args) { SpringApplication.run(ConsumerDiscoveryApplication.class, args); } @Bean public RestTemplate restTemplate(){ return new RestTemplate(); } }
3. Create Controller: consumer. controller. ConsumerDiscovery Controller that invokes provider services
package consumer.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class ConsumerDiscoveryController { @Autowired private DiscoveryClient discoveryClient; @Autowired private RestTemplate restTemplate; @GetMapping("/getBookList") public String getBookList(){ //Obtain instance information by service name List<ServiceInstance> list = discoveryClient.getInstances("sc-provider"); if (list != null && list.size() > 0 ) { //Call the service and return the service result return restTemplate.getForObject(list.get(0).getUri() + "/book/list", String.class); } return null; } }
4. Create application.yml:
server: port: 8082 spring: application: name: sc-consumer-discovery eureka: client: registerWithEureka: false #In this example, consumers do not provide services, so there is no need to register in the registry. In practical applications, consumers may also be providers. serviceUrl: defaultZone: http://localhost:8080/eureka/
5. Start the registry sc-eureka, provider sc-provider and consumer sc-consumer-discovery in turn. When consumers start, they will query the list of available services and their network addresses from the registry. The results of direct access providers and consumer invocation providers are as follows:
summary
In traditional applications, the provider's network address is hard coded in the code, which leads to high coupling between the provider and the consumer. When the provider's network address changes, it needs to modify the consumer configuration and redistribute. Eureka plays a decoupling role. Providers register in the Eureka registry. Consumers get the network address of the provider from the Eureka registry and call it. When the network address of the provider changes, they register again in the registry.