Eureka of SpringCloud
SpringCloud provides developers with tools to quickly build distributed systems, including configuration management, service discovery, circuit breakers, routing, micro-agents, event buses, global locks, decision campaigns, distributed sessions, and so on. The utility model has the advantages of simple configuration, quick start-up, mature ecology and easy application. But it has a strong dependence on SpringBoot and needs some foundation, but SpringBoot can get started in two hours. In addition, if you do not understand the "micro-service architecture", you can search for the "micro-service architecture" through the search engine. In addition, this is the version of SpringCloud for Greenwich.SR2,JDK, version 1.8 for Spring Boot, and version for 2.1.7.RELEASE.
Create a new parent project, create a Maven parent project lovincloud, to facilitate version management, and then delete the src folder
Add pom dependencies and versions of SpringCloud and SpringBoot
Org.springframework.boot spring-boot-starter-parent 2.1.7.RELEASE com.eelve.lovincloudlovincloud1.0-SNAPSHOTpomlovincloud http://maven.apache.org UTF-8 Greenwich.SR2 1.8 org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} Pom import org.springframework.boot spring-boot-maven-plugin II. Add a registry
Here, we need to use the component on Spring Cloud Netflix Eureka, eureka is a service registration and discovery module.
Create a new subproject lovin-eureka-server as the registry for the service
Lovincloud com.eelve.lovincloud 1.0-SNAPSHOT4.0.0lovin-eureka-serverjareurekaserver0.0.1eureka server org.springframework.cloud spring-cloud-starter-netflix-eureka-server org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} Pom import org.springframework.boot spring-boot-maven-plugin then adds @ EnableEurekaServer annotation to the startup class: package com.eelve.lovin
Import org.springframework.boot.SpringApplication
Import org.springframework.boot.autoconfigure.SpringBootApplication
Import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer
/ * *
@ ClassName LovinEurekaServerApplication@Description TDO@Author zhao.zhilue@Date 2019-8-15 16:20@Version 1.0 percent /
@ EnableEurekaServerbr/ > * /
@ EnableEurekaServer
Public class LovinEurekaServerApplication {
Public static void main (String [] args) {
SpringApplication.run (LovinEurekaServerApplication.class,args)
}
}-eureka is a highly available component, it has no backend cache, and each instance needs to send a heartbeat to the registry after registration (so it can be done in memory). By default, erureka server is also an eureka client, and you must specify a server. Eureka server's configuration file appication.yml:~~~yamlspring:application:naem: lovineurkaserver # service module name server:port: 8881 # Eureka port number eureka:instance:hostname: localhost # set the host address of eureka client:registerWithEureka: false # indicates whether to register yourself with Eureka Server. The default is true. Since the current application is Eureka Server, setting it to falsefetchRegistry: false # indicates whether to obtain registration information from Eureka Server. The default is true. Because this is a single-point Eureka Server, there is no need to synchronize the data of other Eureka Server nodes, so it is set to falseserviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ # Eureka server address. Both query service and registration service depend on this address. Multiple addresses can be separated by commas (in English), and a service consumer can be added.
Create a new subproject lovin-eureka-server as the registry for the service
Org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-maven-plugin, a consumer side of lovincloud com.eelve.lovincloud 1.0-SNAPSHOT4.0.0lovin-eureka-clientjareurekaclient0.0.1eureka, then launches the class Add @ EnableEurekaClient comment on: package com.eelve.lovin
Import org.springframework.boot.SpringApplication
Import org.springframework.boot.autoconfigure.SpringBootApplication
Import org.springframework.cloud.netflix.eureka.EnableEurekaClient
/ * *
@ ClassName LovinEurekaClientApplication@Description TDO@Author zhao.zhilue@Date 2019-8-15 16:37@Version 1.0 percent /
@ SpringBootApplicationbr/ > * /
@ SpringBootApplication
Public class LovinEurekaClientApplication {
Public static void main (String [] args) {
SpringApplication.run (LovinEurekaClientApplication.class,args)
}
}-then we need to connect to the server as follows: ~ yamlserver:port: 8801 # Service port number spring:application:name: lovineurkaclient # Service name eureka:client:serviceUrl: defaultZone: http://localhost:8881/eureka/ # registered to the eureka service address, create a new Controller and write a test interface package com.eelve.lovin.controller
Import com.eelve.lovin.config.ServerConfig
Import org.springframework.beans.factory.annotation.Autowired
Import org.springframework.web.bind.annotation.RequestMapping
Import org.springframework.web.bind.annotation.RestController
/ * *
@ ClassName HelloController@Description TDO App default access Interface @ Author zhao.zhilue@Date 16:45 on 2019-8-15
@ Version 1.0 percent /
@ RestControllerbr/ > * /
@ RestController
@ Autowired
ServerConfig serverConfig
@ RequestMapping ("hello")
Public String hello () {
Return serverConfig.getUrl () + "#" + HelloController.class.getName ()
}
}
#, the server and client that start the registry respectively visit localhost:8881 to view the results! [registry] (https://cache.yisu.com/upload/information/20200312/76/254765.jpg) here we can see that the client has been successfully registered with the server, and then we access the test interface! [192202] (https://cache.yisu.com/upload/information/20200312/76/254766.jpg) can see that the access has been successful, and the construction of Eureka has been completed. # Wu, join the security configuration in the Internet, we generally consider security, especially the registration center that manages the service So we can use * * spring-boot-starter-security** to make security restrictions-add * * pom dependencies of * * spring-boot-starter-security** to * * pomorg.springframework.bootspring-boot-starter-security modify configuration file spring:application:naem: lovineurkaserver # service module name security:basic:enabled: trueuser:name: lovinpassword: ${REGISTRY_SERVER_PASSWORD:lovin} server:port: 8881 # set eureka Port number eureka:instance:hostname: localhost # sets the host address of eureka metadata-map:user.name: ${security.user.name} user.password: ${security.user.password} client:registerWithEureka: false # indicates whether to register yourself with Eureka Server The default is true. Since the current application is Eureka Server, setting it to falsefetchRegistry: false # indicates whether to obtain registration information from Eureka Server. The default is true. Because this is a single point of Eureka Server, there is no need to synchronize the data of other Eureka Server nodes, so it is set to falseserviceUrl:defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/ # Eureka server address, which is dependent on the query service and registration service. Multiple addresses can be divided by comma (English) to add security configuration package com.eelve.lovin.config
Import org.springframework.context.annotation.Configuration
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
/ * *
@ ClassName SecurityConfig@Description TDO@Author zhao.zhilue@Date 14:13 on 2019-8-16
@ Version 1.0 percent /
@ EnableWebSecuritybr/ > * /
@ EnableWebSecurity
@ Override
Protected void configure (HttpSecurity http) throws Exception {
Http.csrf () .disable ()
Http.authorizeRequests ()
.antMatch ("/ css/**") .permitAll ()
.anyRequest () .authenticated ()
.and ()
.formLogin ()
.and ()
.httpBasic ()
Super.configure (http)
}
}
-add pom dependency of * * spring-boot-starter-security** to * * pomorg.springframework.bootspring-boot-starter-security modify yaml configuration file server:port: 8801 # Service port number spring:application:name: lovineurkaclient # Service name security:basic:enabled: trueuser:name: lovinpassword: ${REGISTRY_SERVER_PASSWORD:lovin} eureka:client:serviceUrl:defaultZone: http://lovin:lovin@localhost:8881/eureka/ # eureka service address registered to instance:leaseRenewalIntervalInSeconds: 10health-check-url-path: / actuator/healthmetadata-map:user.name: lovinuser.password: lovin add security configuration package com.eelve.lovin.config
Import org.springframework.context.annotation.Configuration
Import org.springframework.security.config.annotation.web.builders.HttpSecurity
Import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
/ * *
@ ClassName SecurityConfig@Description TDO@Author zhao.zhilue@Date 14:13 on 2019-8-16
@ Version 1.0 percent /
@ Configurationbr/ > * /
@ Configuration
@ Override
Protected void configure (HttpSecurity http) throws Exception {
Http.authorizeRequests (). AnyRequest (). PermitAll ()
.and () .csrf () .disable ()
}
}
-in addition, in order to test multi-client registration, we can modify and create a new configuration file for the client, and then enable the multi-node operation of IDEA. Check * * Allow parallel runners registration! [192203] as shown in the following figure. (https://cache.yisu.com/upload/information/20200312/76/254767.jpg)- then in order to distinguish which node is the request, we can add the acquisition port ~ ~ javapackage com.eelve.lovin.config.
Import org.springframework.boot.web.context.WebServerInitializedEvent
Import org.springframework.context.ApplicationListener
Import org.springframework.stereotype.Component
Import java.net.InetAddress
Import java.net.UnknownHostException
/ * *
@ ClassName ServerConfig@Description TDO@Author zhao.zhilue@Date 12:03 on 2019-8-18
@ Version 1.0 percent /
@ Componentbr/ > * /
@ Component
Private int serverPort
Public String getUrl () {
InetAddress address = null
Try {
Address = InetAddress.getLocalHost ()
} catch (UnknownHostException e) {
E.printStackTrace ()
}
Return "http://"+address.getHostAddress() +": "+ this.serverPort
}
@ Override
Public void onApplicationEvent (WebServerInitializedEvent event) {
This.serverPort = event.getWebServer () .getPort ()
}
}
-then we restart the server and two clients at a time. When we visit http://localhost:8881/, we can see that we have been asked to enter a user name and password here, indicating that * * spring-boot-starter-security** has been configured successfully. At this point, we enter the configured user name: lovin and password: lovin! [192204] (https://cache.yisu.com/upload/information/20200312/76/254772.jpg) here we can see that it has been successful, so the configuration of Eureka here has been all successful. * [the last part is the source code of this blog. Welcome to follow this set of SpringCloud practices] (https://github.com/lovinstudio/lovincloud)