In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge points about how to use Spring Cloud's Eureka to achieve service registration. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.
Eureka is an open source service registration discovery component of Netflix. Service discovery can be said to be the core function of micro-service architecture. After micro-service deployment, there must be service registration and discovery capabilities, and Eureka plays this role. If you have ever used dubbo, you must know that the service registration and discovery functions in dubbo are implemented in zookeeper.
Eureka is currently in version 2.x, and it has been officially announced that updates will no longer be maintained. But in fact, Eureka is already very stable, and there is no problem as a registry. Spring Cloud integrates Eureka and makes a perfect package. It is convenient for us to use simple configuration when developing with Spring boot.
There are three types of roles in the micro-service framework, namely, registry, service provider, and service consumer. Registry is the protagonist Eureka today. This article briefly explains the use of Spring Cloud Eureka, simulates the implementation of a single point and highly available registry, and briefly introduces how service providers and service consumers use the service registration and discovery functions provided by Eureka.
Version description
Java: 1.8
Spring Boot: 2.1.3.RELEASE
Spring Cloud: Finchley.SR2
So let's talk about the version, because the package name of the Finchley.SR2 version has changed from the previous version, so you should be careful when referencing the maven package.
Single point registration center
Create an Eureka registry
1. Reference the maven package, where
Org.springframework.cloud
Spring-cloud-dependencies
Finchley.SR2
Pom
Import
Org.springframework.cloud
Spring-cloud-starter-netflix-eureka-server
Org.springframework.boot
Spring-boot-starter-actuator
2. Create a new bootstrap.yml and configure the Spring cloud parameter
Spring:
Application:
Name: kite-eureka-center
Cloud:
Inetutils: # # Network card settings
IgnoredInterfaces: # # ignored Nic
-docker0
-veth.*
-VM.*
PreferredNetworks: # # priority network segment
-192.168
3. Create a new application.yml and configure the parameters
Server:
Port: 3000
Eureka:
Instance:
Hostname: eureka-center
Appname: registry
Client:
RegisterWithEureka: false # when single point is set to false to disable self-registration
FetchRegistry: false
ServiceUrl:
DefaultZone: http://localhost:3000/eureka
Server:
EnableSelfPreservation: false
EvictionIntervalTimerInMs: 4000
The difference between bootstrap.yml and application.yml:
Bootstrap.yml starts before application.yml
Bootstrap.yml configures name, spring.cloud.config.server.git.uri, and some encryption/decryption (encryption / decryption) information of application
The information of application.yml will overwrite the contents of bootstrap.yml when the same configuration is encountered.
4. Create a new Application.java startup file
@ EnableEurekaServer
@ SpringBootApplication
Public class Application {
Public static void main (String [] args) {
SpringApplication.run (Application.class, args)
}
}
@ EnableEurekaServer means to use the EurekaServer side function, that is, to launch as a registry node.
5. Run Application.java and visit http://localhost:3000 to see the ui console provided by Eureka.
Create a service provider
Next, create a service provider and register with the Eureka registry created above.
1. Add maven dependency package
Org.springframework.boot
Spring-boot-starter-web
Org.springframework.cloud
Spring-cloud-starter-netflix-eureka-client
2. Configure application.yml
Server:
Port: 3001
Eureka:
Instance:
PreferIpAddress: true
Client:
ServiceUrl:
DefaultZone: http://localhost:3000/eureka # # sign in to eureka
Spring:
Application:
Name: single-provider # # application name, which will be used later in the consumer
3. Create a simple RESTful interface controller
@ Slf4j
@ RestController
Public class ProviderController {
@ Autowired
Private DiscoveryClient discoveryClient
@ RequestMapping (value = "/ hello")
Public String hello () {
List services = discoveryClient.getServices ()
For (String s: services) {
Log.info (s)
}
Return "hello spring cloud!"
}
@ RequestMapping (value = "/ nice")
Public String nice () {
List services = discoveryClient.getServices ()
For (String s: services) {
Log.info ("gogogo" + s)
}
Return "nice to meet you!"
}
}
4. Create a spring boot startup class
@ EnableEurekaClient
@ SpringBootApplication
Public class SingleProviderApplication {
Public static void main (String [] args) {
SpringApplication.run (SingleProviderApplication.class, args)
}
}
The @ EnableEurekaClient modifier indicates that you want to register with the registry.
5. Start the project and normally register with the Eureka registry. Open the Eureka console and you will see that this service has already appeared.
Create a service consumer
With the service provider, then create a consumer to consume
1. Reference the maven package
Org.springframework.cloud
Spring-cloud-starter-netflix-eureka-client
Org.springframework.boot
Spring-boot-starter-web
Org.springframework.cloud
Spring-cloud-starter-openfeign
Org.springframework.boot
Spring-boot-starter-actuator
2. Configure application.yml
Server:
Port: 3002
Eureka:
Client:
ServiceUrl:
DefaultZone: http://127.0.0.1:3000/eureka # # sign in to eureka
Instance:
PreferIpAddress: true
Spring:
Application:
Name: single-customer
3. Start consuming the service interface provided by the provider. Here we demonstrate two consumption methods: one is to use RestTemplate, and the other is to use FeignClient,Feign, which is also open source with Netflix, and is encapsulated in the spring-cloud-starter-openfeign package by Spring Cloud.
Create a startup class and add related comments
@ SpringBootApplication
@ EnableEurekaClient
@ EnableFeignClients
Public class SingleCustomerApplication {
/ * *
* inject RestTemplate
* and use @ LoadBalanced annotation to request the service provider with load balancing strategy
* this is the capability provided by Spring Ribbon
* @ return
* /
@ LoadBalanced
@ Bean
Public RestTemplate restTemplate () {
Return new RestTemplate ()
}
Public static void main (String [] args) {
SpringApplication.run (SingleCustomerApplication.class, args)
}
}
@ EnableEurekaClient declares that this project is an eureka client, and @ EnableFeignClients declares that this project can use Feign.
4. Create a service interface class. This is how Feign is used. For detailed usage, you can check the Spring Cloud Feign related documentation.
/ * *
* IHelloService
* configure the service provider: single-provider is the application.name of the service provider
* /
@ FeignClient ("single-provider")
Public interface IHelloService {
@ RequestMapping (value = "/ hello")
String hello ()
@ RequestMapping (value = "nice")
String nice ()
}
The value annotated by @ FeignClient is the appplication.name of the service provider.
5. Create a Controller to invoke the service
@ RestController
Public class ConsumerController {
@ Autowired
Private RestTemplate restTemplate
@ Autowired
Private IHelloService helloService
Private static final String applicationName = "single-provider"
@ RequestMapping (value = "feignRequest")
Public Object feignRequest () {
String s = helloService.nice ()
Return s
}
@ RequestMapping (value = "commonRequest")
Public Object commonRequest () {
String url = "http://"+ applicationName +" / hello "
String s = restTemplate.getForObject (url,String.class)
Return s
}
}
The feignRequest method invokes the service interface using Feign.
The commonRequest method invokes the service interface with the method provided by RestTemplate
These are all the contents of the article "how to use Spring Cloud's Eureka to achieve service registration". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.