A, Eurake profile
springcloud core component of Eureka is responsible for micro-service architecture, service management functions responsible for each instance of service registration and discovery.
Eureka includes server and client components. Server, also referred to as a service registry, to the registration and discovery service. The client component includes service consumers and service producers. In the application is running, the producers registered with the service registry own service instance, when consumers need to call this service, the registry will start to find the corresponding producer, then you can achieve the service of the consumer.
The figure is a relatively simple registration service consumption process, but also an essential component of running processes Eureka, Eureka point in popular parlance as a real estate agent, landlord want to rent out a house information linked to real estate agents, real estate agents and tenants by information resource rent to own house favorite
Second, the project to build a registry Eurake
1. Create a new project SpringBoot shown:
Next finsh point above the last point to complete the project creation
2. Import eureka dependent, specific pom follows:
<?xml version="1.0" encoding="UTF-8"?> <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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.regedit</groupId> <artifactId>regedit</artifactId> <version>0.0.1-SNAPSHOT</version> <name>regedit</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR3</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
3, the application starts to add EurekaServer class notes, so that when SpringBoot start, it will start EurekaServer registry
package com.regedit; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class RegeditApplication { public static void main(String[] args) { SpringApplication.run(RegeditApplication.class, args); } }
4. Configure application.property or application.yml, yml format I used here, the main configuration parameters registry information of Eureka, Eureka Client after each registration, will send a heartbeat to the Server side. The default Eureka Server itself is a Client, you need to specify registry, which registerWithEureka: false fetchRegistry: false representation itself is a Eureka Server, is a registered center. Configuration is as follows:
server: port: 8762 eureka: instance: hostname: 127.0.0.1 server: enable-self-preservation: false eviction-interval-timer-in-ms: 3000 response-cache-update-interval-ms: 3000 response-cache-auto-expiration-in-seconds: 180 client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ logging: config: classpath:logback-spring.xml spring: security: user: name: admin password: 123456
5. In order to use this http: // $ {user}: $ {password} @ $ {host}: $ {port} / eureka / Log this spring security cited in the introduction of spring-boot- file pom starter-security authentication code to open the package as follows:
package com.regedit; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @EnableWebSecurity public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { //开启认证 //为了可以使用 http://${user}:${password}@${host}:${port}/eureka/ 这种方式登录,所以必须是httpBasic http.csrf().disable().authorizeRequests().anyRequest().authenticated().and().httpBasic(); } }
6, this configuration and project build has been completed, start the application program code is structured as follows:
7, visit: http: // localhost: 8762 Enter the login name and password interface as follows:
Three, Eureka Client-side to create and register
1, the new SpringBoot project, import dependence, Pom File Server and end in the same application startup class EurekaClient add annotations to indicate that this is a client, as follows:
@SpringBootApplication @EnableDiscoveryClient public class SystemModuleJdbcApplication { public static void main(String[] args) { SpringApplication springApplication = new SpringApplication(SystemModuleJdbcApplication.class); springApplication.addListeners(new InitUserRoleResources()); springApplication.run(args); } }
2. Add the configuration in the configuration file, it will register itself to a designated registration center to Eureka
server: port: 8081 servlet: context-path: /system max-http-header-size: 10240 tomcat: uri-encoding: UTF-8 max-threads: 500 max-connections: 10000 config: db: ip: 127.0.0.1 port: 3306 username: root password: root@123456 regcenter: ip: 127.0.0.1 prot: 8762 username: admin password: 123456 #当前使用配置 spring: application: name: systemCenter datasource: url: jdbc:mysql://${config.db.ip}:${config.db.port}/system_module?characterEncoding=utf8&useSSL=false&allowMultiQueries=true username: ${config.db.username} password: ${config.db.password} servlet: multipart: max-file-size: 20MB max-request-size: 100MB logging: config: classpath:logback-spring-test.xml eureka: client: register-with-eureka: true fetch-registry: true ## 注册服务中心的配置 service-url: defaultZone: http://${config.regcenter.username}:${config.regcenter.password}@${config.regcenter.ip}:${config.regcenter.prot}/eureka/
Where: defaultZone is the address registry, application.name is the application name, between the follow-up service call is usually through the application name and address of the application to call.
3. Start application to view the registry and found that this instance is registered came to this registry and examples of our services registered basically completed, as shown below: