Spring Cloud Eureka is the Service governance module under the spring Cloud Netflix project. The spring cloud Netflix project is one of Spring cloud's sub-projects, primarily packaged with Netflix's range of open source products, which provides a self-configuring Netflix OSS integration for spring boot applications. With some simple annotations, developers can quickly configure common modules in the application and build large, distributed systems. It mainly provides modules including: Service Discovery (Eureka), Circuit breakers (Hystrix), intelligent routing (Zuul), Client load Balancing (Ribbon), etc.
Service Registration Center: Eureka-server
Create a new Springboot project: Eureka-server, whose pom.xml is configured as follows
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <projec T.reporting.outputencoding>utf-8</project.reporting.outputencoding> <java.version>1.8</ java.version> </properties> <dependencies> <dependency> <groupid>org.sp Ringframework.cloud</groupid> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> &L T;dependency> <groupId>org.springframework.cloud</groupId> <artifactId> Spring-cloud-dependencies</artifactid> <version>Dalston.SR1</version> < type>pom</type> <scope>import</scope> </dependency> </depen Dencies> </dependencymanaGement>
The ability to implement a service registry is simple, just use @enableeurekaserver annotations on the project's startup class Eurekaserverapplication
@EnableEurekaServer@SpringBootApplicationpublic class EurekaServerApplication{ public static void main(String[] args) { new SpringApplicationBuilder(EurekaServerApplication.class) .web(true).run(args); }}
By default, the service registry will also try to register itself as a client, so we need to disable its client registration behavior by adding the following information in the Application.properties configuration file:
Spring.application.name=eureka-server
server.port=1001
Eureka.instance.hostname=localhost
Eureka.client.register-with-eureka=false
Eureka.client.fetch-registry=false
Start Eurekaserverapplication and visit http://localhost:9001/to see the Eureka page.
Service Providers: Eureka-client
Each instance is registered to send a heartbeat to the registry, and when the client registers with the server, it provides some metadata, such as the host and port, the URL, the home page, and so on. Eureka server receives heartbeat messages from each client instance. If the heartbeat times out, the instance is typically removed from the registration server.
Create a new Springboot project: Eureka-client, whose pom.xml configuration is as follows:
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <projec T.reporting.outputencoding>utf-8</project.reporting.outputencoding> <java.version>1.8</ java.version> </properties> <dependencies> <dependency> <groupid>org.sp Ringframework.cloud</groupid> <artifactId>spring-cloud-starter-eureka</artifactId> < ;/dependency> <dependency> <groupId>org.springframework.boot</groupId> & lt;artifactid>spring-boot-starter-web</artifactid> </dependency> </dependencies> <DEP endencymanagement> <dependencies> <dependency> <GROUPID>ORG.SPRINGFR Amework.cloud</groupid> <artifactId>spring-cloud-dependencies</artifactId> <version>dalston.sr1</version> <type>pom</type> <scope>import</scope> </DEP Endency> </dependencies> </dependencyManagement>
It is also easy to implement a service provider, as long as you use @enableeurekaclient annotations on the project's startup class Eurekaclientapplication
@EnableEurekaClient@SpringBootApplicationpublic class EurekaClientApplication { public static void main(String[] args) { new SpringApplicationBuilder( EurekaClientApplication.class) .web(true).run(args); }}
The following configuration is available in Application.properties friends Penguin 2042849237
Spring.application.name=eureka-client
server.port=9002
eureka.client.serviceurl.defaultzone=http://localhost:9001/eureka/
Start the Eurekaclientapplication class
To refresh the http://localhost:9001/, we can see that our service provider has already registered with the Service Registration center.
Create a new Discoverycontroller use Discoveryclient.getservices () to get the registered service name and use @value to assign the information in the configuration file to the IP
@RestControllerpublic class DiscoveryController { @Autowired private DiscoveryClient discoveryClient; @Value("${server.port}") private String ip; @GetMapping("/client") public String client() { String services = "Services: " + discoveryClient.getServices()+" ip :"+ip; System.out.println(services); return services; }}
Visit: http://localhost:9002/client
Finally, explain the two annotations of @enableeurekaclient and @enablediscoveryclient
First of all, this two annotations can realize the function of service discovery, there are many implementations of discovery service in spring Cloud (Eureka, consul, zookeeper, etc.)
@EnableEurekaClient based on Spring-cloud-netflix. The service uses Eureka as the registration center, the use scene is relatively single.
@EnableDiscoveryClient based on Spring-cloud-commons. The service uses other registries.
Springcloud Service Registration and Discovery (ii)