2 micro-architecture upgrade service: service registration

Original link: http://www.cnblogs.com/townsend/p/9525745.html

  Micro-service architecture, service is the smallest unit of independent deployment of scalable, with a service provider can have multiple instances, load balancing and call the unified management of these instances are registered with the service registry (Eureka Server).
  Spring Cloud is due to have Java as the main development language, Java language this article will talk about how the service registered to the service center, according to this logic then ported to the .net version.

  1. Create a java version of the service, and sign up to the service center

  Eureka Client 1.1 Creating a Maven project

  Mode of operation and the use Maven to create a Eureka Server, the module name: userservice (User Service)

  Eureka Client and web add-dependent:  

<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 a Spring Boot startup class and add comments @SpringBootApplication and @EnableEurekaClient

  

  Create a User entity, a UserController class and annotated RestController, write a list of return method.  

@RestController
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/getall")
    public List<User> getAll(){
        ArrayList<User> list=new ArrayList<>();
        User user1=new User();
        user1.setAge(10);
        user1.setName("小明");
        user1.setDeleted(false);

        User user2=new User();
        user2.setAge(12);
        user2.setName ( "red" );
        user2.setDeleted(true);

        list.add(user1);
        list.add(user2);
        return list;
    }

  1.2 Configuration Service Center

  Service configuration information:

server:
  port: 7771 # port services

eureka:
  client:
    registerWithEureka: true # is registered
    fetchRegistry: true # Enable client-side caching
    ServiceUrl:
      defaultZone: http: // peer1: 8881 / eureka /, http: // peer2: 8882 / eureka / # registered to two service centers

spring:
  application:
    name: userservice # service name

  Start the service, refresh the service center, you can see userservice has been registered

  

  Userservice get the user's access method, successfully returned Json data

  

  1.3 launch multiple instances userservice service and register

  IEDA modify the startup configuration, only a single instance start removed, Edit Configurations-> choose to modify the configuration -> Single Instance only remove the tick

  

  

  Modify userservice port for the 7772 start an instance, and then modify the port to start a successful 7773 launch, so there are three instances registered to the service center

  

  Java version of the service registration is complete, install the idea of ​​using the .net core to create a similar service and register to the service center

  2. Create a .net core services, and registered with the service center

  2.1 Creating .net core API project 

   Create an empty solution MicroService, and then create a .net core web api project UserService

  

  Select the .net core 2.1, select the type of project API, temporarily do not HTTPS, remove the tick

  

  Search NuGet package manager Pivotal.Discovery.Client, select the .net core version Pivotal.Discovery.ClientCore, this component is equivalent to Java in Eureka Client component for service registration, and now the latest stable version is 2.0.1, non Core version is also available, but was most recently updated in September 2017, here choose Core version.

  

  2.2 Configuration Service Center

  Service registry configuration, see steeltoe official documents , and java version of the Eureka Client configuration substantially similar  

  Profiles:   

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*" ,
 / * service registry configuration * / 
  "the Spring" : {
     "the Application" : {
       "name": "UserService" / * service name * /
    }
  },
  "Eureka" : {
     "Client" : {
       "serviceUrl": "HTTP: // localhost: 8881 / Eureka /", / * Eureka service address * / 
      "shouldRegisterWithEureka": to true , / * is registered to the Server Eureka * / 
      " shouldFetchRegistry ": to true  / * open the local cache * /
    },
    "instance" : {
       "Port": 7779 / * service port * /
    }
  }
}

  The practice was found Eureka profile serviceUrl can only use one address, multiple addresses do not know why the service center can not register, but can only use localhost or IP, such as 127.0.0.1, use peer1, peer2 also can not register, what The reason being not to study.

  Specify a different environment configuration file in the Program class

  Reference: http: //steeltoe.io/docs/steeltoe-discovery/#reading-configuration-values
  If you do not specify the configuration file will lead to error: ArgumentException: Discovery client type UNKNOWN, check configuration, the reason I could not find the configuration file, Configuration service discovery dissecting Configuration.GetSection ( "eureka"). GetChildren (). Any () to determine whether to take the profile eureka node.

  

  Adding service discovery client configuration, add the client to use the service method found in the Configure method in the Startup startup class ConfigureServices method

  This is similar to the class start in Spring Boot notes as set Eureka Client

  

  Create a User entity attribute and the Java side as the attention java Getter and Setter corresponding field is a lowercase, and the default is the beginning of the sequence will remove the front is.

  

  Also create a UserController getAll and a method that returns a list of users.

  2.3 Service startup configuration and registration to the service center

  > Debugging select IIS Express debugging and port to port service 7779-- In the Project Properties

   

  Or directly change the port launchSettings.json

  

  VS start debugging the service, the browser calls the API, http: // localhost: 7779 / user / getall

  Successful return Json information

  

  Then refresh Eureka Server, service discovery and registration was successful.

   

  .Net core of this micro-registration service has also been completed successfully.

Reproduced in: https: //www.cnblogs.com/townsend/p/9525745.html

Guess you like

Origin blog.csdn.net/weixin_30194391/article/details/94785517