Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to integrate non-springboot projects into eureka

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to integrate non-springboot projects into eureka". In daily operation, I believe many people have doubts about how to integrate non-springboot projects into eureka. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to integrate non-springboot projects into eureka". Next, please follow the editor to study!

1 background

With the evolution of SOA architecture, more and more service providers need to provide a general infrastructure that can be dynamically scalable.

And for hee "", the same is true. And with the increasing number of TPS, more and more endpoint needs to integrate this distributed infrastructure. Here is a bit similar to the case when google's Bigtable paper was first published, for different businesses, or different functions of the same business, or the same function to achieve node expansion, distributed processing. Some problems in this distributed scenario must be accompanied by a system that implements distributed processing capabilities, which is not discussed here.

2 description of purpose

Similar to Dubbo distributed service governance, different micro-services rely on the registry for service governance and control, as well as springCloud. As the ecology of micro-services, different components are responsible for different functions (for example, archaius to configure and eureka, service PUBSUB, etc.).

3 options comparison 3.1 directly to register

To put it simply. Through the analysis of the source code, it is found that the DiscoveryClient object is the core class for client startup loading.

It registers when it is created, as follows

In the process of restemplate call, you need to get the server through application layer communication. The corresponding server or service you are about to call is maintained in the InstanceInfo,instanceOf whose spring.application.name is vipaddress:

The instance information may not be available here, and finally no corresponding service instance is reported. The above is from Applications Class.

Later on, the source code will be analyzed in detail. Too much analysis here will only solve the problem.

* 4.2 The Error for Register Process*

Here according to the official website case to build the registration environment, there may be all ready to register on the situation.

Go through the registration process as follows:

Archaius will load the config.properties file under classpath as the current memory resource configuration by default, or it can be changed through archaius.configurationSource.defaultFileName. Of course, since it is dynamic, it can also be processed dynamically from multiple data sources. It is not specified in detail.

The significance of the specific configuration is not considered first, but the environment is considered first in order to solve the problem.

* 4.3 eureka/apps for configuration*

The manual registration section does not show empty.

So we need to add this part.

The code is as follows:

Int port = instanceInfo.getPort (); Map maps = Maps.newHashMap (); maps.put ("management.port", String.valueOf (port)); / / activity-manager:dev-10.0.2.17:8010applicationInfoManager.getInstance () .registerAppMetadata (maps)

There is one more detail:

Archaius1Utils.initConfig ("eureka-client")

This is reported when viewing the error log, which needs to be solved for a better compatibility framework, which probably means that this configuration is missing.

The class is initialized together when the implementation class is loaded. Obviously, the component has done it for us, we just need to provide the corresponding configuration file.

Depend on

There are two points to note here:

If you continue, DEBUG will see:

Here we start to call the interceptor recursively, that is, the interceptor we put in at startup will call here.

Of course, here's a detail.

There are two points we need to focus on here.

1 get the core instanceInfo through the service serviceId, which will be explained below

2 Select the corresponding service invocation through the corresponding service information

Correspond to the first and second lines of code respectively

Continue debug

Here, you need to load a LloadBalancer through a serviceId, as follows

Spring calls the above factory load by default, so we will see if we click into it.

There are also two points to note here:

1 our lloadbalancer is loaded only when it is used, in other words, lazy loading

2 there is a cache operation here. If the first if is false, the cache will be returned, in other words, it will only be created once.

And then we go in.

These things.

The above clientConfig is the EurekaClientConfigBean that we set in the code.

There are no above parameters yet, so it doesn't matter because initialization hasn't started yet.

After continuing to load, start loading

Go up

The general idea of the figure is to load the parent class baseLoadbalancer first and then load yourself.

The restOfInit method is the core loading method.

The red icon above is a more important method.

The more important place here is the red mark above.

Go in and see:

He'll be here. What is this?

Correspondingly, you will go from visualHostNameAppMap to List via vipAddress. After successfully getting the instanceInfo, you will return a loadBalancer containing the corresponding instanceInfo when you go back to the place where you just entered the source code.

To sum up:

The principle of load balancer implementation is summarized as follows: load balancer interceptor is loaded according to configuration, user client calls traversal processing, and gets the corresponding instanceInfo through servceId through HTTP (similar to dubbo process, it needs to be cached locally after the first time. The default is 90 seconds to re-call once to pull information) multiple need to select one according to the algorithm and then make a remote call.

The code is as follows:

