The evolution of the SpringCloud framework won’t be covered much.

Tools required:

I’m going to use

IntelliJ IDEA (2018.2)

The JDK: 1.8

Maven 3.3.9

Direct dry goods, easy to learn (do not like spray)~

I. Schematic diagram



It can be understood as three parts

1.Eureka Server: It is used to provide service registration and discovery functions.Eureka does not provide background storage, but stores the service in the registry of memory, and keeps the latest state through heartbeat.

2.Eureka Provider: A service Provider is a Client of Eureka. It registers services with the server, sends heartbeat messages to the server, and obtains the service registration list. When a service is registered with a server, it provides its own meta information (host, port, service name, etc.).

3.Eureka Consumer: For services registered and published to the server side, the service Consumer can find and invoke them. The service caller is also a Eureka client, but its main responsibility is to find and invoke the service.

~ start the code ~

Ii. Build a master Project (Multi-module development)

File -> New -> Project – Maven

Create a Maven project



Click Next to create the package name



After creating the directory structure, delete the SRC file, and leave the pom.xml file unchanged for section 3.



Create the Eureka Server module

Select the SCloud project, right-click new-module-Spring Initializr (SpringCloud is a collection of frameworks based on springBoot implementations, Create maven modules if your parent pom.xml already relies on Spring-boot.)







Generate the following directory structure



Modify pom. XML and pom. XML files of eureka_server in the main project

Pom.xml for the main project



Pom eureka_server module. The XML



The main project startup class added the @enableeurekaserver annotation

@SpringBootApplication
@EnableEurekaServer public class EurekaServerApplication {
        public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}
Copy the code

Configure application. Properties under the Eureka_server project



At this point, the Eureka_server service is configured, and now we start the project to access the http://localhost:8666 port



Create the service provider module

When a service provider registers with a server, it provides metadata such as host and port, URL, home page, and so on. Eureka Server receives heartbeat messages from each client instance. If the heartbeat times out, the instance is usually removed from the registry server.

The procedure for creating the eureka_server module is similar to that for creating the Eureka_server module



Pom. XML under the modify project is created



Indicate that you are a Eureka client by annotating @enableEurekaclient.

@SpringBootApplication @EnableEurekaClientpublic class EurekaClientApplication { public static void main(String[] args) { SpringApplication.run(EurekaClientApplication.class, args); }}Copy the code

Configure the service provider’s application.properties



For a simple demonstration, I added a Restful interface to the program entry

@SpringBootApplication
@EnableEurekaClient@RestControllerpublic class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
@Value("${server.port}")Stringport;
@RequestMapping("/hello")
public String home(@RequestParam(value ="name", defaultValue ="gdl") String name) {
     return "hello: " + name +" ,from port:"+port; }}Copy the code

Start the service before starting the service provider. Go to http://localhost:8666






Continue to http://localhost:8667/hello? access interface name=’wrold’



Create a service consumer module

Create the service provider as in Step 4.

Then modify the pom.xml file under the project similar to the service provider by changing the package name and so on.

Modify the application.properties file



Add @enableeurekaclient to the boot class and add the Restful API to provide service instances to obtain cases. The Ribbon+RestTemplate mode is used to remove instances. Feign will be used in the next chapter.



Now we are starting the service, starting the service provider (ignore if both are already started), and starting the service consumer, visit http://localhost:8666/ below



Then we in the access to the service consumer, http://localhost:8668/hello? name=wrold



Nice ~, now that SpringCloud Eureka service discovery and registration is covered, the next chapter integrates Feign with service consumers to provide a load-balancing HTTP client.


Attached source code address: gitee.com/dolan/sclou…

And switch to the F_EUreka branch!!