Categories
Uncategorized

Java Micro Services (2): The service consumers and providers to build

Then write a paper on the “Java micro-services (a): dubbo-admin console use,” the article describes the installation docker, zookeeper environment and reference dubbo official website demonstrates the use of dubbo-admin console. The article has been to build a good zookeeper service registry, the film article is mainly to build service consumers and service providers. In accordance with the principle of micro-services, it will demo is divided into three parts: service interface, service consumers and service consumers.

Service Interface: defines all the interfaces required for the system.

Service Provider: The main is to achieve the interface.

Consumer Services: the use of the interface

1.Dubbo Introduction

 

 

 

node

Role Description

Expose service provider service

Call the remote service consumer services

Service registration and discovery registries

The number of calls statistics monitoring center services and call time

Run container services

Provider
Consumer
Registry
Monitor
Container
 

Dubbo architecture has the following characteristics, namely communication, robustness, flexibility, and upgradeability to future architectures.

Call relationship Description

    Responsible for starting the service container, load, run service provider.

    Service provider when you start, registration services they provided to the registry.

    Consumer services at startup, you need to subscribe to the service registry.

    Registry return address list service providers to consumers, if there is a change, the registry will be based on long push to change the data connection to the consumer.

    Service consumers, providers from the list of addresses, soft load balancing algorithm, choose a provider call, if the call fails, then select another call.

    Service consumers and providers, in memory of the cumulative number of calls and call time, time sent once per minute statistical data to the monitoring center.

Dubbo architecture has the following characteristics, namely communication, robustness, flexibility, and upgradeability to future architectures.

A more detailed description, please refer to the examiner net: http: //dubbo.apache.org/en-us/

2. Service Interface

Creating a jar by the project idea, project creation process can refer to the “Spring boot entry (a): Spring boot quickly build project,” The purpose of this project is simply defined interfaces, so the direct creation jar package, not a maven project here. Once it’s created, and a new interface. The following is the interface that I created:

 

Wherein UserService code is as follows:

1 package com.edu.hello.dubbo.service.user.api;
2 
3 public interface UserService {
4     String sayHi();
5 }

After creating the interface, you need to install an interface to a local warehouse, for service consumers and service providers use

In Terminal directly to mvn clean install or by clicking directly in the install directory lifecycle installed, the following page appears to indicate a successful installation

 

 

3. Service Provider

Service provider is mainly to achieve the interface, create a maven project using the same method to create a maven project directory after a good as follows:

 

Wherein UserServicelmpl achieve multiple interfaces, as follows:

 1 package com.edu.hello.dubbo.service.user.provider.api.impl;
 2 
 3 import com.alibaba.dubbo.config.annotation.Service;
 4 import com.edu.hello.dubbo.service.user.api.UserService;
 5 import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
 6 import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
 7 import org.springframework.beans.factory.annotation.Value;
 8 
 9 /**
10  * @ClassName UserServiceImpl
11  * @Deccription TODO
12  * @Author DZ
13  * @Date 2019/8/31 11:20
14  **/
15 @Service(version = "${user.service.version}")
16 public class UserServiceImpl implements UserService {
17 
18     @Value("${dubbo.protocol.port}")
19     private String port;
20 
21     /*@HystrixCommand(commandProperties = {
22             @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
23             @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000")
24     })*/
25     @Override
26     public String sayHi() {
27         return "Say Hello, i am from " + port;
28     }
29 }

Which will be mentioned later @HystrixCommand comment fuse, where the first comment.

yml configuration is as follows:

 1 spring:
 2   application:
 3     name: hello-dubbo-service-user-provider
 4 
 5 user:
 6   service:
 7     version: 1.0.0
 8 
 9 dubbo:
10   scan:
11     basePackages: com.edu.hello.dubbo.service.user.provider.api
12   application:
13     id: hello-dubbo-service-user-provider
14     name: hello-dubbo-service-user-provider
15     qos-port: 22222
16     qos-enable: true
17   protocol:
18     id: dubbo
19     name: dubbo
20     port: 12346
21     status: server
22     serialization: kryo #高速序列化
23     # optimizer:
24 
25   registry:
26     id: zookeeper
27     address: zookeeper://192.168.1.12:2181?back=192.168.1.12:2182,192.168.1.12:2183
28   provider:
29     loadbalance: roundrobin #负载均衡
30 
31 
32 
33 management:
34   endpoint:
35     dubbo:
36       enable: true
37     dubbo-shutdown:
38       enabled: true
39     dubbo-configs:
40       enabled: true
41     dubbo-services:
42       enabled: true
43     dubbo-references:
44       enabled: true
45     dubbo-peoperties:
46       enabled: true
47   health:
48     dubbo:
49       status:
50         defaults: memory
51         extras: load,threadpool

