Upgrading MicroServices Architecture 2: Service Registration

Source: Internet
Author: User

In a microservices architecture, a service is the smallest, scalable, standalone deployment unit that can have multiple instances of the same service offering, which are registered to the Service registry (Eureka Server) for unified management and load balancing of calls.
Because spring cloud is already Java as the main development language, this article will first speak the Java language of the service how to register to the service center, and then follow this logic to migrate to the. NET version.

  1. Create a Java version of the service and register it with the service center

  1.1 Creating a MAVEN project for Eureka Client

  Mode of operation and the previous article using MAVEN to create Eureka server, module name: userservice (User Service)

Eureka client and Web Add dependencies:  

<Dependency>    <groupId>Org.springframework.boot</groupId>    <Artifactid>Spring-boot-starter-web</Artifactid></Dependency><Dependency>    <groupId>Org.springframework.cloud</groupId>    <Artifactid>Spring-cloud-starter-netflix-eureka-client</Artifactid></Dependency>

Create the spring Boot startup class and add annotations @springbootapplication and @enableeurekaclient

  

Create a user entity, a Usercontroller class, and annotate it as Restcontroller, and write a method that returns a list.

@RestController @requestmapping ("/USER") Public classUsercontroller {@RequestMapping ("/getall")     PublicList<user>GetAll () {ArrayList<User> list=NewArraylist<>(); User User1=NewUser (); User1.setage (10); User1.setname ("Xiao Ming"); User1.setdeleted (false); User User2=NewUser (); User2.setage (12); User2.setname ("Little Red"); User2.setdeleted (true);        List.add (user1);        List.add (User2); returnlist; }

  1.2 Configuring the Service Center

Service configuration information:

Server:  port:7771 #服务端口eureka:  client:    registerwitheureka:true #是否注册    fetchregistry:true #启用客户端缓存    serviceurl:      defaultzone:http://peer1:8881/eureka/,http://peer2:8882/eureka/#注册到两个服务中心spring:  Application:    name:userservice #服务名

Start the service, refresh under Service center, you can see that UserService has been registered successfully

  

Access UserService to get the user's method to successfully return the JSON data

  

  1.3 Launch multiple UserService service instances and register

Ieda Modify the boot configuration, remove the launch only a single instance, Edit configurations-> Select the configuration you want to modify, remove the check single Instance

  

  

Modify the port for UserService to 7772 to start an instance, and then modify the port to 7773 to start successfully, so that three instances are registered to the service center

  

The Java version of the service registration is complete, install this idea use. NET core to create the same service and register to the service center

  2. Create a . NET core service and register with the service center

  2.1 Creating a. NET Core API Project  

Create an empty solution Microservice, and then create a. NET core Web API Project UserService

  

Select. NET core 2.1, project type selection API, temporarily without HTTPS, remove tick

  

Search for Pivotal.Discovery.Client in the NuGet Package Manager, select. NET Core version Pivotal.Discovery.ClientCore, which is equivalent to Eureka Client components in Java For service registration, the latest stable version is now 2.0.1, non-core version is also available, but the most recent update is September 2017, where the core version is selected.

  

  2.2 Configuring the Service Center

The service registration configuration can refer to the Steeltoe official documentation, and the Java version of the Eureka client configuration is roughly similar

Configuration file:

{  "Logging": {    "LogLevel": {      "Default": "Warning"    }  },  "Allowedhosts": "*",/*Service Registration Configuration*/"Spring": {    "Application": {      "Name": "UserService"/*Service Name*/    }  },  "Eureka": {    "Client": {      "serviceurl": "http://localhost:8881/eureka/",/*Eureka Service Address*/"Shouldregisterwitheureka":true,/*whether to register to Eureka Server*/"Shouldfetchregistry":true /*turn on local cache*/    },    "Instance": {      "Port": 7779/*Service Port*/    }  }}

The practice found that the serviceurl in Eureka configuration file can only use one address, multiple service center addresses do not know why not registered, and can only use localhost or IP, such as 127.0.0.1, using peer1,peer2 also registered, What's the reason for not studying yet.

Specifying a different environment profile in the program class

Reference: http://steeltoe.io/docs/steeltoe-discovery/#reading-configuration-values
Failure to specify a profile causes an error: Argumentexception:discovery client type UNKNOWN, check config, because the configuration file could not be found. Configuration.getsection ("Eureka") can be added when configuring service discovery. GetChildren (). Any () to determine if the configuration file for the Eureka node can be taken.

  

Add a service Discovery client configuration in the Startup Startup class Configureservices method, and add a method that uses the service discovery client in the Configure method

This is similar to the Eureka client annotation set in spring boot class

  

Create a user entity with the same attributes as the Java side, noting that the fields corresponding to the getter and setter for Java are lowercase, and the serialization at the beginning of the default is to remove the previous is.

  

Also create a usercontroller and a GetAll method that returns the list of users.

  2.3 Service start configuration and register to service center

Select IIS Express debugging in Project properties, debugging, and set the port to service port 7779

or change the port directly on the Launchsettings.json.

  

Start vs Debug the service, the browser calls the Api,http://localhost:7779/user/getall

Successfully returned JSON information

  

Refresh the next Eureka Server, discover the service and register successfully.

The micro-service to this. NET core has also successfully completed registration.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.