background
At present, the company still has many front-end and back-end projects (hereinafter referred to as FM projects). The development students can only jointly debug the alpha environment locally. However, after the project is transferred to the test and performance, the jira orders proposed by the test students are all hwbeta and gamma environments. Therefore, in order to reproduce the problem scene, the development students need to make orders in the alpha environment or bother the test students to make orders. This process is very inefficient.
analysis
We take the local startup of webagent gateway service and joint commissioning of local agentbuy service as an example to analyze the process.
Agentbuy service is a controller layer service, which calls other remote services to obtain data and inject it into the page file (ftl), and then freemaker engine compiles the ftl into html and returns it to the browser
Remote service invocation procedure
Now let's make an in-depth analysis of the remote service invocation process. First, let's see how Eureka registers and discovers services. Eureka's official structure diagram:
Spring Cloud Euraka is a component in the Spring Cloud collection. It is an integration of Euraka for service registration and discovery. Eureka is an open source framework in Netflix. Like Zookeeper and consult, it is used for service registration management. Similarly, Spring Cloud also integrates Zookeeper and consult.
Eureka consists of multiple instances (service instances), which can be divided into two types: Eureka Server and Eureka client. For ease of understanding, we subdivide Eureka client into Service Provider and Service Consumer.
- Eureka Server is the service registration center, which exposes its address to the outside, manages and records the information of service providers, and returns the list of qualified service provider addresses to service consumers
- Service Provider is a Service Provider. After the service is started, the Service Provider registers its own IP, port, service provision and other information with Eureka, and regularly renews its contract to update its status
- Service Consumer is a Service Consumer. It finds the address information of the service provider through Eureka Server, and then initiates a remote call to the service provider
Agentbuy service is a Maven Web project
- Integrate Spring MVC to build front and rear end not separated (MVC) projects;
- Integrate Eureka to build a Service Consumer for service discovery or location services;
- Integrate the Ribbon to realize client load balancing. Spring Cloud Ribbon is a client load balancing tool based on HTTP and TCP, which allows us to easily automatically convert service-oriented REST template requests into client load balanced service calls;
- Integration Feign helps us define and implement the definition of dependent service interfaces. Under the implementation of Spring Cloud feign, you only need to create an interface and configure it with annotations to complete the interface binding of the service provider, which simplifies the development of self encapsulating the service call client when using the Spring Cloud Ribbon. That is, Feign encapsulates the request and response processing with other services;
Take the remote call of inventory service by order service as an example to describe the relationship among Eureka, Ribbon and Feign:
reference resources: Understand the relationship between spring cloud and Eureka, Feign, Ribbon, Hystrix and Zuul core components
Build agentbuy service consumer service
Add Eureka client dependency to pom file
<dependency> <groupId>com.netflix.eureka</groupId> <artifactId>eureka-client</artifactId> <exclusions> <exclusion> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> </exclusion> </exclusions> </dependency>
In Eureka client Relevant configuration information is added in the properties configuration file. Because agentbuy service consumer service does not need to provide services to other services, it does not need to register with the registration center (Eureka Server) and the setting does not need to register Eureka registration. Enabled = false and Eureka registerWithEureka=false
// eureka-client.properties ###Eureka Client configuration for Sample Eureka Client # see the README in eureka-examples to see an overview of the example set up # 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.registration.enabled=false # Indicates whether to register with Eureka Server. The default value is true ## configuration related to reaching the eureka servers eureka.preferSameZone=true eureka.shouldUseDns=false eureka.registerWithEureka=false # Indicates whether to register yourself with Eureka Server. The default value is true eureka.serviceUrl.default=http://10.118.71.204:8761/eureka / # registry address eureka.appinfo.replicate.interval=10 eureka.vipAddress=agentbuy-service # Define service name variable eureka.name=${eureka.vipAddress} # Service name, the basis of service invocation in spring cloud eureka.port=8001 # Service port #eureka.ipAddr=192.168.29.170 #eureka.instanceId=${eureka.vipAddress}:${eureka.port} #eureka.hostname=${eureka.ipAddr} eureka.homePageUrlPath=/${eureka.vipAddress} #eureka.statusPageUrlPath=/${eureka.vipAddress}/status #eureka.healthCheckUrlPath=/${eureka.vipAddress}/health eureka.lease.renewalInterval=5 eureka.lease.duration=15 eureka.decoderName=JacksonJson
Agentbuy service calls inquiry service remotely
On the publish inquiry page, the / getdeliveryaddresses/{companyId}/user/{userLoginId} interface in the agentbuy service service will be called to obtain the harvest address. This interface remotely calls the method of the inquiry service to obtain the harvest address
Use the constructor to create a Bean instance, configure a constructor parameter for the constructor Arg sub element, and assign API to the configAgentUrl attribute (remote service address) in FeignClientBuilder intra. URL, where API intra. The URL variable is in application Defined in the properties configuration file
api.intra.url=http://alpha-api.intra.casstime.com
The above remote calls are completed by Feign, but different from the usual remote calls, FeignClientBuilder is used to dynamically create FeignClient instances to initiate remote calls, rather than using @ Autowired annotation to let spring complete the automatic assembly of bean s and generate instances to initiate remote calls
@Autowired annotation method:
@FeignClient(name = "inquiry-service") public interface InquiryClient extends InquiryService{ }
Then inject FeignClient instance into Spring container:
@Autowired private InquiryClient inquiryClient;
This is because the feign interface provided in the SPI package depends on Spring Boot, so the external system must be a Spring Boot application. At the same time, because the feign interface configuration does not specify the url address of service call, but adopts the method of specifying the service name, if the external system wants to call the feign interface, it must be registered in the same registry as the user authority service. Our agentbuy service is not a Spring Boot service. Using FeignClientBuilder to dynamically create FeignClient instances can get rid of the dependence on Spring Boot.
Agentbuy service local joint commissioning of other environments
As mentioned above, agentbuy service is a freemaker web service. If you need to debug hwbeta and gamma environments locally, you only need to point the remote call service address to hwbeta and gamma environments
(1) Go to the local agentbuy service through the gateway webagent
(2) Change the agentbuy service remote service invocation address
Agentbuy service uses FeignClientBuilder to dynamically create FeignClient instances to initiate remote calls. By specifying the configuragenturl in FeignClientBuilder, the service can be called across Eureka without locating the service through the Eureka Server registry