View Code

pom file as follows:

 1 xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0modelVersion>
 5     <parent>
 6         <groupId>org.springframework.bootgroupId>
 7         <artifactId>spring-boot-starter-parentartifactId>
 8         <version>2.1.7.RELEASEversion>
 9         <relativePath/> 
10     parent>
11     <groupId>com.edugroupId>
12     <artifactId>hello-dubbo-service-user-providerartifactId>
13     <version>1.0.0-SNAPSHOTversion>
14     <name>hello-dubbo-service-user-providername>
15     <description>Demo project for Spring Bootdescription>
16 
17     <properties>
18         <java.version>1.8java.version>
19     properties>
20 
21     <dependencies>
22         <dependency>
23             <groupId>org.springframework.bootgroupId>
24             <artifactId>spring-boot-starterartifactId>
25         dependency>
26         <dependency>
27             <groupId>org.springframework.bootgroupId>
28             <artifactId>spring-boot-starter-actuatorartifactId>
29         dependency>
30         <dependency>
31             <groupId>org.springframework.bootgroupId>
32             <artifactId>spring-boot-starter-testartifactId>
33             <scope>testscope>
34         dependency>
35         <dependency>
36             <groupId>com.alibaba.bootgroupId>
37             <artifactId>dubbo-spring-boot-starterartifactId>
38             <version>0.2.0version>
39         dependency>
40         <dependency>
41             <groupId>com.edugroupId>
42             <artifactId>hello-dubbo-service-user-apiartifactId>
43             <version>${project.version}version>
44         dependency>
45         <dependency>
46             <groupId>de.javakaffeegroupId>
47             <artifactId>kryo-serializersartifactId>
48             <version>0.42version>
49         dependency>
50         
51         
52         <dependency>
53             <groupId>org.springframework.cloudgroupId>
54             <artifactId>spring-cloud-starter-netflix-hystrixartifactId>
55             <version>2.0.1.RELEASEversion>
56         dependency>
57         
58         <dependency>
59             <groupId>org.springframework.cloudgroupId>
60             <artifactId>spring-cloud-starter-netflix-hystrix-dashboardartifactId>
61             <version>2.0.1.RELEASEversion>
62         dependency>
63 
64 
65 
66     dependencies>
67 
68     <build>
69         <plugins>
70             <plugin>
71                 <groupId>org.springframework.bootgroupId>
72                 <artifactId>spring-boot-maven-pluginartifactId>
73             plugin>
74         plugins>
75     build>
76 
77 project>

View Code

This paper pom file and yml dubbo file is mainly based on the official website of the service provider pom file dependencies from, with specific reference to: https: //github.com/apache/dubbo-spring-boot-project/tree/master/dubbo -spring-boot-samples / dubbo-registry-zookeeper-samples.

 

 

Note basePackages comment

4. Service Provider

Create a service provider in the same way who, profiles and service providers are also similar, directly attached to the code

 

 

 1 package com.edu.hello.dubbo.service.user.consumer.controller;
 2 
 3 
 4 import com.alibaba.dubbo.config.annotation.Reference;
 5 import com.edu.hello.dubbo.service.user.api.UserService;
 6 import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
 7 import org.springframework.web.bind.annotation.RequestMapping;
 8 import org.springframework.web.bind.annotation.RequestMethod;
 9 import org.springframework.web.bind.annotation.RestController;
10 
11 /**
12  * @ClassName UserController
13  * @Deccription TODO
14  * @Author DZ
15  * @Date 2019/8/31 18:37
16  **/
17 @RestController
18 public class UserController {
19 
20     @Reference(version = "${user.service.version}")
21     private UserService userService;
22 
23     @HystrixCommand(fallbackMethod = "sayHiError")
24     @RequestMapping(value = "hi", method = RequestMethod.GET)
25     public String sayHi() {
26         return userService.sayHi();
27     }
28 
29     public String sayHiError() {
30         return "Hystrix fallback";
31     }
32 }

 

 

