Can Eureka registry one service like two different services? - java

I have few micro services and I use eureka for service discovery. I want to split one micro service, but I can't do it from code now(create separate jar). I want to registry one service twice in eureka with same address but different service name. Can I do it?

The answer is No!
Assuming you are using Spring Boot, the Service name is going to be your spring-application-name. Hence, you cannot have more than one name registered from a single Jar
But you have mentioned that you have the constraint that the address should also be same. May be that if that can be relaxed, through Spring Cloud Config you can start multiple instance of your Service with different Profile names and under each profile's config you can override your spring-application-name.

Related

Kubernetes Java Spring microservices - generating a unique identifier for each container/service replica

I'm working with microservices in Java Spring MVC. With Kubernetes, the pods containing this microservice application logic can scale/replicate based on incoming load. In few words, there can be 2 or more copies of my application running.
I require to have a specific identifier mechanism which describes the specific pod replica / container containing the application. I was thinking to generate a random number as a descriptor at runtime and store it as an identifier to the container. But I was wondering if there is a better way, considering that I am working with Spring, TomCat and Kubernetes, I would expect that some of this tech stack can do something like this for me?
Kubernetes can do this. Each Pod will have a unique name that you can access as the hostname or through an environment variable. If you use a standard Deployment resource though this can change if the Pod dies and is recreated. It sounds to me like you want a StatefulSet, in which Pods are assigned unique ordinal indexes and retain these when recreated - https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#pod-identity

Create multiple Ribbon clients dynamically

I have a spring microservices architecture application but we aren't using eureka or any other service discovery. My requirement is to create a dynamic load balancer. So, for each services there will be unique and dynamic set of servers.
The configuration will be something like:
ribbon:
listOfServices: say-hello-service, say-hi-service
What I don't want to do is, repeat #RibbonClient annotation for each service client that I will use.
I found out that RibbonClientSpecification is the key component of the Ribbon factory. If I register it with names say-hello-service.RibbonClientSpecification and say-hi-service.RibbonClientSpecification, it will try to call respective service even though I don't have #RibbonClient and any Ribbon related configuration. So my questions are
Why is #RibbonClient annotation necessary since we can identify the services from the properties defined? It seems to me that, it is redundant what we have to define ribbon properties in properties file and as well as have the annotation with same name.
How do I register load balancers for each ribbon client that I create dynamically?
What is the right of creating multiple ribbon clients dynamically without having multiple #RibbonClient configuration classes?
You can use its as
#RibbonClient("{services1,service2}")
No need to specify or create new ribbon all the time for different service.

Spring Cloud shared configuration for group of microservices

Is it possible to share some properties for limited group of microservices? I mean I just want to declare common datasourse in one place for several microservices, which will use the same database
I try to implement it using gradle variables, which should consist all data related to db connection with profiles, but probably easier way to do it exists.
Thanks
Yes it is. You're looking for "Spring Cloud Config" server:
Spring Cloud’s config server acts as a single source of configuration
data for all other services in a microservice-based application. It is
itself a microservice whose job is to obtain configuration data from a
Git repository and serve it via RESTful endpoints. All other services
can either consume the config server’s API directly or, if they’re
Spring applications, treat the configuration server as another
property source in Spring’s Environment abstraction.
You can find a nice reading along with examples here.

Spring Boot sperate #Configurations for multiple application contexts

I'd like to run one spring boot application but have it listen on multiple ports.
The aim is to be able to let an Apache forward multiple (sub-) domains to the spring boot application (Tomcat) on different ports. Example:
example.com/** -> PORT 8080
client.example.com/** -> PORT 8090
employee.example.com/** -> PORT 8100
As far as I understood from several threads on SO, I'm best off launching multiple #SpringBootApplication Annotated classes from one main class, right? (https://stackoverflow.com/a/25870132/1510659)
What I didn't grasp yet, is how to configure each one of those applications separately.
Let's say I have launched these three Applications as shown in the linked post above:
MainExampleApplication
ClientExampleApplication
EmployeeExampleApplication
Now, for example, I want to have separate Spring Security #Configuration classes for each of these Applications, as well as #RequestMappings which might have the same value (e.g. "/").
How do I tell the #Configuration or #Controller classes which Application they are assigned to?
Or are there properties that can to be passed to the applications on startup to specify which resources are responsible for the context?
I hope I'm not going in a totally wrong direction here. I do have experience with Spring MVC and have configured some rather simplistic Spring applications - but not with multiple contexts. I'd be really glad if someone could lead me in the right direction. Thank you in advance.
UPDATE
As mentioned in iamiddy's answer and xeon's comment, I used Spring Profiles for that. I provided the SpringApplicationBuilder with a profile for each of my application contexts on startup and used the #Profile("some_profile") on the #Components that should only be available to some of the contexts.
Use Profiles it's a great spring feature, loads only beans associted with the profile.
Once that's done start your applications N times with different port and profile arguments
Ex: Here is how you would start the first one, do it for the rest to your N
java -jar -Dspring.profiles.active=production1 -Dserver.port=9000 app.jar

Inject a bean from another application context?

Is it possible to inject a bean from a web application that deploy in another server!
I declare a scenario to myself, I have two web application that use spring framework and deploy separately in different application servers (one is TOMCAT and another one is WEBLOGIC),the first application has ServiceA and the second one has ServiceB, now I want to inject ServiceB in ServieA?
I try to do this with RMI once an another one with JMS, now I am wondering that:
Is it possible with another thing?
Is there any active project about this scenario exist?
How can share application context in spring framework, is it possible?
Thanks.
Bean is just an object in JVM. You certainly cannot use an object from one JVM in another JVM straightforward.
But you can do 2 things:
Use proxies - some objects that will have the same interface but invoke somehow to the proper server as implementation.
Use service-oriented architecture (SOA). Each server should have some limited set of beans that are responsible for their functionality. And all beans can interact with each other.
Maybe OSGI is suitable for this.
Web services, JAX-RS is the simplest. But JAX-WS provides you with the tools to automatically generate the client code.

Categories

Resources