Package com.kili.lipapay.nmc.common;import com.google.common.collect.Maps;import com.netflix.appinfo.ApplicationInfoManager;import com.netflix.appinfo.InstanceInfo;import com.netflix.appinfo.providers.EurekaConfigBasedInstanceInfoProvider;import com.netflix.config.ConfigurationManager;import com.netflix.config.DynamicPropertyFactory;import com.netflix.discovery.DefaultEurekaClientConfig;import com.netflix.discovery.DiscoveryClient;import com.netflix.discovery.DiscoveryManager;import com.netflix.discovery.EurekaClient;import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;import java.io.* Import java.net.InetAddress;import java.net.Socket;import java.util.Date;import java.util.Map;import java.util.Properties;@Componentclass SimpleEurakeService {private static final DynamicPropertyFactory configInstance = com.netflix.config.DynamicPropertyFactory .getInstance (); public void registerWithEureka () throws IOException {ApplicationInfoManager applicationInfoManager = null; EurakeInstanceConfig config = null; InstanceInfo instanceInfo = null / / Register with Eureka if (applicationInfoManager = = null) {config = new EurakeInstanceConfig (); instanceInfo = new EurekaConfigBasedInstanceInfoProvider (config). Get (); applicationInfoManager = new ApplicationInfoManager (config, instanceInfo);} / / Archaius1Utils.initConfig ("eureka-client"); Properties properties = new Properties (); InputStream inputStream = Thread.currentThread (). GetContextClassLoader (). GetResourceAsStream ("config.properties") Properties.load (inputStream); properties.setProperty ("eureka.ipAddr", InetAddress.getLocalHost (). GetHostAddress ()); String instanceId = applicationInfoManager.getInfo (). GetAppName () + ": dev-" + properties.getProperty ("eureka.ipAddr") + ":" + properties.getProperty ("eureka.port"); properties.setProperty ("eureka.instanceId", instanceId); ConfigurationManager.loadProperties (properties); int port = instanceInfo.getPort () Map maps = Maps.newHashMap (); maps.put ("management.port", String.valueOf (port)); / / activity-manager:dev-10.0.2.17:8010 applicationInfoManager.getInstance () .registerAppMetadata (maps); applicationInfoManager.getInstance () .setInstanceStatus (InstanceInfo.InstanceStatus.UP); EurekaClient eurekaClient = new DiscoveryClient (applicationInfoManager, new DefaultEurekaClientConfig ()) String vipAddress = configInstance.getStringProperty ("eureka.vipAddress", "unknown"). Get (); InstanceInfo nextServerInfo = null; while (nextServerInfo = = null) {try {nextServerInfo = eurekaClient .getNextServerFromEureka (vipAddress, false) } catch (Throwable e) {System.out .println ("Waiting for service to register with eureka.."); try {Thread.sleep (10000);} catch (InterruptedException E1) {/ / TODO Auto-generated catch block e1.printStackTrace () }} public void unRegisterWithEureka () {/ / Un register from eureka. DiscoveryManager.getInstance (). ShutdownComponent ();} private void processRequest (final Socket s) {try {BufferedReader rd = new BufferedReader (new InputStreamReader (s.getInputStream (); String line = rd.readLine (); if (line! = null) {System.out.println ("Received the request from the client.") } PrintStream out = new PrintStream (s.getOutputStream ()); System.out.println ("Sending the response to the client..."); out.println ("Reponse at" + new Date ());} catch (Throwable e) {System.err.println ("Error processing requests") } finally {if (s! = null) {try {s.close ();} catch (IOException e) {/ / TODO Auto-generated catch block e.printStackTrace () } @ PostConstruct private void init () throws IOException {SimpleEurakeService sampleEurekaService = new SimpleEurakeService (); sampleEurekaService.registerWithEureka ();}

The configuration is as follows:

# note that for a purely client usage (e.g. Only used to get information about other services,# there is no need for registration. This property applies to the singleton DiscoveryClient so# if you run a server that is both a service provider and also a service consumer # then don't set this property to false.#eureka.shouldEnforceRegistrationAtInit=false## configuration related to reaching the eureka serversSSeureka.client.service-url.defaultZone= http://localhost:7025/eurekaeureka.serviceUrl.default=http://localhost:7025/eurekaspring.cloud.config.discovery.enabled=truespring.cloud.config.discovery.service-id=config-servereureka.region=defaultspring.application.name=sampleservice#Name of the application to be identified by other serviceseureka.name=sampleservice#Virtual host name by which the clients identifies this serviceeureka.vipAddress=sampleservice#The port where the service will be running and serving requestseureka.port=8080#For eureka clients running in eureka server It needs to connect to servers in other zoneseureka.preferSameZone=false#Change this if you want to use a DNS based lookup for determining other eureka servers. For example#of specifying the DNS entries, check the eureka-client-test.properties, eureka-client-prod.propertieseureka.shouldUseDns=falseeureka.us-east-1.availabilityZones=defaulterueka.registration.enabled=true so far, the study on "how to integrate non-springboot projects into eureka" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report