pom yml profile and profile substantially similar and providers;

 1 spring:
 2   application:
 3     name: hello-dubbo-service-user-consumer
 4 
 5 user:
 6   service:
 7     version: 1.0.0
 8 
 9 dubbo:
10   scan:
11     basePackages: com.edu.hello.dubbo.service.user.consumer.controller
12   application:
13     id: hello-dubbo-service-user-consumer
14     name: hello-dubbo-service-user-consumer
15     qos-port: 22223
16     qos-enable: true
17   protocol:
18     id: dubbo
19     name: dubbo
20     port: 12345
21     #status: server
22     serialization: kryo
23   registry:
24     id: zookeeper
25     address: zookeeper://192.168.1.12:2181?back=192.168.1.12:2182,192.168.1.12:2183
26 
27 
28 management:
29   endpoint:
30     dubbo:
31       enable: true
32     dubbo-shutdown:
33       enabled: true
34     dubbo-configs:
35       enabled: true
36     dubbo-services:
37       enabled: true
38     dubbo-references:
39       enabled: true
40     dubbo-peoperties:
41       enabled: true
42   health:
43     dubbo:
44       status:
45         defaults: memory
46         extras: load,threadpool
47 server:
48   port: 9090

View Code

 1 xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0modelVersion>
 5     <parent>
 6         <groupId>org.springframework.bootgroupId>
 7         <artifactId>spring-boot-starter-parentartifactId>
 8         <version>2.1.7.RELEASEversion>
 9         <relativePath/> 
10     parent>
11     <groupId>com.edugroupId>
12     <artifactId>hello-dubbo-service-user-consumerartifactId>
13     <version>1.0.0-SNAPSHOTversion>
14     <name>hello-dubbo-service-user-consumername>
15     <description>Demo project for Spring Bootdescription>
16 
17     <properties>
18         <java.version>1.8java.version>
19     properties>
20 
21     <dependencies>
22         <dependency>
23             <groupId>org.springframework.bootgroupId>
24             <artifactId>spring-boot-starter-webartifactId>
25         dependency>
26         <dependency>
27             <groupId>org.springframework.bootgroupId>
28             <artifactId>spring-boot-starter-actuatorartifactId>
29         dependency>
30         <dependency>
31             <groupId>org.springframework.bootgroupId>
32             <artifactId>spring-boot-starter-testartifactId>
33             <scope>testscope>
34         dependency>
35         <dependency>
36             <groupId>com.alibaba.bootgroupId>
37             <artifactId>dubbo-spring-boot-starterartifactId>
38             <version>0.2.0version>
39         dependency>
40         <dependency>
41             <groupId>com.edugroupId>
42             <artifactId>hello-dubbo-service-user-apiartifactId>
43             <version>${project.version}version>
44         dependency>
45         <dependency>
46             <groupId>de.javakaffeegroupId>
47             <artifactId>kryo-serializersartifactId>
48             <version>0.42version>
49         dependency>
50         
51         
52         <dependency>
53             <groupId>org.springframework.cloudgroupId>
54             <artifactId>spring-cloud-starter-netflix-hystrixartifactId>
55             <version>2.0.1.RELEASEversion>
56         dependency>
57         
58         <dependency>
59             <groupId>org.springframework.cloudgroupId>
60             <artifactId>spring-cloud-starter-netflix-hystrix-dashboardartifactId>
61             <version>2.0.1.RELEASEversion>
62         dependency>
63     dependencies>
64 
65     <build>
66         <plugins>
67             <plugin>
68                 <groupId>org.springframework.bootgroupId>
69                 <artifactId>spring-boot-maven-pluginartifactId>
70                 <configuration>
71                     <mainClass>com.edu.hello.dubbo.service.user.consumer.HelloDubboServiceUserConsumerApplicationmainClass>
72                 configuration>
73             plugin>
74         plugins>
75     build>
76 
77 project>

View Code

This service fuse inside on the code and load balancing can temporarily focus back will be devoted to discussion of the fuse.

 

5. Results

Respectively, after starting the service consumers and service providers, started successfully, as follows:

 

Visit http: // localhost: 9090 / hi

 

At the same time we can start dubbo-admin console to view the service, attention to port conflict:

 

Leave a Reply