1. Development of Eureka Server

(1) Create tensquare_eureka module
(2) Introducing a Spring Cloud version that relies on the parent project pom.xml


Note: It's the parent project's pom.xml
Tenquare_eureka module pom.xml introduces eureka-server


(2) Add application.yml

  port: 6868 #Service Port
    registerWithEureka: false #Whether to register oneself in Eureka service or not is in itself all that does not need to be registered.
    fetchRegistry: false #Whether to get registration information from Eureka
    serviceUrl: #The address where the Eureka client interacts with the Eureka server

(3) Writing startup classes
Create the package com.tensquare.eureka, and create classes under the package

public class EurekaServer {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServer.class, args);

(4) Start running the startup class and enter it in the browser address bar http://localhost:6868/ The operation effect is as follows

In the main interface, system status is system information General Info is general information Instances currently registered with Eureka is a list of all micro services registered.

II. Service Registration

Register all microservices in Eureka so that all microservices can call each other.
(1) Adding dependencies to other micro-service modules


(2) Modify the application.yml for each microservice and add the configuration for registering the eureka service

      defaultZone: http://localhost:6868/eureka
    prefer-ip-address: true

(3) Modify the startup class of each service class and add annotations


(4) Start the test: Start each micro-service and you will find that you can see these micro-clothes in the registration list of eureka

There are nine in all.

3. Feign implements calls between services

  1. Introduction to Feign
    Feign is a tool to simplify Java HTTP client development (java-to-http client-binder), inspired by Retrofit, JAXRS-2.0 and WebSocket. The original intention of Feign is to reduce the complexity of uniformly binding Denominator to HTTP API without distinguishing whether it is restful or not.
  2. Quick Experience
    Now we are asking and answering the method of invoking basic micro services (querying labels according to ID)
    (1) Adding dependencies to tensquare_qa module

(2) Modify the startup class of tensquare_qa module and add annotations


(3) Create com.tensquare.qa.client package in tensquare_qa module and create interface under the package

public interface LabelClient {
    @RequestMapping(value="/label/{id}", method = RequestMethod.GET)
    public Result findById(@PathVariable("id") String id);

@ The FeignClient annotation is used to specify which service to invoke the function from. Note that the name in the annotation is consistent with the name of the invoked service and cannot contain underscores.

@ The Request Mapping annotation is used for address mapping of the invoked microservice. Note that the @PathVariable annotation must specify the parameter name, otherwise an error will occur

(5) Modifying ProblemController of tensquare_qa module

	private LabelClient labelClient;
	@RequestMapping(value = "/label/{labelid}")
	public Result findLabelById(@PathVariable String labelid){
		Result result = labelClient.findById(labelid);
		return result;

(6) Testing: http://localhost:9003/problem/label/1 See label information

3. Load Balancing
Test: Open multiple basic micro services at the same time to see if they are invoked in turn.
Modifying findById Method of LabelController in Tenquare_base Project

@RequestMapping(value="/{id}", method = RequestMethod.GET)
	public Result findById(@PathVariable String id){
		return new Result(true,StatusCode.OK,"query was successful",labelService.findById(id) );

After starting the basic micro-service, modify the port and output information, and start the basic micro-service again.
Start the Q&A microservice, and the browser executes http://localhost:9003/problem/label/1 to see if it starts in turn.